Polish "Allow ProblemDetailsExceptionHandlers to be proxied"

See gh-34503
pull/34741/head^2
Andy Wilkinson 2 years ago
parent b96a8dd370
commit 1eb5bbe3ea

@ -32,12 +32,17 @@ import java.util.concurrent.TimeUnit;
import java.util.function.Consumer; import java.util.function.Consumer;
import jakarta.validation.ValidatorFactory; import jakarta.validation.ValidatorFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource; import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.aop.support.AopUtils;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration;
import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration; import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration;
import org.springframework.boot.autoconfigure.validation.ValidatorAdapter; import org.springframework.boot.autoconfigure.validation.ValidatorAdapter;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
@ -645,6 +650,17 @@ class WebFluxAutoConfigurationTests {
.run((context) -> assertThat(context).hasSingleBean(ProblemDetailsExceptionHandler.class)); .run((context) -> assertThat(context).hasSingleBean(ProblemDetailsExceptionHandler.class));
} }
@Test
void problemDetailsExceptionHandlerDoesNotPreventProxying() {
this.contextRunner.withConfiguration(AutoConfigurations.of(AopAutoConfiguration.class))
.withBean(ExceptionHandlerInterceptor.class)
.withPropertyValues("spring.webflux.problemdetails.enabled:true")
.run((context) -> {
assertThat(context).hasSingleBean(ProblemDetailsExceptionHandler.class);
assertThat(AopUtils.isCglibProxy(context.getBean(ProblemDetailsExceptionHandler.class)));
});
}
@Test @Test
void problemDetailsBacksOffWhenExceptionHandler() { void problemDetailsBacksOffWhenExceptionHandler() {
this.contextRunner.withPropertyValues("spring.webflux.problemdetails.enabled:true") this.contextRunner.withPropertyValues("spring.webflux.problemdetails.enabled:true")
@ -957,4 +973,14 @@ class WebFluxAutoConfigurationTests {
} }
@Aspect
static class ExceptionHandlerInterceptor {
@AfterReturning(pointcut = "@annotation(org.springframework.web.bind.annotation.ExceptionHandler)",
returning = "returnValue")
void exceptionHandlerIntercept(JoinPoint joinPoint, Object returnValue) {
}
}
} }

@ -36,9 +36,14 @@ import java.util.function.Consumer;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.ValidatorFactory; import jakarta.validation.ValidatorFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.aop.support.AopUtils;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
@ -988,6 +993,17 @@ class WebMvcAutoConfigurationTests {
.run((context) -> assertThat(context).hasSingleBean(ProblemDetailsExceptionHandler.class)); .run((context) -> assertThat(context).hasSingleBean(ProblemDetailsExceptionHandler.class));
} }
@Test
void problemDetailsExceptionHandlerDoesNotPreventProxying() {
this.contextRunner.withConfiguration(AutoConfigurations.of(AopAutoConfiguration.class))
.withBean(ExceptionHandlerInterceptor.class)
.withPropertyValues("spring.mvc.problemdetails.enabled:true")
.run((context) -> {
assertThat(context).hasSingleBean(ProblemDetailsExceptionHandler.class);
assertThat(AopUtils.isCglibProxy(context.getBean(ProblemDetailsExceptionHandler.class)));
});
}
@Test @Test
void problemDetailsBacksOffWhenExceptionHandler() { void problemDetailsBacksOffWhenExceptionHandler() {
this.contextRunner.withPropertyValues("spring.mvc.problemdetails.enabled:true") this.contextRunner.withPropertyValues("spring.mvc.problemdetails.enabled:true")
@ -1539,4 +1555,14 @@ class WebMvcAutoConfigurationTests {
} }
@Aspect
static class ExceptionHandlerInterceptor {
@AfterReturning(pointcut = "@annotation(org.springframework.web.bind.annotation.ExceptionHandler)",
returning = "returnValue")
void exceptionHandlerIntercept(JoinPoint joinPoint, Object returnValue) {
}
}
} }

Loading…
Cancel
Save