swagger-api / swagger-api/swagger-codegen

[Spring] Generator assigns 200 status code to default responses

Open
#5,675 6 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

help wanted Issue: Bug Server: Spring
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

The Spring generator doesn't deal correctly with default responses. When setting a 200 response and a default response in the yaml file, the generator creates just 2 @ApiResponse objects, both containing code 200.

Swagger-codegen version

master and 2.3.0

Swagger declaration file content or url

Extract:

paths:
   ...
   get:
   ...
      responses:
        200:
          description: Successful response
          schema:
            $ref: "#/definitions/Product"
        default:
          description: Bad Request
          schema:
            $ref: "#/definitions/Error"
Command line used for generation

java -jar swagger-codegen-cli.jar generate -i swagger.yaml -l spring

Suggest a fix

The annotation @ApiResponse expects an integer that represents an HTTP status code.

If there were a way to set somewhere in the spec what are the status codes that we want to consider in our API, we could just generate one @ApiResponse for each of them. The one for 200 would have the successful response model, and the rest would have the default response model.

If that's not possible, I don't think we could or should add all the possible status codes as different @ApiResponse annotations, as there are too many valid HTTP status codes.

From my tests, it seems that only default is interpreted as 200. I wrote a different word as a response code (defaulr) and the generator didn't interpret it in any way. It simply wrote the word as if it were a valid code already, which resulted in a compilation error later.

Seeing as the generator knows already about default responses, we could use a different integer to represent them instead of 200. A value which is clearly an invalid HTTP status code could be suitable, e.g. -1. This solution could be discussed with people at SpringFox too, so they could be able to interpret -1 as the default status code and display information accordingly in the rendered docs.

Feel free to suggest any other alternatives to fix the problem. If the issue is present for other languages generators, I guess it could be more complicated than this.

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

Reproduce the issue with the supplied YAML and the swagger-codegen-cli.jar generate -l spring command, then trace the Spring generator's handling of the 200 and default responses. Compare the generated @ApiResponse annotations with the input and determine an agreed representation for the default response; done means the output no longer duplicates status 200 and remains valid for SpringFox.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.