Skip to content

Commit 6afecf3

Browse files
committed
When getting ExceptionHandler in the controller, use target class in case of AOP Proxy. Fixes #2098
1 parent 4cf2725 commit 6afecf3

File tree

6 files changed

+200
-2
lines changed

6 files changed

+200
-2
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* *
44
* * *
5-
* * * * Copyright 2019-2022 the original author or authors.
5+
* * * * Copyright 2019-2023 the original author or authors.
66
* * * *
77
* * * * Licensed under the Apache License, Version 2.0 (the "License");
88
* * * * you may not use this file except in compliance with the License.
@@ -668,7 +668,12 @@ else if (returnType instanceof ParameterizedType) {
668668
*/
669669
private synchronized Map<String, ApiResponse> getGenericMapResponse(Class<?> beanType) {
670670
List<ControllerAdviceInfo> controllerAdviceInfosInThisBean = localExceptionHandlers.stream()
671-
.filter(controllerInfo -> beanType.equals(controllerInfo.getControllerAdvice().getClass()))
671+
.filter(controllerInfo -> {
672+
Class<?> objClz = controllerInfo.getControllerAdvice().getClass();
673+
if (org.springframework.aop.support.AopUtils.isAopProxy(controllerInfo.getControllerAdvice()))
674+
objClz = org.springframework.aop.support.AopUtils.getTargetClass(controllerInfo.getControllerAdvice());
675+
return beanType.equals(objClz);
676+
})
672677
.collect(Collectors.toList());
673678

674679
Map<String, ApiResponse> genericApiResponseMap = controllerAdviceInfosInThisBean.stream()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package test.org.springdoc.api.v30.app202;
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,25 @@
1+
package test.org.springdoc.api.v30.app202;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.validation.annotation.Validated;
5+
import org.springframework.web.bind.annotation.ExceptionHandler;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.ResponseStatus;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping("/example")
13+
@Validated
14+
public class ExampleController {
15+
@GetMapping("/")
16+
public void index() {
17+
throw new IllegalArgumentException();
18+
}
19+
20+
@ExceptionHandler(IllegalArgumentException.class)
21+
@ResponseStatus(HttpStatus.NOT_FOUND)
22+
public String customControllerException() {
23+
return "example";
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package test.org.springdoc.api.v30.app202;
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-2023 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.app202;
24+
25+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
26+
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
29+
public class SpringDocApp202Test extends AbstractSpringDocV30Test {
30+
31+
@SpringBootApplication
32+
static class SpringDocTestApp {}
33+
34+
}
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)