Skip to content

Commit ecc45df

Browse files
committed
merge done
2 parents 800bd8d + 5537e88 commit ecc45df

File tree

8 files changed

+241
-1
lines changed

8 files changed

+241
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
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)) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test.org.springdoc.api.app157;
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+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package test.org.springdoc.api.app157;
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+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.app157;
20+
21+
import org.springframework.http.HttpStatus;
22+
import org.springframework.http.ResponseEntity;
23+
import org.springframework.web.bind.annotation.GetMapping;
24+
import org.springframework.web.bind.annotation.RestController;
25+
26+
/**
27+
* Put Foo and Bar in the schema's components, make sure there is an ignored wrapper
28+
* ({@code ResponseEntity}).
29+
*/
30+
@RestController
31+
public class HelloController {
32+
33+
@GetMapping( "/foo")
34+
public ResponseEntity<Foo> getFoo() {
35+
return new ResponseEntity<Foo>(HttpStatus.OK);
36+
}
37+
38+
@GetMapping( "/bar")
39+
public ResponseEntity<Bar> getBar() {
40+
return new ResponseEntity<Bar>(HttpStatus.OK);
41+
}
42+
43+
44+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test.org.springdoc.api.app157;
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.app157;
2+
3+
import org.springframework.web.bind.annotation.ControllerAdvice;
4+
5+
@ControllerAdvice(assignableTypes = HelloController.class)
6+
public class SpecificFooErrorHandler extends CommonFooErrorHandler {
7+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package test.org.springdoc.api.app158;
2+
3+
import java.util.List;
4+
5+
import io.swagger.v3.core.converter.ModelConverters;
6+
import org.junit.jupiter.api.AfterEach;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.springdoc.core.Constants;
10+
import test.org.springdoc.api.AbstractSpringDocTest;
11+
12+
import org.springframework.boot.autoconfigure.SpringBootApplication;
13+
14+
import static org.hamcrest.Matchers.hasProperty;
15+
import static org.hamcrest.Matchers.is;
16+
import static org.hamcrest.Matchers.not;
17+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
18+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
19+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
20+
21+
/**
22+
* This test is to make sure that a new model converter can access the parent of a type, even if
23+
* the type is enclosed in an ignored wrapper. We test this by setting up a model converter which
24+
* adds "stringy" to the "required" property of a schema's parent, when the sub schema is a String.
25+
*/
26+
public class SpringDocApp157Test extends AbstractSpringDocTest {
27+
28+
@SpringBootApplication
29+
static class SpringBootApp {}
30+
31+
private StringyConverter myConverter = new StringyConverter();
32+
private ModelConverters converters = ModelConverters.getInstance();
33+
34+
@BeforeEach
35+
public void registerConverter() {
36+
converters.addConverter(myConverter);
37+
}
38+
39+
@AfterEach
40+
public void unregisterConverter() {
41+
converters.removeConverter(myConverter);
42+
}
43+
44+
@Test
45+
public void testApp() throws Exception {
46+
mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL))
47+
.andExpect(status().isOk())
48+
.andExpect(jsonPath("$.openapi", is("3.0.1")))
49+
.andExpect(jsonPath("$.components.schemas.Foo.required", is(List.of("stringy"))))
50+
.andExpect(jsonPath("$.components.schemas.Bar", not(hasProperty("required"))));
51+
}
52+
=======
53+
import SpringBootApplication;
54+
import AbstractSpringDocTest;
55+
56+
public class SpringDocApp157Test extends AbstractSpringDocTest {
57+
58+
@SpringBootApplication
59+
static class SpringDocTestApp {}
60+
61+
>>>>>>> 5537e88ef7529cc8bca8d4f85a36cfab6b6b5b86
62+
}
Lines changed: 70 additions & 0 deletions
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)