Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Example values are not generated for enum types referenced with $ref #4300

Open
tkalliom opened this issue Oct 28, 2019 · 9 comments
Open

Comments

@tkalliom
Copy link

Description

openapi-generator only produces example schemas/responses for enum types when they are inlined in the response type. When an enum is $referenced, 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"} } and
  • setExampleResponse(request, "application/json", "{ \"bar\" : \"BAR_A\"}") for Spring rather than the expected setExampleResponse(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 enums; in:title enum example returned 0 matches.

@auto-labeler
Copy link

auto-labeler bot commented Oct 28, 2019

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@lwlee2608
Copy link
Contributor

I am able to resolve it by replacing this line

from return null; to return resolvePropertyToExample(name, mediaType, schema, processedModels);

However, I'm not sure if it is a correct fix.

@gg-math
Copy link

gg-math commented May 12, 2020

This would be a nice improvement 👍

@KopiasCsaba
Copy link

KopiasCsaba commented Jul 12, 2021

I've just ran into this with 5.0.0 and 5.2.0 too using php-server.

One picture worth a thousand words:

image

I confirm that inlining the $ref works around the problem.
image
Also there is data loss in the first scenario, as you can see that the generated SubjectType contains nothing at all.
We just ran into this after thinking that we have implemented an api successfully. Just now we found out that some fields remain empty even though there is data in the request for that fields.

It would be nice to have this fixed:)

Thanks for openapi-generator!

@KopiasCsaba
Copy link

It seems that this might only happen with scalar values, I've just found a $ref-ed "type object" that works.

@giantcow
Copy link

giantcow commented Nov 8, 2021

I believe this is happening with the GraphQL generator as well. Please note that I'm using a smithy generator to create a OpenAPI definition which I'm converting to the GQL schema.

Input OpenAPI Schema:

...
    "components": {
        "schemas": {
            "AccessType": {
                "type": "string",
                "minLength": 1,
                "enum": [
                    "private",
                    "public"
                ]
            },
            "CreateScheduleRequestContent": {
                "type": "object",
                "properties": {
                    "scheduleData": {
                        "$ref": "#/components/schemas/ScheduleData"
                    },
                    "access": {
                        "$ref": "#/components/schemas/AccessType"
                    }
                },
                "required": [
                    "access",
                    "scheduleData"
                ]
            },
            "CreateScheduleResponseContent": {
                "type": "object",
                "properties": {
                    "scheduleId": {
                        "type": "string",
                        "pattern": "^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$"
                    },
                    "scheduleState": {
                        "$ref": "#/components/schemas/ScheduleState"
                    }
                },
                "required": [
                    "scheduleId",
                    "scheduleState"
                ]
            },
            "GetScheduleResponseContent": {
                "type": "object",
                "properties": {
                    "scheduleId": {
                        "type": "string",
                        "pattern": "^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$"
                    },
                    "scheduleData": {
                        "$ref": "#/components/schemas/ScheduleData"
                    },
                    "scheduleState": {
                        "$ref": "#/components/schemas/ScheduleState"
                    }
                },
                "required": [
                    "scheduleData",
                    "scheduleId",
                    "scheduleState"
                ]
            },
            "ScheduleData": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string"
                    }
                }
            },
            "ScheduleState": {
                "type": "string",
                "enum": [
                    "active",
                    "depricated"
                ]
            }
        }
    }
...

GQL output:

# model/access_type.graphql
type AccessType {

}

# model/schedule_state.graphql
type ScheduleState {

}

# model/schedule_data.graphql
type ScheduleData {

  name: String!

}

@Johnlon
Copy link

Johnlon commented Feb 2, 2022

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?

  • Look for a "example" field on the request paramter and use that?
    Or drill down the to the referred enum type and ...
  • Pick any one of the valid enum values and use it as the value in the test code?
  • or look for a "example" field in the enum and use that?
  • or look for a "default" field in the enum and use that?

(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.

        body = 'body_example'
        query_string = [('enum_query_int', openapi_server.OuterEnumInteger())]
        headers = {
            'Accept': '*/*',
 /enum-post-json:
    post:
      description: Test serialization of enum properties with examples
      operationId: enum_post_json
      responses:
        '200':
          description: Output enum corncopia
          content:
            '*/*':
              schema:
                type: string
      parameters:
      - description: Query outer enum parameter
        in: query
        name: enum_query_int
        required: false
        # TODO Code gen doesn't pick this example up nor the one in the referred type unfortunately - and the generated controller test is not valid code
        # https://github.com/OpenAPITools/openapi-generator/issues/4300
        example: 1
        schema:
          $ref: '#/components/schemas/OuterEnumInteger'
      requestBody:
        required: true
        content:
          application/json:
            schema:
                type: string

What the param type is ...

    OuterEnumInteger:
      type: integer
      enum:
      - 0
      - 1
      - 2
      example: 2

@tkalliom
Copy link
Author

Update: seems that #5676 was not the cause for this after all, as the issue is still present in 6.4.0, which has #13880.

@jakub-nezasa
Copy link

jakub-nezasa commented Feb 12, 2024

Suffering from the same issue. Any updates now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants