Skip to content

Commit 3bd27ce

Browse files
MaxFreedomPollardbnasslahsen
authored andcommitted
fix: read @ResponseStatus from the controller class
A @ResponseStatus on a controller class applies to every handler method that controller declares, and Spring returns that status at runtime. Operations on such a controller were documented as 200 instead. GenericResponseService.evaluateResponseStatus takes the method and the handler bean type, and falls back to the bean type when the method itself carries no @ResponseStatus, the same order Spring's own HandlerMethod uses. All four call sites passed methodParameter.getMethod().getClass(), which is java.lang.reflect.Method rather than the controller, so that fallback could never match. They now pass methodParameter.getContainingClass(), which is the handler bean type for a MethodParameter taken from a HandlerMethod and the declaring class otherwise. The guard in buildApiResponses that appends the @ResponseStatus code next to explicit @apiresponse entries looks at the controller class as well. Signed-off-by: Badr NASS LAHSEN <[email protected]>
1 parent 354c98c commit 3bd27ce

8 files changed

Lines changed: 267 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- The MCP dashboard no longer pre-fills the OAuth2 token endpoint, client id and client secret. The form shows hints instead, and warns when the token endpoint is not HTTPS
1313

14+
### Fixed
15+
16+
- #3352 – A `@ResponseStatus` on a controller class is now applied to the responses of its operations
17+
1418
## [3.1.1] - 2026-09-06
1519

1620
### Security

