Skip to content

Commit 49d9adc

Browse files
committed
project update
2 parents caae4c4 + 299683c commit 49d9adc

File tree

8 files changed

+203
-0
lines changed

8 files changed

+203
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ public void buildGenericResponse(Components components, Map<String, Object> find
166166
ApiResponses apiResponsesOp = new ApiResponses();
167167
MethodAttributes methodAttributes = new MethodAttributes(methodProduces, springDocConfigProperties.getDefaultConsumesMediaType(),
168168
springDocConfigProperties.getDefaultProducesMediaType(), controllerAdviceInfoApiResponseMap);
169+
//calculate JsonView Annotation
170+
methodAttributes.setJsonViewAnnotation(AnnotatedElementUtils.findMergedAnnotation(method, JsonView.class));
169171
Map<String, ApiResponse> apiResponses = computeResponseFromDoc(components, methodParameter, apiResponsesOp, methodAttributes);
170172
buildGenericApiResponses(components, methodParameter, apiResponsesOp, methodAttributes);
171173
apiResponses.forEach(controllerAdviceInfoApiResponseMap::put);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package test.org.springdoc.api.app159;
2+
3+
public class CustomException extends RuntimeException{
4+
public CustomException(String message) {
5+
super(message);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package test.org.springdoc.api.app159;
2+
3+
import com.fasterxml.jackson.annotation.JsonView;
4+
5+
public class FooBean {
6+
@JsonView(Views.View2.class)
7+
private String message;
8+
@JsonView(Views.View1.class)
9+
private int code;
10+
11+
public String getMessage() {
12+
return message;
13+
}
14+
15+
public void setMessage(String message) {
16+
this.message = message;
17+
}
18+
19+
public int getCode() {
20+
return code;
21+
}
22+
23+
public void setCode(int code) {
24+
this.code = code;
25+
}
26+
27+
public FooBean(String message, int code) {
28+
this.message = message;
29+
this.code = code;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package test.org.springdoc.api.app159;
2+
3+
import com.fasterxml.jackson.annotation.JsonView;
4+
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.ControllerAdvice;
8+
import org.springframework.web.bind.annotation.ExceptionHandler;
9+
import org.springframework.web.bind.annotation.ResponseStatus;
10+
11+
@ControllerAdvice(assignableTypes = HelloController.class)
12+
public class FooErrorHandler {
13+
14+
@ExceptionHandler
15+
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
16+
@JsonView(Views.View1.class)
17+
public ResponseEntity<FooBean> storeAssignmentPublishingError(Exception e) {
18+
return new ResponseEntity<>(new FooBean("INTERNAL_SERVER_ERROR",500), HttpStatus.INTERNAL_SERVER_ERROR);
19+
}
20+
21+
@ExceptionHandler
22+
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
23+
@JsonView(Views.View2.class)
24+
public ResponseEntity<FooBean> storeAssignmentPublishingError(CustomException e) {
25+
return new ResponseEntity<>(new FooBean("BAD Request",400), HttpStatus.BAD_REQUEST);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package test.org.springdoc.api.app159;
2+
3+
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
4+
import io.swagger.v3.oas.annotations.info.Info;
5+
import io.swagger.v3.oas.annotations.tags.Tag;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping("/api")
13+
@OpenAPIDefinition(info = @Info(title = "API Examples", version = "1.0"), tags = @Tag(name = "Operations"))
14+
public class HelloController {
15+
16+
@PostMapping("/foo")
17+
public String create(@RequestBody String foo) {
18+
return "foo";
19+
}
20+
}
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package test.org.springdoc.api.app159;
2+
3+
import test.org.springdoc.api.AbstractSpringDocTest;
4+
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
public class SpringDocApp159Test extends AbstractSpringDocTest {
8+
9+
@SpringBootApplication
10+
static class SpringDocTestApp {}
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package test.org.springdoc.api.app159;
2+
3+
public class Views {
4+
public static class View1 {
5+
}
6+
public static class View2 extends View1 {
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
"post":{
21+
"tags":[
22+
"hello-controller"
23+
],
24+
"operationId":"create",
25+
"requestBody":{
26+
"content":{
27+
"application/json":{
28+
"schema":{
29+
"type":"string"
30+
}
31+
}
32+
},
33+
"required":true
34+
},
35+
"responses":{
36+
"500":{
37+
"description":"Internal Server Error",
38+
"content":{
39+
"*/*":{
40+
"schema":{
41+
"$ref":"#/components/schemas/FooBean_View1"
42+
}
43+
}
44+
}
45+
},
46+
"400":{
47+
"description":"Bad Request",
48+
"content":{
49+
"*/*":{
50+
"schema":{
51+
"$ref":"#/components/schemas/FooBean_View2"
52+
}
53+
}
54+
}
55+
},
56+
"200":{
57+
"description":"OK",
58+
"content":{
59+
"*/*":{
60+
"schema":{
61+
"type":"string"
62+
}
63+
}
64+
}
65+
}
66+
}
67+
}
68+
}
69+
},
70+
"components":{
71+
"schemas":{
72+
"FooBean_View1":{
73+
"type":"object",
74+
"properties":{
75+
"code":{
76+
"type":"integer",
77+
"format":"int32"
78+
}
79+
}
80+
},
81+
"FooBean_View2":{
82+
"type":"object",
83+
"properties":{
84+
"message":{
85+
"type":"string"
86+
},
87+
"code":{
88+
"type":"integer",
89+
"format":"int32"
90+
}
91+
}
92+
}
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)