Skip to content

Pick up ExceptionHandlers from any parent of a class marked as @ControllerAdvice #1156

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 1 commit into from
May 17, 2021
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 @@ -52,6 +52,7 @@
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand Down Expand Up @@ -152,7 +153,7 @@ public void buildGenericResponse(Components components, Map<String, Object> find
if (org.springframework.aop.support.AopUtils.isAopProxy(controllerAdvice))
objClz = org.springframework.aop.support.AopUtils.getTargetClass(controllerAdvice);
ControllerAdviceInfo controllerAdviceInfo = new ControllerAdviceInfo(controllerAdvice);
Arrays.stream(objClz.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(ExceptionHandler.class)).forEach(methods::add);
Arrays.stream(ReflectionUtils.getAllDeclaredMethods(objClz)).filter(m -> m.isAnnotationPresent(ExceptionHandler.class)).forEach(methods::add);
// for each one build ApiResponse and add it to existing responses
for (Method method : methods) {
if (!operationService.isHidden(method)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package test.org.springdoc.api.app157;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

public class CommonFooErrorHandler {

@ExceptionHandler
@ResponseStatus(HttpStatus.CONFLICT)
public ErrorDTO onException(Exception e) {
return new ErrorDTO("Something wrong has happened");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package test.org.springdoc.api.app157;

public class ErrorDTO {
private String message;

public ErrorDTO() {
}

public ErrorDTO(String message) {
this.message = message;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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.GetMapping;
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 {

@GetMapping("/foo")
public SimpleDTO hello() {
return new SimpleDTO("foo");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package test.org.springdoc.api.app157;

public class SimpleDTO {

private String payload;

public SimpleDTO() {
}

public SimpleDTO(String payload) {
this.payload = payload;
}

public String getPayload() {
return payload;
}

public void setPayload(String payload) {
this.payload = payload;
}

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

import org.springframework.web.bind.annotation.ControllerAdvice;

@ControllerAdvice(assignableTypes = HelloController.class)
public class SpecificFooErrorHandler extends CommonFooErrorHandler {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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,70 @@
{
"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": {
"get": {
"tags": [
"hello-controller"
],
"operationId": "hello",
"responses": {
"409": {
"description": "Conflict",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/ErrorDTO"
}
}
}
},
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/SimpleDTO"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"ErrorDTO": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
},
"SimpleDTO": {
"type": "object",
"properties": {
"payload": {
"type": "string"
}
}
}
}
}
}