‎springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/GenericResponseService.java‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
/**
102102
* The type Generic response builder.
103103
*
104-
* @author bnasslahsen
104+
* @author bnasslahsen, Max Freedom Pollard
105105
*/
106106
public class GenericResponseService implements ApplicationContextAware {
107107

@@ -450,7 +450,7 @@ private void buildGenericApiResponses(Components components, MethodParameter met
450450
else {
451451
// Use response parameters with no description filled - No documentation
452452
// available
453-
String httpCode = evaluateResponseStatus(methodParameter.getMethod(), Objects.requireNonNull(methodParameter.getMethod()).getClass(), true);
453+
String httpCode = evaluateResponseStatus(methodParameter.getMethod(), methodParameter.getContainingClass(), true);
454454
if (Objects.nonNull(httpCode)) {
455455
apiResponse = methodAttributes.getGenericMapResponse().containsKey(httpCode) ? methodAttributes.getGenericMapResponse().get(httpCode)
456456
: new ApiResponse();
@@ -491,16 +491,17 @@ private void buildApiResponses(Components components, MethodParameter methodPara
491491
buildApiResponses(components, methodParameter, apiResponsesOp, methodAttributes, httpCode, apiResponse, false);
492492
}
493493
}
494-
if (AnnotatedElementUtils.hasAnnotation(methodParameter.getMethod(), ResponseStatus.class)) {
494+
if (AnnotatedElementUtils.hasAnnotation(methodParameter.getMethod(), ResponseStatus.class)
495+
|| AnnotatedElementUtils.hasAnnotation(methodParameter.getContainingClass(), ResponseStatus.class)) {
495496
// Handles the case with @ResponseStatus, if the specified response is not already handled explicitly
496-
String httpCode = evaluateResponseStatus(methodParameter.getMethod(), Objects.requireNonNull(methodParameter.getMethod()).getClass(), false);
497+
String httpCode = evaluateResponseStatus(methodParameter.getMethod(), methodParameter.getContainingClass(), false);
497498
if (Objects.nonNull(httpCode) && !apiResponsesOp.containsKey(httpCode) && !apiResponsesOp.containsKey(ApiResponses.DEFAULT)) {
498499
buildApiResponses(components, methodParameter, apiResponsesOp, methodAttributes, httpCode, new ApiResponse(), false);
499500
}
500501
}
501502
}
502503
else {
503-
String httpCode = evaluateResponseStatus(methodParameter.getMethod(), Objects.requireNonNull(methodParameter.getMethod()).getClass(), false);
504+
String httpCode = evaluateResponseStatus(methodParameter.getMethod(), methodParameter.getContainingClass(), false);
504505
if (Objects.nonNull(httpCode))
505506
buildApiResponses(components, methodParameter, apiResponsesOp, methodAttributes, httpCode, new ApiResponse(), false);
506507
}
@@ -812,7 +813,7 @@ private boolean isValidHttpCode(String httpCode, MethodParameter methodParameter
812813
if (isHttpCodePresent(httpCode, responseSet))
813814
result = true;
814815
}
815-
if (httpCode.equals(evaluateResponseStatus(method, method.getClass(), false)))
816+
if (httpCode.equals(evaluateResponseStatus(method, methodParameter.getContainingClass(), false)))
816817
result = true;
817818
}
818819
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2019-2026 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package test.org.springdoc.api.v30.app273;
18+
19+
import org.springframework.http.HttpStatus;
20+
import org.springframework.web.bind.annotation.GetMapping;
21+
import org.springframework.web.bind.annotation.PostMapping;
22+
import org.springframework.web.bind.annotation.ResponseStatus;
23+
import org.springframework.web.bind.annotation.RestController;
24+
25+
/**
26+
* A controller that sets a status code for all of its handler methods, and one method that
27+
* sets its own.
28+
*
29+
* @author Max Freedom Pollard
30+
*/
31+
@RestController
32+
@ResponseStatus(HttpStatus.CREATED)
33+
public class HelloController {
34+
35+
@PostMapping("/hello")
36+
public String hello() {
37+
return null;
38+
}
39+
40+
@GetMapping("/bye")
41+
@ResponseStatus(HttpStatus.ACCEPTED)
42+
public String bye() {
43+
return null;
44+
}
45+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2019-2026 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package test.org.springdoc.api.v30.app273;
18+
19+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
20+
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
23+
/**
24+
* A @ResponseStatus on the controller class applies to every handler method it declares.
25+
*
26+
* @author Max Freedom Pollard
27+
*/
28+
public class SpringDocApp273Test extends AbstractSpringDocV30Test {
29+
30+
@SpringBootApplication
31+
static class SpringDocTestApp {
32+
}
33+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2019-2026 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package test.org.springdoc.api.v31.app273;
18+
19+
import org.springframework.http.HttpStatus;
20+
import org.springframework.web.bind.annotation.GetMapping;
21+
import org.springframework.web.bind.annotation.PostMapping;
22+
import org.springframework.web.bind.annotation.ResponseStatus;
23+
import org.springframework.web.bind.annotation.RestController;
24+
25+
/**
26+
* A controller that sets a status code for all of its handler methods, and one method that
27+
* sets its own.
28+
*
29+
* @author Max Freedom Pollard
30+
*/
31+
@RestController
32+
@ResponseStatus(HttpStatus.CREATED)
33+
public class HelloController {
34+
35+
@PostMapping("/hello")
36+
public String hello() {
37+
return null;
38+
}
39+
40+
@GetMapping("/bye")
41+
@ResponseStatus(HttpStatus.ACCEPTED)
42+
public String bye() {
43+
return null;
44+
}
45+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2019-2026 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package test.org.springdoc.api.v31.app273;
18+
19+
import test.org.springdoc.api.v31.AbstractSpringDocTest;
20+
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
23+
/**
24+
* A @ResponseStatus on the controller class applies to every handler method it declares.
25+
*
26+
* @author Max Freedom Pollard
27+
*/
28+
public class SpringDocApp273Test extends AbstractSpringDocTest {
29+
30+
@SpringBootApplication
31+
static class SpringDocTestApp {
32+
}
33+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"openapi" : "3.0.1",
3+
"info" : {
4+
"title" : "OpenAPI definition",
5+
"version" : "v0"
6+
},
7+
"servers" : [ {
8+
"url" : "http://localhost",
9+
"description" : "Generated server url"
10+
} ],
11+
"paths" : {
12+
"/hello" : {
13+
"post" : {
14+
"tags" : [ "hello-controller" ],
15+
"operationId" : "hello",
16+
"responses" : {
17+
"201" : {
18+
"description" : "Created",
19+
"content" : {
20+
"*/*" : {
21+
"schema" : {
22+
"type" : "string"
23+
}
24+
}
25+
}
26+
}
27+
}
28+
}
29+
},
30+
"/bye" : {
31+
"get" : {
32+
"tags" : [ "hello-controller" ],
33+
"operationId" : "bye",
34+
"responses" : {
35+
"202" : {
36+
"description" : "Accepted",
37+
"content" : {
38+
"*/*" : {
39+
"schema" : {
40+
"type" : "string"
41+
}
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
},
49+
"components" : { }
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"openapi" : "3.1.0",
3+
"info" : {
4+
"title" : "OpenAPI definition",
5+
"version" : "v0"
6+
},
7+
"servers" : [ {
8+
"url" : "http://localhost",
9+
"description" : "Generated server url"
10+
} ],
11+
"paths" : {
12+
"/hello" : {
13+
"post" : {
14+
"tags" : [ "hello-controller" ],
15+
"operationId" : "hello",
16+
"responses" : {
17+
"201" : {
18+
"description" : "Created",
19+
"content" : {
20+
"*/*" : {
21+
"schema" : {
22+
"type" : "string"
23+
}
24+
}
25+
}
26+
}
27+
}
28+
}
29+
},
30+
"/bye" : {
31+
"get" : {
32+
"tags" : [ "hello-controller" ],
33+
"operationId" : "bye",
34+
"responses" : {
35+
"202" : {
36+
"description" : "Accepted",
37+
"content" : {
38+
"*/*" : {
39+
"schema" : {
40+
"type" : "string"
41+
}
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
},
49+
"components" : { }
50+
}

0 commit comments

Comments
 (0)