swagger-api / swagger-api/swagger-codegen

[java spring] Doesn't support different response models

Open
#4,693 12 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature help wanted Server: Java
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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 .

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

Start with the provided swagger.yaml example and run the documented generate -v -i ./swagger.yaml -l spring -o . command. Compare the generated Spring controller annotations and return type for the 200 and 400 responses; the completed behavior should represent both Pet and errorModel rather than using Pet for every response.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.