OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA][Spring] Swagger UI Incorrectly Displays Response Bodies for No-Content Responses

Open
#17,527 0 comments 3 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

The core of the issue is that sometimes whenever a response is specified in the spec without a response body, a response body is still present and shown in the SwaggerUI.

For example, if we take a look at this one endpoint from OpenAPI spec:

get:
      summary: Broken
      operationId: broken
      tags:
        - dogs
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Dog'
        '204':
          description: No Content
        '400':
          description: Bad Request
...

We would expect in our Swagger UI 204 and 400 to not have any response bodies. However, they do.

Expected:
image

Actual:
image

Notice that 204 and 400 have a request body which was not specified

openapi-generator version

Have tried and reproduced in 7.0.0, 7.1.0, 7.2.0 and with the newest master as well. Occurs in every case

OpenAPI declaration file content or url

I have created a separate GitHub repo with an example and very detailed documentation of how this bug occurs and how to fix it. If you would like to see very detailed investigation of this issue, please take a look : )
Feel free to just git clone and run it to see the issue and how it is easily reproducible

Repository with reproduction example and very detailed documentation of the bug investigation process
Just the openapi.yaml spec file
Plugin configuration in pom.xml

Generation Details

I am using the Openapi generator maven plugin with spring generator. Latest version (7.2.0)
Generation configuration can be found in pom.xml referenced above.
for a generation with the plugin:

mvn clean compile

Can also be generated with openapi-generator-cli:

sudo openapi-generator-cli generate -g spring -i https://raw.githubusercontent.com/GlobeDaBoarder/openapi-generator-wrong-response-body-bug-demo/bug-showcase/src/main/resource
s/openapi.yaml -o out

or when using latest master build:

java -jar openapi-generator/modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g spring -i https://raw.githubusercontent.com
/GlobeDaBoarder/openapi-generator-wrong-response-body-bug-demo/bug-showcase/src/main/resources/openapi.yaml -o ./out

All produce the same result

Steps to reproduce

The fastest way would probably be to clone my repo:
  1. Clone repo:
git clone https://github.com/GlobeDaBoarder/openapi-generator-wrong-response-body-bug-demo.git
  1. Open in IDE
  2.  mvn clean install
    
  3. Go to localhost:8080 to observe the issue in SwaggerUI
  4. Check the generated API code
If you prefer generating from just the spec file:
sudo openapi-generator-cli generate -g spring -i https://raw.githubusercontent.com/GlobeDaBoarder/openapi-generator-wrong-response-body-bug-demo/bug-showcase/src/main/resource
s/openapi.yaml -o out

Related issues/PRs

Suggest a fix

I am working on creating a corresponding PR with the bugfix. It will be mentioned and referenced here soon

EDIT

Added a corresponding PR
#17528

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 reproduction repository's src/main/resources/openapi.yaml and pom.xml, then run the documented Maven generation steps to compare the 204 and 400 responses in Swagger UI with the generated Spring API code. Done means responses without specified bodies no longer display response bodies; PR #17528 already tracks the proposed fix.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.