OpenAPITools / OpenAPITools/openapi-generator

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

Open
#4,300 9 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: Spring
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Use the supplied OpenAPI YAML declaration to reproduce the missing example with both the openapi and spring generators, then inspect the shared example-generation path. Compare the generated Specification.example and HelloApi::helloGet() output with the expected values for both the inline and $ref enum properties; done means both generators include FOO_A and BAR_A.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.