oapi-codegen / oapi-codegen

Generate Go client and server boilerplate from OpenAPI 3 specifications
Apache License 2.0
5.6k stars 816 forks source link

oapi-codegen fails to generate a client for jup.ag (Jupiter Station) #1420

Closed shackra closed 6 months ago

shackra commented 6 months ago

When using the program with oapi-codegen -package jupiter -generate types,client $(PROJECT_DIR)/jupiter-station/swagger.yaml > $(PROJECT_DIR)/jupiter-station/jup-ag.gen.go I get the following error:

error loading swagger spec in /home/jorge/code/criptomonedas/robot-trader/jupiter-station/swagger.yaml: error unmarshaling JSON: while decoding JSON: json: cannot unmarshal array into Go struct field TBis.components of type stringmake: *** [Makefile:16: jupiter-generate-client] Error 1

the OpenAPI version of the file says 3.0.3, you can find it here: https://github.com/jup-ag/jupiter-quote-api-node/blob/main/swagger.yaml

jamietanna commented 6 months ago

Although oapi-codegen tries to handle most specs there are always gaps in what we can handle, so thanks for raising the issue!

Having a quick look at the spec when validated with https://github.com/daveshanley/vacuum I see:

$ vacuum lint swagger.yaml -de
Location            | Severity | Message                                                                            | Rule                  | Category   | Path
swagger.yaml:24:7   | error    | the 'get' operation at path '/quote' does not contain an operationId               | operation-operationId | Operations | $.paths./quote.get
swagger.yaml:47:7   | error    | the 'post' operation at path '/swap' does not contain an operationId               | operation-operationId | Operations | $.paths./swap.post
swagger.yaml:64:7   | error    | the 'post' operation at path '/swap-instructions' does not contain an operationId  | operation-operationId | Operations | $.paths./swap-instructions.post
swagger.yaml:81:7   | error    | the 'get' operation at path '/program-id-to-label' does not contain an operationId | operation-operationId | Operations | $.paths./program-id-to-label.get
swagger.yaml:94:7   | error    | the 'get' operation at path '/indexed-route-map' does not contain an operationId   | operation-operationId | Operations | $.paths./indexed-route-map.get
swagger.yaml:162:11 | error    | a `$ref` cannot be placed next to any other properties                             | no-$ref-siblings      | Schemas    | $.components.schemas.SwapMode
swagger.yaml:235:7  | error    | Schema: missing properties: '$ref'                                                 | oas3-schema           | Schemas    | $.components.schemas.SwapRequest
swagger.yaml:255:11 | error    | Schema: missing properties: '$ref'                                                 | oas3-schema           | Schemas    | $.components.schemas.SwapRequest.properties.computeUnitPrice...
swagger.yaml:256:17 | error    | Schema: expected string, but got array                                             | oas3-schema           | Schemas    | $.components.schemas.SwapRequest.properties.computeUnitPrice...
swagger.yaml:258:11 | error    | Schema: missing properties: '$ref'                                                 | oas3-schema           | Schemas    | $.components.schemas.SwapRequest.properties.prioritizationFe...
swagger.yaml:259:17 | error    | Schema: expected string, but got array                                             | oas3-schema           | Schemas    | $.components.schemas.SwapRequest.properties.prioritizationFe...
swagger.yaml:318:11 | error    | a `$ref` cannot be placed next to any other properties                             | no-$ref-siblings      | Schemas    | $.components.schemas.Instruction

I can say that the multiple types i.e. here are definitely not valid in OpenAPI 3.0.x.

It's probably best for you to manually fix the oas3-schema findings, and then re-run with oapi-codegen, after which hopefully it'll generate something valid.

If so, it's worth getting this raised upstream with https://github.com/jup-ag/jupiter-quote-api-node/

shackra commented 6 months ago

Yeah, I noticed that. I did this to workaround that:

hellomoon-generate-client:
    @ogen -target $(PROJECT_DIR)/hellomoon -package hellomoon -v -clean $(PROJECT_DIR)/hellomoon-open-api.yaml

hellomoon-download-openapi:
    @wget -O $(PROJECT_DIR)/hellomoon-openapi.json https://gist.githubusercontent.com/hankc97/7f35c95c296e2983a0ccc22130c0f515/raw/de0183887c7469a30caffab0df6305f851bc41fe/open-api.json
    @cat $(PROJECT_DIR)/hellomoon-openapi.json | yq '(.. | select(has("enum"))) |= (.enum |= unique)' -oy -P > $(PROJECT_DIR)/hellomoon-open-api.yaml
    @rm -f $(PROJECT_DIR)/hellomoon-openapi.json