Skip to content

Add api response for the httpcode specified by response status #1783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

rudolfgrauberger
Copy link

@rudolfgrauberger rudolfgrauberger commented Aug 8, 2022

Describe the problem

If you specify the default controller endpoint HTTP status code via @ResponseStatus and then add the special/error cases via @ApiResponse, the http code you specified with @ResponseStatus will not be listed as a possible response.

To Reproduce

What version of spring-boot you are using?

1.6.9

What modules and versions of springdoc-openapi are you using?

springdoc-openapi-ui

What is the actual and the expected result using OpenAPI Description (yml or json)?

actual

{
  "openapi": "3.0.1",
  "info": { "title": "OpenAPI definition", "version": "v0" },
  "servers": [
    { "url": "http://localhost:8080", "description": "Generated server url" }
  ],
  "paths": {
    "/withResponseStatus": {
      "get": {
        "tags": ["hello-controller"],
        "description": "200 defined by @ResponseStatus",
        "operationId": "withResponseStatus",
        "responses": {
          "422": {
            "description": "Test",
            "content": {
              "application/json": { "schema": { "type": "string" } }
            }
          },
          "409": {
            "description": "Test 2",
            "content": {
              "application/json": { "schema": { "type": "string" } }
            }
          }
        }
      }
    }
  },
  "components": {}
}

expected

{
  "openapi": "3.0.1",
  "info": { "title": "OpenAPI definition", "version": "v0" },
  "servers": [
    { "url": "http://localhost:8080", "description": "Generated server url" }
  ],
  "paths": {
    "/withResponseStatus": {
      "get": {
        "tags": ["hello-controller"],
        "description": "200 defined by @ResponseStatus",
        "operationId": "withResponseStatus",
        "responses": {
          "422": {
            "description": "Test",
            "content": {
              "application/json": { "schema": { "type": "string" } }
            }
          },
          "409": {
            "description": "Test 2",
            "content": {
              "application/json": { "schema": { "type": "string" } }
            }
          },
          "200": {
            "description": "OK",
            "content": {
              "application/json": { "schema": { "type": "string" } }
            }
          }
        }
      }
    }
  },
  "components": {}
}

Provide with a sample code (HelloController) or Test that reproduces the problem

	@Operation(description = "200 defined by @ResponseStatus",
		responses = {
			@ApiResponse(responseCode = "422", description = "Test")
	})
	@ApiResponses({
			@ApiResponse(responseCode = "409", description = "Test 2")
	})
	@GetMapping(value = "/withResponseStatus", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseStatus(value = HttpStatus.OK)
	public ResponseEntity<String> withResponseStatus() {
		return ResponseEntity.ok("hello");
	}

Expected behavior

According to the documentation for @ResponseStatus this annotation specifies which code should be returned.

With this, in my opinion, the code specified in @ResponseStatus should also be specified as a possible response. Of course only if it is not overridden by an explicit @ApiResponse documentation.

@bnasslahsen bnasslahsen merged commit c13aa32 into springdoc:master Aug 9, 2022
@bnasslahsen
Copy link
Collaborator

Thanks @rudolfgrauberger for your contribution to the project.

bnasslahsen added a commit that referenced this pull request Aug 9, 2022
@rudolfgrauberger rudolfgrauberger deleted the add-api-response-for-the-httpcode-specified-by-responseStatus branch August 9, 2022 14:26
mpleine pushed a commit to mpleine/springdoc-openapi that referenced this pull request May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants