openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] Null example values generated for enum properties

Open alexrjones opened this issue 1 year ago • 0 comments

Bug Report Checklist

  • [X] Have you provided a full/minimal spec to reproduce the issue?
  • [X] Have you validated the input using an OpenAPI validator (example)?
  • [X] Have you tested with the latest master to confirm the issue still exists?
  • [X] Have you searched for related issues/PRs?
  • [X] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

If a schema has a property that references an enum schema, the example will generate null values for the schema.

openapi-generator version

7.5.0

OpenAPI declaration file content or url

Input:

definitions:
  myresponse:
    type: object
    additionalProperties: false
    properties:
      x:
        $ref: "#/definitions/myenum"
  myenum:
    type: string
    enum:
      - A
      - B
      - C

Expected output:

components:
  schemas:
    myresponse:
      additionalProperties: false
      example:
        x: A
      properties:
        x:
          $ref: '#/components/schemas/myenum'
      type: object
    myenum:
      enum:
        - A
        - B
        - C
      type: string

Actual output:

components:
  schemas:
    myresponse:
      additionalProperties: false
      example:
        x: null
      properties:
        x:
          $ref: '#/components/schemas/myenum'
      type: object
    myenum:
      enum:
      - A
      - B
      - C
      type: string
Generation Details

openapi-generator-cli generate -i "<input-yaml>" -g openapi-yaml

Steps to reproduce
  1. Run the above generator command with the above input yaml
  2. Observe that the output yaml matches the actual output yaml shown above
Related issues/PRs

None that I could find

Suggest a fix

In ExampleGenerator.resolveModelToExample, the schema should be processed if it is a valid enum (i.e. it has an enum field and that field is a non-empty sequence). This check should be placed here:

https://github.com/OpenAPITools/openapi-generator/blob/e36172090e4ed7c0658ec181a8217f392068c909/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java#L380-L385

alexrjones avatar May 09 '24 04:05 alexrjones