Skip to content

Commit f2b50cf

Browse files
committed
Merge branch 'uc4w6c-fix_exceptionhandler_response_status'
2 parents cb7b0a3 + 245d39f commit f2b50cf

File tree

11 files changed

+338
-8
lines changed

11 files changed

+338
-8
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ public class GenericResponseService {
103103
*/
104104
private final PropertyResolverUtils propertyResolverUtils;
105105

106+
/**
107+
* The Controller infos.
108+
*/
109+
private final List<ControllerAdviceInfo> localExceptionHandlers = new ArrayList<>();
110+
106111
/**
107112
* The Controller advice infos.
108113
*/
@@ -234,7 +239,12 @@ public void buildGenericResponse(Components components, Map<String, Object> find
234239
}
235240
}
236241
synchronized (this) {
237-
controllerAdviceInfos.add(controllerAdviceInfo);
242+
if (AnnotatedElementUtils.hasAnnotation(objClz, ControllerAdvice.class)) {
243+
controllerAdviceInfos.add(controllerAdviceInfo);
244+
}
245+
else {
246+
localExceptionHandlers.add(controllerAdviceInfo);
247+
}
238248
}
239249
}
240250
}
@@ -636,25 +646,22 @@ else if (returnType instanceof ParameterizedType) {
636646
* @return the generic map response
637647
*/
638648
private synchronized Map<String, ApiResponse> getGenericMapResponse(Class<?> beanType) {
639-
List<ControllerAdviceInfo> controllerAdviceInfosInThisBean = controllerAdviceInfos.stream()
640-
.filter(controllerAdviceInfo ->
641-
new ControllerAdviceBean(controllerAdviceInfo.getControllerAdvice()).isApplicableToBeanType(beanType))
642-
.filter(controllerAdviceInfo -> beanType.equals(controllerAdviceInfo.getControllerAdvice().getClass()))
649+
List<ControllerAdviceInfo> controllerAdviceInfosInThisBean = localExceptionHandlers.stream()
650+
.filter(controllerInfo -> beanType.equals(controllerInfo.getControllerAdvice().getClass()))
643651
.collect(Collectors.toList());
644652

645653
Map<String, ApiResponse> genericApiResponseMap = controllerAdviceInfosInThisBean.stream()
646-
.map(ControllerAdviceInfo::getApiResponseMap)
654+
.map(ControllerAdviceInfo::getApiResponseMap)
647655
.collect(LinkedHashMap::new, Map::putAll, Map::putAll);
648656

649657
List<ControllerAdviceInfo> controllerAdviceInfosNotInThisBean = controllerAdviceInfos.stream()
650658
.filter(controllerAdviceInfo ->
651659
new ControllerAdviceBean(controllerAdviceInfo.getControllerAdvice()).isApplicableToBeanType(beanType))
652-
.filter(controllerAdviceInfo -> !beanType.equals(controllerAdviceInfo.getControllerAdvice().getClass()))
653660
.collect(Collectors.toList());
654661

655662
for (ControllerAdviceInfo controllerAdviceInfo : controllerAdviceInfosNotInThisBean) {
656663
controllerAdviceInfo.getApiResponseMap().forEach((key, apiResponse) -> {
657-
if(!genericApiResponseMap.containsKey(key))
664+
if (!genericApiResponseMap.containsKey(key))
658665
genericApiResponseMap.put(key, apiResponse);
659666
});
660667
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package test.org.springdoc.api.app35;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
8+
import lombok.Data;
9+
10+
@Entity
11+
public @Data
12+
class ChildProperty {
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.AUTO)
16+
private long id;
17+
18+
private String name;
19+
20+
public long getId() {
21+
return id;
22+
}
23+
24+
public void setId(long id) {
25+
this.id = id;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package test.org.springdoc.api.app35;
2+
3+
import io.swagger.v3.oas.annotations.Hidden;
4+
5+
import org.springframework.data.repository.PagingAndSortingRepository;
6+
7+
@Hidden
8+
public interface ChildPropertyRepository extends PagingAndSortingRepository<ChildProperty, Long> {
9+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package test.org.springdoc.api.app35;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
import javax.persistence.JoinColumn;
8+
import javax.persistence.ManyToOne;
9+
10+
import lombok.Data;
11+
12+
@Entity
13+
public @Data
14+
class Property {
15+
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.AUTO)
18+
private long id;
19+
20+
private String name;
21+
22+
@ManyToOne
23+
@JoinColumn(name = "child_property_id")
24+
private ChildProperty myChildPropertyName;
25+
26+
public long getId() {
27+
return id;
28+
}
29+
30+
public void setId(long id) {
31+
this.id = id;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
42+
public ChildProperty getMyChildPropertyName() {
43+
return myChildPropertyName;
44+
}
45+
46+
public void setMyChildPropertyName(ChildProperty myChildPropertyName) {
47+
this.myChildPropertyName = myChildPropertyName;
48+
}
49+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package test.org.springdoc.api.app35;
2+
3+
import org.springframework.data.repository.PagingAndSortingRepository;
4+
5+
public interface PropertyRepository extends PagingAndSortingRepository<Property, Long> {
6+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 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.app35;
24+
25+
import test.org.springdoc.api.AbstractSpringDocTest;
26+
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
29+
public class SpringDocApp35Test extends AbstractSpringDocTest {
30+
31+
@SpringBootApplication
32+
static class SpringDocTestApp {}
33+
}
Lines changed: 23 additions & 0 deletions
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+
}
Lines changed: 23 additions & 0 deletions
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+
}
Lines changed: 23 additions & 0 deletions
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+
}
Lines changed: 34 additions & 0 deletions
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)