From 68732c28b67e67e1a21b7d6134d79d69c2c20d72 Mon Sep 17 00:00:00 2001 From: Malkeet Singh Date: Sat, 8 May 2021 21:00:54 +0530 Subject: [PATCH 1/2] Added support for @JsonView on ExceptionHandlers methods --- .../core/GenericResponseService.java | 4 + .../springdoc/api/app157/CustomException.java | 7 ++ .../org/springdoc/api/app157/FooBean.java | 31 ++++++ .../springdoc/api/app157/FooErrorHandler.java | 26 +++++ .../springdoc/api/app157/HelloController.java | 22 +++++ .../api/app157/SpringDocApp157Test.java | 12 +++ .../test/org/springdoc/api/app157/Views.java | 8 ++ .../src/test/resources/results/app157.json | 95 +++++++++++++++++++ 8 files changed, 205 insertions(+) create mode 100644 springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/CustomException.java create mode 100644 springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/FooBean.java create mode 100644 springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/FooErrorHandler.java create mode 100644 springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/HelloController.java create mode 100644 springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/SpringDocApp157Test.java create mode 100644 springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/Views.java create mode 100644 springdoc-openapi-webmvc-core/src/test/resources/results/app157.json diff --git a/springdoc-openapi-common/src/main/java/org/springdoc/core/GenericResponseService.java b/springdoc-openapi-common/src/main/java/org/springdoc/core/GenericResponseService.java index d4ea9f76a..7d722ef1d 100644 --- a/springdoc-openapi-common/src/main/java/org/springdoc/core/GenericResponseService.java +++ b/springdoc-openapi-common/src/main/java/org/springdoc/core/GenericResponseService.java @@ -38,6 +38,7 @@ import com.fasterxml.jackson.annotation.JsonView; import io.swagger.v3.core.util.AnnotationsUtils; +import io.swagger.v3.core.util.ReflectionUtils; import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.media.Content; @@ -53,6 +54,7 @@ import org.springframework.http.HttpStatus; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.method.ControllerAdviceBean; @@ -165,6 +167,8 @@ public void buildGenericResponse(Components components, Map find ApiResponses apiResponsesOp = new ApiResponses(); MethodAttributes methodAttributes = new MethodAttributes(methodProduces, springDocConfigProperties.getDefaultConsumesMediaType(), springDocConfigProperties.getDefaultProducesMediaType(), controllerAdviceInfoApiResponseMap); + //calculate JsonView Annotation + methodAttributes.setJsonViewAnnotation(AnnotatedElementUtils.findMergedAnnotation(method, JsonView.class)); Map apiResponses = computeResponseFromDoc(components, methodParameter, apiResponsesOp, methodAttributes); buildGenericApiResponses(components, methodParameter, apiResponsesOp, methodAttributes); apiResponses.forEach(controllerAdviceInfoApiResponseMap::put); diff --git a/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/CustomException.java b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/CustomException.java new file mode 100644 index 000000000..de9cec8be --- /dev/null +++ b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/CustomException.java @@ -0,0 +1,7 @@ +package test.org.springdoc.api.app157; + +public class CustomException extends RuntimeException{ + public CustomException(String message) { + super(message); + } +} diff --git a/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/FooBean.java b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/FooBean.java new file mode 100644 index 000000000..48231d72d --- /dev/null +++ b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/FooBean.java @@ -0,0 +1,31 @@ +package test.org.springdoc.api.app157; + +import com.fasterxml.jackson.annotation.JsonView; + +public class FooBean { + @JsonView(Views.View2.class) + private String message; + @JsonView(Views.View1.class) + private int code; + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public FooBean(String message, int code) { + this.message = message; + this.code = code; + } +} diff --git a/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/FooErrorHandler.java b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/FooErrorHandler.java new file mode 100644 index 000000000..c94813fce --- /dev/null +++ b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/FooErrorHandler.java @@ -0,0 +1,26 @@ +package test.org.springdoc.api.app157; + +import com.fasterxml.jackson.annotation.JsonView; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ControllerAdvice(assignableTypes = HelloController.class) +public class FooErrorHandler { + + @ExceptionHandler + @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) + @JsonView(Views.View1.class) + public ResponseEntity storeAssignmentPublishingError(Exception e) { + return new ResponseEntity<>(new FooBean("INTERNAL_SERVER_ERROR",500), HttpStatus.INTERNAL_SERVER_ERROR); + } + + @ExceptionHandler + @ResponseStatus(value = HttpStatus.BAD_REQUEST) + @JsonView(Views.View2.class) + public ResponseEntity storeAssignmentPublishingError(CustomException e) { + return new ResponseEntity<>(new FooBean("BAD Request",400), HttpStatus.BAD_REQUEST); + } +} diff --git a/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/HelloController.java b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/HelloController.java new file mode 100644 index 000000000..20f84e557 --- /dev/null +++ b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/HelloController.java @@ -0,0 +1,22 @@ +package test.org.springdoc.api.app157; + +import io.swagger.v3.oas.annotations.OpenAPIDefinition; +import io.swagger.v3.oas.annotations.info.Info; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api") +@OpenAPIDefinition(info = @Info(title = "API Examples", version = "1.0"), tags = @Tag(name = "Operations")) +public class HelloController { + + @PostMapping("/foo") + public String create(@RequestBody String foo) { + return "foo"; + } +} + + diff --git a/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/SpringDocApp157Test.java b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/SpringDocApp157Test.java new file mode 100644 index 000000000..e97b838fc --- /dev/null +++ b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/SpringDocApp157Test.java @@ -0,0 +1,12 @@ +package test.org.springdoc.api.app157; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import test.org.springdoc.api.AbstractSpringDocTest; + + +public class SpringDocApp157Test extends AbstractSpringDocTest { + + @SpringBootApplication + static class SpringDocTestApp {} + +} diff --git a/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/Views.java b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/Views.java new file mode 100644 index 000000000..bdf6c61f5 --- /dev/null +++ b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app157/Views.java @@ -0,0 +1,8 @@ +package test.org.springdoc.api.app157; + +public class Views { + public static class View1 { + } + public static class View2 extends View1 { + } +} diff --git a/springdoc-openapi-webmvc-core/src/test/resources/results/app157.json b/springdoc-openapi-webmvc-core/src/test/resources/results/app157.json new file mode 100644 index 000000000..b8efc40ca --- /dev/null +++ b/springdoc-openapi-webmvc-core/src/test/resources/results/app157.json @@ -0,0 +1,95 @@ +{ + "openapi":"3.0.1", + "info":{ + "title":"API Examples", + "version":"1.0" + }, + "servers":[ + { + "url":"http://localhost", + "description":"Generated server url" + } + ], + "tags":[ + { + "name":"Operations" + } + ], + "paths":{ + "/api/foo":{ + "post":{ + "tags":[ + "hello-controller" + ], + "operationId":"create", + "requestBody":{ + "content":{ + "application/json":{ + "schema":{ + "type":"string" + } + } + }, + "required":true + }, + "responses":{ + "500":{ + "description":"Internal Server Error", + "content":{ + "*/*":{ + "schema":{ + "$ref":"#/components/schemas/FooBean_View1" + } + } + } + }, + "400":{ + "description":"Bad Request", + "content":{ + "*/*":{ + "schema":{ + "$ref":"#/components/schemas/FooBean_View2" + } + } + } + }, + "200":{ + "description":"OK", + "content":{ + "*/*":{ + "schema":{ + "type":"string" + } + } + } + } + } + } + } + }, + "components":{ + "schemas":{ + "FooBean_View1":{ + "type":"object", + "properties":{ + "code":{ + "type":"integer", + "format":"int32" + } + } + }, + "FooBean_View2":{ + "type":"object", + "properties":{ + "message":{ + "type":"string" + }, + "code":{ + "type":"integer", + "format":"int32" + } + } + } + } + } +} From 299683c914de55234fd9a6323e06103948379a7d Mon Sep 17 00:00:00 2001 From: Malkeet Singh Date: Sat, 8 May 2021 21:09:24 +0530 Subject: [PATCH 2/2] Added support for @JsonView on ExceptionHandlers methods --- .../main/java/org/springdoc/core/GenericResponseService.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/springdoc-openapi-common/src/main/java/org/springdoc/core/GenericResponseService.java b/springdoc-openapi-common/src/main/java/org/springdoc/core/GenericResponseService.java index 7d722ef1d..0032db85e 100644 --- a/springdoc-openapi-common/src/main/java/org/springdoc/core/GenericResponseService.java +++ b/springdoc-openapi-common/src/main/java/org/springdoc/core/GenericResponseService.java @@ -38,7 +38,6 @@ import com.fasterxml.jackson.annotation.JsonView; import io.swagger.v3.core.util.AnnotationsUtils; -import io.swagger.v3.core.util.ReflectionUtils; import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.media.Content; @@ -54,7 +53,6 @@ import org.springframework.http.HttpStatus; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.method.ControllerAdviceBean;