swagger-api / swagger-api/swagger-codegen

Spring-MVC: Having different kind of responses (200 and errors)

Open
#1,877 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hi,

I'm having a problem with Spring MVC.

I'd like to have one type of response when it's a 200 and if the server triggers an error, then it can send a different type of response. In terms of JSON spec, it would look like this:

"responses": {
    "200": {
      "description": "Response",
      "schema": {
        "$ref": "#/definitions/createAdvisorResponse"
      }
    },
    "400": {
      "description": "Malformed request"
    },
    "422": {
      "description": "Unprocessable Entity",
      "schema": {
        "$ref": "#/definitions/response"
      }
    },
    "500": {
      "description": "Internal Server Error"
    }
  }

If I use something like this and I generate the Spring MVC server, then I will only see this kind of functions:

public ResponseEntity<CreateAdvisorResponse> addAdvisor( ...

Therefore, I cannot send a type Response in case of an error is triggered..

Do you know if there's a way to do this?? Maybe I'm doing something wrong..

Thanks in advance

P.S.: Swagger UI recognize the fact that the 200 response is different from the 422..

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 generated Spring MVC server method and the Swagger response definitions shown in the issue. Check how the generator represents the documented 200, 400, 422, and 500 responses, then determine whether distinct success and error response types can be represented; done means the generated API exposes the documented response shapes.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.