[BUG] Example values are not generated for enum types referenced with $ref
See original GitHub issueDescription
openapi-generator only produces example schemas/responses for enum
types when they are inlined in the response type. When an enum
is $ref
erenced, no example value is produced. Because this happens with both openapi
and spring
generators, I assume this is a core bug and not specific to either generator.
With the declaration file below, I get
"Specification" : { "example" : {"bar" : "BAR_A"} }
for openapi rather than the expected"Specification" : { "example" : {"foo" : "FOO_A", "bar" : "BAR_A"} }
andsetExampleResponse(request, "application/json", "{ \"bar\" : \"BAR_A\"}")
for Spring rather than the expectedsetExampleResponse(request, "application/json", "{ \"foo\": \"FOO_A\", \"bar\" : \"BAR_A\"}")
.
openapi-generator version
Problem seen at least in 4.0.3, 4.1.3, latest 4.2.0-SNAPSHOT and 5.0.0-SNAPSHOT.
OpenAPI declaration file content or url
openapi: "3.0.0"
info:
version: 0.0.1
title: Enum example
components:
schemas:
Foo:
type: string
enum:
- FOO_A
- FOO_B
Specification:
type: object
properties:
foo:
$ref: '#/components/schemas/Foo'
bar:
type: string
enum:
- BAR_A
- BAR_B
required:
- foo
- bar
paths:
/hello:
get:
responses:
200:
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/Specification'
Command line used for generation
java -jar openapi-generator-cli-4.2.0-20191028.135625-129.jar generate -i api.yaml -g openapi -o oa-420/
java -jar openapi-generator-cli-4.2.0-20191028.135625-129.jar generate -i api.yaml -g spring -o spring-420/
Steps to reproduce
Generate an Openapi JSON file or a Spring application with the above command lines and check $.components.schemas.Specification.example
and HelloApi::helloGet()
, respectively
Related issues/PRs
Could not find anything related to examples for enum
s; in:title enum example
returned 0 matches.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:7 (1 by maintainers)
I am able to resolve it by replacing this line https://github.com/OpenAPITools/openapi-generator/blob/a2b37d67c05a9e09a047d0c5d20652326fe3d2fa/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java#L354
from
return null;
toreturn resolvePropertyToExample(name, mediaType, schema, processedModels);
However, I’m not sure if it is a correct fix.
This is really bugging me as I want to rejuvenate the flask generator and fix a load of bugs and gaps in enum handling but this one probem has me stuck. I cannot figure out how to get the generated code to contain a legal example value or legal enum value of any kind. And I hate that.
Any way folk …
What’s the required behaviour here?
(PS @lwlee2608 that change above didn’t work for me)
The python code I get generated in the controller test is just wrong. The substituted example value “openapi_server.OuterEnumInteger()” can’t even compile. Even “openapi_server.models.OuterEnumInteger()” would be wrong as this is imported. Using just “OuterEnumInteger()” compiles at least. However, even then we get a runtime error because “OuterEnumInteger()” is not a valid value for the enum which is actually an int inmy case.