From 1eb5bbe3ea638e292463a5eca2c3adfc38c4450d Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 23 Mar 2023 20:34:29 +0000 Subject: [PATCH] Polish "Allow ProblemDetailsExceptionHandlers to be proxied" See gh-34503 --- .../WebFluxAutoConfigurationTests.java | 26 +++++++++++++++++++ .../servlet/WebMvcAutoConfigurationTests.java | 26 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java index 8dfc0c0bbe..5a252a46af 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java @@ -32,12 +32,17 @@ import java.util.concurrent.TimeUnit; import java.util.function.Consumer; 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.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import org.springframework.aop.support.AopUtils; 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.ValidatorAdapter; import org.springframework.boot.autoconfigure.web.ServerProperties; @@ -645,6 +650,17 @@ class WebFluxAutoConfigurationTests { .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 void problemDetailsBacksOffWhenExceptionHandler() { 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) { + } + + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java index 618a868fdc..1da1ae5660 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java @@ -36,9 +36,14 @@ import java.util.function.Consumer; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; 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.springframework.aop.support.AopUtils; 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.http.HttpMessageConverters; import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration; @@ -988,6 +993,17 @@ class WebMvcAutoConfigurationTests { .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 void problemDetailsBacksOffWhenExceptionHandler() { 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) { + } + + } + }