Skip to content

Commit 0e65753

Browse files
committed
ExceptionHandler in controller is not used by another controller. Fixes #1909
1 parent 89bc6e8 commit 0e65753

File tree

6 files changed

+205
-6
lines changed

6 files changed

+205
-6
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ public class GenericResponseService {
117117
*/
118118
private final List<ControllerAdviceInfo> controllerAdviceInfos = new ArrayList<>();
119119

120+
/**
121+
* The Controller infos.
122+
*/
123+
private final List<ControllerAdviceInfo> localExceptionHandlers = new ArrayList<>();
124+
120125
/**
121126
* The Response entity exception handler class.
122127
*/
@@ -243,7 +248,12 @@ public void buildGenericResponse(Components components, Map<String, Object> find
243248
}
244249
}
245250
synchronized (this) {
246-
controllerAdviceInfos.add(controllerAdviceInfo);
251+
if (AnnotatedElementUtils.hasAnnotation(objClz, ControllerAdvice.class)) {
252+
controllerAdviceInfos.add(controllerAdviceInfo);
253+
}
254+
else {
255+
localExceptionHandlers.add(controllerAdviceInfo);
256+
}
247257
}
248258
}
249259
}
@@ -643,11 +653,9 @@ else if (returnType instanceof ParameterizedType) {
643653
* @return the generic map response
644654
*/
645655
private synchronized Map<String, ApiResponse> getGenericMapResponse(Class<?> beanType) {
646-
List<ControllerAdviceInfo> controllerAdviceInfosInThisBean = controllerAdviceInfos.stream()
647-
.filter(controllerAdviceInfo ->
648-
new ControllerAdviceBean(controllerAdviceInfo.getControllerAdvice()).isApplicableToBeanType(beanType))
649-
.filter(controllerAdviceInfo -> beanType.equals(controllerAdviceInfo.getControllerAdvice().getClass()))
650-
.toList();
656+
List<ControllerAdviceInfo> controllerAdviceInfosInThisBean = localExceptionHandlers.stream()
657+
.filter(controllerInfo -> beanType.equals(controllerInfo.getControllerAdvice().getClass()))
658+
.collect(Collectors.toList());
651659

652660
Map<String, ApiResponse> genericApiResponseMap = controllerAdviceInfosInThisBean.stream()
653661
.map(ControllerAdviceInfo::getApiResponseMap)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package test.org.springdoc.api.v30.app197;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ExceptionHandler;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.ResponseStatus;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
@RequestMapping("/example2")
12+
public class Example2Controller {
13+
@GetMapping("/")
14+
public void index() {
15+
throw new IllegalArgumentException();
16+
}
17+
18+
@ExceptionHandler(IllegalArgumentException.class)
19+
@ResponseStatus(HttpStatus.BAD_REQUEST)
20+
public String customControllerException() {
21+
return "example";
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package test.org.springdoc.api.v30.app197;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ExceptionHandler;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.ResponseStatus;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
@RequestMapping("/example")
12+
public class ExampleController {
13+
@GetMapping("/")
14+
public void index() {
15+
throw new IllegalArgumentException();
16+
}
17+
18+
@ExceptionHandler(IllegalArgumentException.class)
19+
@ResponseStatus(HttpStatus.NOT_FOUND)
20+
public String customControllerException() {
21+
return "example";
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package test.org.springdoc.api.v30.app197;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.web.HttpRequestMethodNotSupportedException;
8+
import org.springframework.web.bind.annotation.ControllerAdvice;
9+
import org.springframework.web.bind.annotation.ExceptionHandler;
10+
import org.springframework.web.bind.annotation.ResponseBody;
11+
import org.springframework.web.bind.annotation.ResponseStatus;
12+
13+
@ControllerAdvice
14+
public class MyExceptionHandler {
15+
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
16+
@ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
17+
@ResponseBody
18+
public Map<String, Object> handleError() {
19+
Map<String, Object> errorMap = new HashMap<String, Object>();
20+
errorMap.put("message", "error");
21+
return errorMap;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2022 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*/
22+
23+
package test.org.springdoc.api.v30.app197;
24+
25+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
26+
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
29+
public class SpringDocApp197Test extends AbstractSpringDocV30Test {
30+
31+
@SpringBootApplication
32+
static class SpringDocTestApp {}
33+
34+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/example2/": {
15+
"get": {
16+
"tags": [
17+
"example-2-controller"
18+
],
19+
"operationId": "index",
20+
"responses": {
21+
"200": {
22+
"description": "OK"
23+
},
24+
"400": {
25+
"description": "Bad Request",
26+
"content": {
27+
"*/*": {
28+
"schema": {
29+
"type": "string"
30+
}
31+
}
32+
}
33+
},
34+
"500": {
35+
"description": "Internal Server Error",
36+
"content": {
37+
"*/*": {
38+
"schema": {
39+
"type": "object",
40+
"additionalProperties": {
41+
"type": "object"
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
},
50+
"/example/": {
51+
"get": {
52+
"tags": [
53+
"example-controller"
54+
],
55+
"operationId": "index_1",
56+
"responses": {
57+
"200": {
58+
"description": "OK"
59+
},
60+
"404": {
61+
"description": "Not Found",
62+
"content": {
63+
"*/*": {
64+
"schema": {
65+
"type": "string"
66+
}
67+
}
68+
}
69+
},
70+
"500": {
71+
"description": "Internal Server Error",
72+
"content": {
73+
"*/*": {
74+
"schema": {
75+
"type": "object",
76+
"additionalProperties": {
77+
"type": "object"
78+
}
79+
}
80+
}
81+
}
82+
}
83+
}
84+
}
85+
}
86+
},
87+
"components": {}
88+
}

0 commit comments

Comments
 (0)