Skip to content

Support for @JsonView on @ExceptionHandler methods #1159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ public void buildGenericResponse(Components components, Map<String, Object> 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<String, ApiResponse> apiResponses = computeResponseFromDoc(components, methodParameter, apiResponsesOp, methodAttributes);
buildGenericApiResponses(components, methodParameter, apiResponsesOp, methodAttributes);
apiResponses.forEach(controllerAdviceInfoApiResponseMap::put);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test.org.springdoc.api.app157;

public class CustomException extends RuntimeException{
public CustomException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<FooBean> 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<FooBean> storeAssignmentPublishingError(CustomException e) {
return new ResponseEntity<>(new FooBean("BAD Request",400), HttpStatus.BAD_REQUEST);
}
}
Original file line number Diff line number Diff line change
@@ -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";
}
}


Original file line number Diff line number Diff line change
@@ -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 {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package test.org.springdoc.api.app157;

public class Views {
public static class View1 {
}
public static class View2 extends View1 {
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
}