Generating open API spec (api.yaml) from iNaturalistAPI/openapi/paths/v2

CC @pisum

I’d like to use https://github.com/oapi-codegen/oapi-codegen to generate a Go language API client for the iNaturalist API. That tool expects an open API spec as input, an api.yaml file: https://github.com/oapi-codegen/oapi-codegen?tab=readme-ov-file#generating-api-clients

I am not finding any YAML files in https://github.com/inaturalist/iNaturalistAPI/tree/main/openapi ; instead there are Joi specifications. I found some tools online for converting Joi specs to openapi specs, but before I try running those, is there an existing solution to this?

Thanks!
S

1 Like

The OpenAPI 3.0 spec for v2 is dynamically generated. It can be found at https://api.inaturalist.org/v2/api-docs . Now that I’m looking at it I see one validation error I’ll fix - the include_ancestors parameter is listed as a path parameter but it should be a query parameter. The v1 spec (OpenAPI 2.0/Swagger) is at https://api.inaturalist.org/v1/swagger.json if you’d rather use that. Both are JSON but easily converted to YAML if you need that specifically

3 Likes

Thanks! This is exactly what I needed.

Generating the Go client code using oapi-codegen fails with a spec validation error:

% go generate
error generating code: error creating operation definitions: path '/observations/species_counts' has 0 positional parameters, but spec has 1 declared
exit status 1
generate.go:3: running "go": exit status 1

Opening the iNaturalist open API spec on https://editor.swagger.io/ shows two validation errors:

Structural error at paths./observations/species_counts.get.parameters.97
should have required property 'required'
missingProperty: required
Jump to line 11449

Semantic error at paths./observations/species_counts.get.parameters.97.name
Path parameter "include_ancestors" must have the corresponding {include_ancestors} segment in the "/observations/species_counts" path
Jump to line 11454

I’ve included a screenshot of the latter.

I’ve documented the steps I’ve taken here: https://github.com/Sajmani/inat?tab=readme-ov-file#inat

Any insights into how I can resolve these errors?

Thanks!
S

I was able to use Gemini CLI to identify and fix the error in my copy of api.json:

On line 16117 in the pretty printed api.json from https://api.inaturalist.org/v2/api-docs, “in”: “path” should be “in”: “query” for the “include_ancestors” parameter for the “/observations/species_counts” path.

I’m not sure how to fix this in the upstream server, but this was enough to enable me to generate the Go client code, so resolving this thread.

1 Like

This change has been applied. It appears the v2 spec is validating properly now. Thanks!

2 Likes