Skip to content

Commit caae4c4

Browse files
committed
Merge branch 'eshishkin-exception-handlers'
2 parents 800bd8d + a33702f commit caae4c4

File tree

11 files changed

+288
-34
lines changed

11 files changed

+288
-34
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.springframework.core.annotation.AnnotatedElementUtils;
5353
import org.springframework.http.HttpStatus;
5454
import org.springframework.util.CollectionUtils;
55+
import org.springframework.util.ReflectionUtils;
5556
import org.springframework.web.bind.annotation.ExceptionHandler;
5657
import org.springframework.web.bind.annotation.RequestMapping;
5758
import org.springframework.web.bind.annotation.ResponseStatus;
@@ -152,7 +153,7 @@ public void buildGenericResponse(Components components, Map<String, Object> find
152153
if (org.springframework.aop.support.AopUtils.isAopProxy(controllerAdvice))
153154
objClz = org.springframework.aop.support.AopUtils.getTargetClass(controllerAdvice);
154155
ControllerAdviceInfo controllerAdviceInfo = new ControllerAdviceInfo(controllerAdvice);
155-
Arrays.stream(objClz.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(ExceptionHandler.class)).forEach(methods::add);
156+
Arrays.stream(ReflectionUtils.getAllDeclaredMethods(objClz)).filter(m -> m.isAnnotationPresent(ExceptionHandler.class)).forEach(methods::add);
156157
// for each one build ApiResponse and add it to existing responses
157158
for (Method method : methods) {
158159
if (!operationService.isHidden(method)) {

springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/AbstractCommonTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public abstract class AbstractCommonTest {
2323
@Autowired
2424
protected WebTestClient webTestClient;
2525

26-
protected String getContent(String fileName) throws Exception {
26+
protected String getContent(String fileName) {
2727
try {
2828
Path path = Paths.get(FileUtils.class.getClassLoader().getResource(fileName).toURI());
2929
byte[] fileBytes = Files.readAllBytes(path);
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package test.org.springdoc.api.app157;
22

3+
import java.util.ArrayList;
4+
35
import io.swagger.v3.core.converter.ModelConverters;
4-
import io.swagger.v3.core.util.Json;
5-
import org.junit.jupiter.api.AfterAll;
66
import org.junit.jupiter.api.AfterEach;
7-
import org.junit.jupiter.api.BeforeAll;
87
import org.junit.jupiter.api.BeforeEach;
98
import org.junit.jupiter.api.Test;
109
import org.springdoc.core.Constants;
11-
import org.springdoc.core.converters.ModelConverterRegistrar;
12-
import org.springframework.boot.autoconfigure.SpringBootApplication;
13-
import org.springframework.test.web.servlet.MvcResult;
1410
import test.org.springdoc.api.AbstractSpringDocTest;
1511

16-
import java.util.List;
12+
import org.springframework.boot.autoconfigure.SpringBootApplication;
1713

18-
import static org.hamcrest.Matchers.*;
14+
import static org.hamcrest.Matchers.hasProperty;
15+
import static org.hamcrest.Matchers.is;
16+
import static org.hamcrest.Matchers.not;
1917
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
2018
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
2119
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -27,28 +25,31 @@
2725
*/
2826
public class SpringDocApp157Test extends AbstractSpringDocTest {
2927

30-
@SpringBootApplication
31-
static class SpringBootApp {}
32-
33-
private StringyConverter myConverter = new StringyConverter();
34-
private ModelConverters converters = ModelConverters.getInstance();
35-
36-
@BeforeEach
37-
public void registerConverter() {
38-
converters.addConverter(myConverter);
39-
}
40-
41-
@AfterEach
42-
public void unregisterConverter() {
43-
converters.removeConverter(myConverter);
44-
}
45-
46-
@Test
47-
public void testApp() throws Exception {
48-
mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL))
49-
.andExpect(status().isOk())
50-
.andExpect(jsonPath("$.openapi", is("3.0.1")))
51-
.andExpect(jsonPath("$.components.schemas.Foo.required", is(List.of("stringy"))))
52-
.andExpect(jsonPath("$.components.schemas.Bar", not(hasProperty("required"))));
53-
}
28+
@SpringBootApplication
29+
static class SpringBootApp {}
30+
31+
private StringyConverter myConverter = new StringyConverter();
32+
33+
private ModelConverters converters = ModelConverters.getInstance();
34+
35+
@BeforeEach
36+
public void registerConverter() {
37+
converters.addConverter(myConverter);
38+
}
39+
40+
@AfterEach
41+
public void unregisterConverter() {
42+
converters.removeConverter(myConverter);
43+
}
44+
45+
@Test
46+
public void testApp() throws Exception {
47+
mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL))
48+
.andExpect(status().isOk())
49+
.andExpect(jsonPath("$.openapi", is("3.0.1")))
50+
.andExpect(jsonPath("$.components.schemas.Foo.required", is(new ArrayList<String>() {{
51+
add("stringy");
52+
}})))
53+
.andExpect(jsonPath("$.components.schemas.Bar", not(hasProperty("required"))));
54+
}
5455
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test.org.springdoc.api.app158;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ExceptionHandler;
5+
import org.springframework.web.bind.annotation.ResponseStatus;
6+
7+
public class CommonFooErrorHandler {
8+
9+
@ExceptionHandler
10+
@ResponseStatus(HttpStatus.CONFLICT)
11+
public ErrorDTO onException(Exception e) {
12+
return new ErrorDTO("Something wrong has happened");
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package test.org.springdoc.api.app158;
2+
3+
public class ErrorDTO {
4+
private String message;
5+
6+
public ErrorDTO() {
7+
}
8+
9+
public ErrorDTO(String message) {
10+
this.message = message;
11+
}
12+
13+
public String getMessage() {
14+
return message;
15+
}
16+
17+
public void setMessage(String message) {
18+
this.message = message;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app158;
20+
21+
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
22+
import io.swagger.v3.oas.annotations.info.Info;
23+
import io.swagger.v3.oas.annotations.tags.Tag;
24+
import org.springframework.web.bind.annotation.GetMapping;
25+
import org.springframework.web.bind.annotation.RequestMapping;
26+
import org.springframework.web.bind.annotation.RestController;
27+
28+
@RestController
29+
@RequestMapping("/api")
30+
@OpenAPIDefinition(info = @Info(title = "API Examples", version = "1.0"), tags = @Tag(name = "Operations"))
31+
public class HelloController {
32+
33+
@GetMapping("/foo")
34+
public SimpleDTO hello() {
35+
return new SimpleDTO("foo");
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test.org.springdoc.api.app158;
2+
3+
public class SimpleDTO {
4+
5+
private String payload;
6+
7+
public SimpleDTO() {
8+
}
9+
10+
public SimpleDTO(String payload) {
11+
this.payload = payload;
12+
}
13+
14+
public String getPayload() {
15+
return payload;
16+
}
17+
18+
public void setPayload(String payload) {
19+
this.payload = payload;
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package test.org.springdoc.api.app158;
2+
3+
import org.springframework.web.bind.annotation.ControllerAdvice;
4+
5+
@ControllerAdvice(assignableTypes = HelloController.class)
6+
public class SpecificFooErrorHandler extends CommonFooErrorHandler {
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package test.org.springdoc.api.app158;
2+
3+
import test.org.springdoc.api.AbstractSpringDocTest;
4+
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
public class SpringDocApp158Test extends AbstractSpringDocTest {
8+
9+
@SpringBootApplication
10+
static class SpringDocTestApp {}
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "API Examples",
5+
"version": "1.0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"tags": [
14+
{
15+
"name": "Operations"
16+
}
17+
],
18+
"paths": {
19+
"/api/foo": {
20+
"get": {
21+
"tags": [
22+
"hello-controller"
23+
],
24+
"operationId": "hello",
25+
"responses": {
26+
"409": {
27+
"description": "Conflict",
28+
"content": {
29+
"*/*": {
30+
"schema": {
31+
"$ref": "#/components/schemas/ErrorDTO"
32+
}
33+
}
34+
}
35+
},
36+
"200": {
37+
"description": "OK",
38+
"content": {
39+
"*/*": {
40+
"schema": {
41+
"$ref": "#/components/schemas/SimpleDTO"
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
},
50+
"components": {
51+
"schemas": {
52+
"ErrorDTO": {
53+
"type": "object",
54+
"properties": {
55+
"message": {
56+
"type": "string"
57+
}
58+
}
59+
},
60+
"SimpleDTO": {
61+
"type": "object",
62+
"properties": {
63+
"payload": {
64+
"type": "string"
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "API Examples",
5+
"version": "1.0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"tags": [
14+
{
15+
"name": "Operations"
16+
}
17+
],
18+
"paths": {
19+
"/api/foo": {
20+
"get": {
21+
"tags": [
22+
"hello-controller"
23+
],
24+
"operationId": "hello",
25+
"responses": {
26+
"409": {
27+
"description": "Conflict",
28+
"content": {
29+
"*/*": {
30+
"schema": {
31+
"$ref": "#/components/schemas/ErrorDTO"
32+
}
33+
}
34+
}
35+
},
36+
"200": {
37+
"description": "OK",
38+
"content": {
39+
"*/*": {
40+
"schema": {
41+
"$ref": "#/components/schemas/SimpleDTO"
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
},
50+
"components": {
51+
"schemas": {
52+
"ErrorDTO": {
53+
"type": "object",
54+
"properties": {
55+
"message": {
56+
"type": "string"
57+
}
58+
}
59+
},
60+
"SimpleDTO": {
61+
"type": "object",
62+
"properties": {
63+
"payload": {
64+
"type": "string"
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)