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

Conversation

Malkeekalsi
Copy link

@Malkeekalsi Malkeekalsi commented May 8, 2021

Currently, springdoc-api ignores the @JsonView annotation on exception handler methods, and it generates a generic schema(all fields) for the ResponseBean. However, spring rest supports @JsonView annotation on exception handler methods and generates a different schema for different Views of ResponseBean.

This PR introduces the support for @JsonView annotation on Exception handler methods.

In the below example, exception handlers for INTERNAL_SERVER_ERROR and BAD_REQUEST have different JsonViews, but springdoc generates the same schema /components/schemas/FooBean for both error responses.

public class FooBean {
    @JsonView(Views.View2.class)
    private String message;
    @JsonView(Views.View1.class)
    private int code;
}

//Exception handler class
@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);
	}
}

//Existing response. Documentation is not generated as per @JsonView annotation

 "responses":{
               "500":{
                  "description":"Internal Server Error",
                  "content":{
                     "*/*":{
                        "schema":{
                           "$ref":"#/components/schemas/FooBean"
                        }
                     }
                  }
               },
               "400":{
                  "description":"Bad Request",
                  "content":{
                     "*/*":{
                        "schema":{
                           "$ref":"#/components/schemas/FooBean"
                        }
                     }
                  }
               },
               "200":{
                  "description":"OK",
                  "content":{
                     "*/*":{
                        "schema":{
                           "type":"string"
                        }
                     }
                  }
               }
            },
       "components":{
         "schemas":{
                 "FooBean":{
                    "type":"object",
                    "properties":{
                       "message":{
                          "type":"string"
                       },
                       "code":{
                          "type":"integer",
                          "format":"int32"
                       }
                    }
                 }
              }
          }

//After this PR
In the below example, springdoc generates the different schema (#/components/schemas/FooBean_View1 , #/components/schemas/FooBean_View2) for error responses.

 "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"
                }
              }
            }
          }
        }

@bnasslahsen bnasslahsen merged commit 49d9adc into springdoc:master May 17, 2021
@bnasslahsen
Copy link
Collaborator

Thank you @Malkeekalsi for your contribution

@bnasslahsen bnasslahsen added the enhancement New feature or request label Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants