swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.03k stars 6.03k forks source link

[Spring] Generator assigns 200 status code to default responses #5675

Open ebautistabar opened 7 years ago

ebautistabar commented 7 years ago
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.

mendfred commented 4 years ago

Same problem here. Would be nice to get it fixed.

Found a duplicate bug report here https://github.com/swagger-api/swagger-codegen-generators/issues/360

wiliao commented 3 years ago

I saw this issue was still there, is there any plan to bring in the fix or solution?

arandth commented 1 year ago

Same question for me. It's now open for over three years....

asmita-tiwari commented 1 year ago

I am also getting similar issue. Do we have any alternative solution for this ?

Nikolas-Charalambidis commented 1 year ago

May 2017 and still open in August 2023?

GregoryEssertel commented 1 year ago

if someone want a stop gap solution by overriding the mustache templates:

create a new file: responseCode.mustache

{{#is1xx}}{{{code}}}{{/is1xx}}{{#is2xx}}{{{code}}}{{/is2xx}}{{#is3xx}}{{{code}}}{{/is3xx}}{{#is4xx}}{{{code}}}{{/is4xx}}{{#is5xx}}{{{code}}}{{/is5xx}}{{^is1xx}}{{^is2xx}}{{^is3xx}}{{^is4xx}}{{^is5xx}}default{{/is5xx}}{{/is4xx}}{{/is3xx}}{{/is2xx}}{{/is1xx}}

and then override the api.mustache, apiController.mustache, and apiDelegate.mustache replacing {{code}} and {{{code}}} by {{>responseCode}}

I may have missed some files where the code is used!