From c49605cd7b6a76168738019a21632bd64b2634ed Mon Sep 17 00:00:00 2001 From: Lopfest Date: Wed, 5 Aug 2020 19:15:10 +0200 Subject: [PATCH 1/2] Fix include exception handling in DefaultErrorAttributes This commit fixes a problem with the handling of the includeException field in DefaultErrorAttributes. See gh-22750 --- .../boot/web/reactive/error/DefaultErrorAttributes.java | 2 +- .../boot/web/servlet/error/DefaultErrorAttributes.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributes.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributes.java index 040fdadd69..6210c5ce8c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributes.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributes.java @@ -86,7 +86,7 @@ public class DefaultErrorAttributes implements ErrorAttributes { @Override public Map getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) { Map errorAttributes = getErrorAttributes(request, options.isIncluded(Include.STACK_TRACE)); - if (this.includeException != null) { + if (Boolean.TRUE.equals(this.includeException)) { options = options.including(Include.EXCEPTION); } if (!options.isIncluded(Include.EXCEPTION)) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributes.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributes.java index 5ec93af76c..d1c991cf4b 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributes.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributes.java @@ -108,7 +108,7 @@ public class DefaultErrorAttributes implements ErrorAttributes, HandlerException @Override public Map getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) { Map errorAttributes = getErrorAttributes(webRequest, options.isIncluded(Include.STACK_TRACE)); - if (this.includeException != null) { + if (Boolean.TRUE.equals(this.includeException)) { options = options.including(Include.EXCEPTION); } if (!options.isIncluded(Include.EXCEPTION)) { From c2ec46c5b10d17aa0bf0bb638162741738341a69 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Thu, 13 Aug 2020 19:14:00 -0500 Subject: [PATCH 2/2] Polish "Fix include exception handling in DefaultErrorAttributes" See gh-22750 --- .../reactive/error/DefaultErrorAttributesTests.java | 13 +++++++++++++ .../servlet/error/DefaultErrorAttributesTests.java | 11 +++++++++++ 2 files changed, 24 insertions(+) diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributesTests.java index 5966056319..ad0c061cc2 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributesTests.java @@ -164,6 +164,19 @@ class DefaultErrorAttributesTests { assertThat(attributes.get("message")).isEqualTo("Test"); } + @Test + @SuppressWarnings("deprecation") + void excludeExceptionWithDeprecatedConstructor() { + RuntimeException error = new RuntimeException("Test"); + this.errorAttributes = new DefaultErrorAttributes(false); + MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); + ServerRequest serverRequest = buildServerRequest(request, error); + Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, + ErrorAttributeOptions.of()); + assertThat(this.errorAttributes.getError(serverRequest)).isSameAs(error); + assertThat(attributes.get("exception")).isNull(); + } + @Test void processResponseStatusException() { RuntimeException nested = new RuntimeException("Test"); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributesTests.java index 22ac9eee84..8c29405b9e 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributesTests.java @@ -231,6 +231,17 @@ class DefaultErrorAttributesTests { assertThat(attributes.get("message")).isEqualTo("Test"); } + @Test + @SuppressWarnings("deprecation") + void excludeExceptionAttributeWithDeprecatedConstructor() { + DefaultErrorAttributes errorAttributes = new DefaultErrorAttributes(false); + RuntimeException ex = new RuntimeException("Test"); + this.request.setAttribute("javax.servlet.error.exception", ex); + Map attributes = errorAttributes.getErrorAttributes(this.webRequest, + ErrorAttributeOptions.of()); + assertThat(attributes.get("exception")).isNull(); + } + @Test void withStackTraceAttribute() { RuntimeException ex = new RuntimeException("Test");