OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] Spring stub response missing required enum fields in execute

Open
#15,691 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

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

Using openapi-generator-cli to generate a Spring stub from an openapi v3.0 yaml file. The stub identifies correctly all fields of the answer in the autogenerated examples, however the "try it out / execute" will omit enum fields if they are defined with a $ref. Non-enum fields and enums directly written in schema will be returned.

From the example below, the stub correctly identifies the expected fields in the examples section:

{
  "data": {
    "requests": [
      {
        "non_enum_string_ref": "string",
        "non_enum_string": "string",
        "enum_direct": "ONE",
        "enum_ref": "ALPHA",
        "status": "SCHEDULED",
        "creationDate": "2023-05-30T12:20:29.895Z"
      }
    ]
  }
}

However, "Try it out / execute" feature will omit enum_ref and status fields, even though they are marked as "required":

{
  "data": {
    "requests": [
      {
        "enum_direct": "ONE",
        "non_enum_string_ref": "non_enum_string_ref",
        "creationDate": "2000-01-23T04:56:07.000+00:00",
        "non_enum_string": "non_enum_string"
      },
      {
        "enum_direct": "ONE",
        "non_enum_string_ref": "non_enum_string_ref",
        "creationDate": "2000-01-23T04:56:07.000+00:00",
        "non_enum_string": "non_enum_string"
      }
    ]
  }
}

The bug disappear when setting an example value in the enum field.

openapi-generator version

Tested on 6.1.0, 6.6.0, and latest snapshot (7.0.0-SNAPSHOT from 2023/05/27), all versions have the same behaviour

OpenAPI declaration file content or url

Enum.yaml file:

StatusEnum:
  description: Enumeration type defined in another file, to be used with $ref
  type: string
  enum:
    - SCHEDULED
    - ON_GOING
    - COMPLETED

ExternalId:
  type: string
  description: Example of string defined with $ref, without enumeration

OtherEnum:
  description: Another example of enumeration defined in another file
  type: string
  enum:
    - ALPHA
    - BRAVO
    - CHARLIE

rest-api.yaml file:

openapi: 3.0.0
info:
  title: Test API
  description: Test API
  version: ${VERSION}
servers:
  - url: https://test.api.com
    description: Test API

paths: 
  /example/requests-monitoring:
    get:
      summary: getRequests
      description: |
        This endpoint provides followup informations on client requests beeing processed by the system.  
      parameters:
        - in: query
          name: status
          description: Filter on request status.
          schema:
            $ref: './Enums.yaml#/StatusEnum'
      responses:
        '200':
          description: Success
          content:
            application/json; charset=UTF-8:
              schema:
                type: object
                required:
                  - data
                properties:
                  data:
                    type: object
                    required:
                      - requests
                    properties:
                      requests:
                        type: array
                        items:
                          description: object description
                          type: object
                          required:
                            - non_enum_string_ref
                            - non_enum_string
                            - enum_direct
                            - enum_ref
                            - status
                            - creationDate
                          properties:
                            non_enum_string_ref:
                              description: Client request internal unique identifier in the system
                              $ref: './Enums.yaml#/ExternalId'
                            non_enum_string:
                              description: a string directly written without $ref
                              type: string
                            enum_direct:
                              description: an enum directly written here
                              type: string
                              enum:
                                - ONE
                                - TWO
                                - THREE
                            enum_ref:
                              description: An enum written in another file and referenced with $ref
                              $ref: './Enums.yaml#/OtherEnum'
                            status:
                              description: The actual value of status
                              $ref: './Enums.yaml#/StatusEnum'
                            creationDate:
                              description: The request creation date
                              type: string
                              format: date-time
Generation Details

Language: Java
Java version: openjdk 17.0.7 2023-04-18

Steps to reproduce
  • Create yaml files with above content
  • Generate spring stub with following command line: java -jar openapi-generator-cli-7.0.0-SNAPSHOT.jar generate -g spring -i rest-api.yaml -o stub-spring -p generateAliasAsModel=false -p returnSuccessCode=true -p artifactId=spring_stub -p artifactVersion="0.0.1"
  • Either launch stub directly (class OpenApiGeneratorApplication.java) or create a jar file and execute it (mvn clean package -DskipTests then java -jar stub-spring/target/spring_stub-0.0.1.jar)
  • Open a browser to localhost:8080
  • Go to endpoint GET /example/requests-monitoring: autogenerated examples have all 6 required fields non_enum_string_ref, non_enum_string, enum_direct, enum_ref, status, creationDate
  • Click Try it out then execute: only 4 fields are returned in each item of the array, enum_ref and status are missing
Related issues/PRs

None identified

Suggest a fix

N/A

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

Start with the provided rest-api.yaml and Enums.yaml, generate and run the Spring stub through OpenApiGeneratorApplication.java, then compare the autogenerated example with the Try it out response. Trace how referenced enum properties are handled during response generation. Done means required enum_ref and status fields are included when their schemas use $ref.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.