swagger-api/swagger-codegen

[Spring] Generator assigns 200 status code to default responses

Open

#5675 opened on May 20, 2017

View on GitHub
 (6 comments) (4 reactions) (0 assignees)HTML (12,701 stars) (5,474 forks)batch import
Issue: BugServer: Springhelp wanted

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