oapi-codegen
oapi-codegen copied to clipboard
Enum in return type shows up as N200[attribute name]
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
Can you share a minimal example spec to confirm the issue?
This is a public spec that also shows the issue when generating code: https://api.kombo.dev/openapi.json
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
+1 -- same issue
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"
},
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
+1
+1
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.
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.
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: