Swagger-codegen: [java spring] Doesn't support different response models

Created on 2 Feb 2017  路  7Comments  路  Source: swagger-api/swagger-codegen

Description

If you have a schema that has multiple responses with different models, the generated controller code only supports the first model.

eg. using the simple pet example (and changing the 'default' response to '400'):

      responses:
        '200':
          description: pet response
          schema:
            type: array
            items:
              $ref: '#/definitions/pet'
        '400':
          description: unexpected error
          schema:
            $ref: '#/definitions/errorModel'

Generates the following interface:

@ApiOperation(value = "", notes = "Returns all pets from the system that the user has access to", response = Pet.class, responseContainer = "List", tags={  })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "pet response", response = Pet.class),
        @ApiResponse(code = 400, message = "unexpected error", response = Pet.class) })
    @RequestMapping(value = "/pets",
        produces = { "application/json", "application/xml", "text/xml", "text/html" }, 
        consumes = { "application/json" },
        method = RequestMethod.GET)
    ResponseEntity<List<Pet>> findPets(@ApiParam(value = "tags to filter by") @RequestParam(value = "tags", required = false) List<String> tags,
        @ApiParam(value = "maximum number of results to return") @RequestParam(value = "limit", required = false) Integer limit);

Note that the error model isnt referenced anywhere in the generated code, and the response is set to ResponseEntity<List<Pet>> rather than the more generic Reponse class.

Swagger-codegen version

Latest head. Also the current version on http://editor.swagger.io/#/

Command line used for generation

generate -v -i ./swagger.yaml -l spring -o .

Feature Java help wanted

All 7 comments

This is a bug in all Java languages, I created #4718 for this to fix it in every languages.
The fix is to use the baseType instead of the returnType for the response. Additionally, you also need a fix for checking for void baseTypes which I added in PR #4717 to AbstractJavaJAXRSServerCodegen.java.

So it is best to wait for #4717 to get merged, then we can fix it in Spring and in all other languages.

any updates?

@jfiala Just for understanding, you want to change the return type to be just Response instead of Response<List<Pet>> in this case? I'm not sure I approve raw type usage, though I suppose Java doesn't allow anything type-safe here. (Apart from throwing all the non-first responses as exceptions.)

The alternative here is to require uses to implement a controller advice class that correctly maps exceptions thrown in the controllers to the correct object.

https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

The generation of the @ApiResponse annotations should be fixed though.

@ApiResponses(value = { 
        @ApiResponse(code = 200, message = "pet response", response = Pet.class),
        @ApiResponse(code = 400, message = "unexpected error", response = Pet.class) }

I just tried version 2.3.1 of the codegen and this issue is still there. Is there an ETA for fixing? If not, is there an alternative swagger pattern for error responses that does generate api classes in spring that can be used directly without making fixes by hand?

Hi, I'm also struggling with this issue. What is the work around?

My generated ..Api.java has the response well defined.

    @ApiResponses(value = { 
        @ApiResponse(code = 201, message = "Created", response = TheObject.class),
        @ApiResponse(code = 400, message = "Bad Request", response = BadRequest.class) })
    @RequestMapping(value = "/v1/objects",
        produces = { "application/json" }, 
        consumes = { "application/json" },
        method = RequestMethod.POST)
    ResponseEntity<TheObject> postObjects(@ApiParam(value = ""  )  @Valid @RequestBody TheObject body

Is there any work around? I have changed the return to:

ResponseEntity<? extends java.lang.Object> postObjects(@ApiParam(value = ""  )  @Valid @RequestBody TheObject body

Which is hard to check on git any changes made to the openapi spec.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings