oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Enum in return type shows up as N200[attribute name]

Open MaximilianGaedig opened this issue 1 year ago • 9 comments

Enum types in return types are not handled properly, they get generated as N200[attribute name with first letter uppercase] which is not defined in the struct

This results in an error during build

I would expect it being correctly handled as an enum but it would be nice to at least just handle that as a string/int so that some client can be generated from an api spec even if the enum type in the return type would not be supported

Workaround: use a string type in the openapi schema

MaximilianGaedig avatar Oct 11 '23 20:10 MaximilianGaedig

Can you share a minimal example spec to confirm the issue?

jamietanna avatar Nov 01 '23 22:11 jamietanna

This is a public spec that also shows the issue when generating code: https://api.kombo.dev/openapi.json

ernest-ag5 avatar Dec 19 '23 12:12 ernest-ag5

Hi @jamietanna I'm having this same issue

This is a valid minimal example spec that demos the issue:

openapi: 3.0.0
info:
  version: 1.0.0
  title: My API
paths:
  /test:
    get:
      operationId: test
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  test:
                    type: string
                    enum:
                      - test1
                      - test2

johnhumphrys-catapult avatar Jan 12 '24 04:01 johnhumphrys-catapult

+1 -- same issue

davidh-cyberark avatar Feb 02 '24 03:02 davidh-cyberark

I found a work around -- move the enum to the components/schema, and then reference it in the reponses section.

Example:

                "responses": {
                    "200": {
                        "description": "blah blah blah",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "array",
                                    "items": {
                                        "type": "object",
                                        "properties": {
                                            "source": {
                                                "allOf": [
                                                    {
                                                        "$ref": "#/components/schemas/SourceEnum"
                                                    }
                                                ],
                                                "example": "foo or bar or baaz"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    },

...and then later in the compontents/schema:

       "SourceEnum": {
                "enum": [
                    "foo",
                    "bar",
                    "baaz",
                    "unknown"
                ],
                "type": "string"
            },

davidh-cyberark avatar Feb 05 '24 16:02 davidh-cyberark

I am having the same issue while generating a client, any solution?

oapi-codegen --version
github.com/deepmap/oapi-codegen/cmd/oapi-codegen
v1.16.2

szaher avatar Mar 13 '24 00:03 szaher

+1

akkuman avatar Mar 26 '24 05:03 akkuman

+1

S4more avatar Apr 16 '24 14:04 S4more

The solution I proposed above, (create the enum in the components, and then reference the component) is the way to go. The openapi3 spec does not define using enum's for use inside the "responses," whereas defining the enum in the components and referencing the component inside the "responses" seems to work.

davidh-cyberark avatar Apr 16 '24 19:04 davidh-cyberark

Hello, is there any workaround without altering OpenAPI schema? I use external 3rd party spec and prefer to avoid doing some jq magic with it.

nikicat avatar Jul 08 '24 11:07 nikicat

I am also seeing this issue with the Supabase Auth OpenAPI spec: https://github.com/supabase/auth/blob/master/openapi.yaml

Here is the problematic definition: https://github.com/supabase/auth/blob/33caaa9893f9343ac175d1c38742ba2e91f4dfc0/openapi.yaml#L712-L715 And here is the problematic code gen: image

robertkozin avatar Jul 08 '24 13:07 robertkozin