swagger-api/swagger-codegen

[Spring] Generator assigns 200 status code to default responses

Open

#5,675 建立於 2017年5月20日

在 GitHub 查看
 (6 留言) (4 反應) (0 負責人)HTML (12,701 star) (5,474 fork)batch import
Issue: BugServer: Springhelp wanted

描述

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.

貢獻者指南