From 5e7be502653b3980f1618a5a2066358ce2fdcea1 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 23 Aug 2018 11:34:16 +0200 Subject: [PATCH] Restore trace request param behaviour with error controller This commits restores the behaviour of Spring Boot 1.x with regards to the "trace" request param used to add the stacktrace to the model. This was inadvertently changed so that the stacktrace would be added if the parameter wasn't set. Closes gh-14171 --- .../error/AbstractErrorController.java | 3 +++ .../BasicErrorControllerIntegrationTests.java | 24 +++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/AbstractErrorController.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/AbstractErrorController.java index e0bbaf22a7..f60d1fe33c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/AbstractErrorController.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/AbstractErrorController.java @@ -76,6 +76,9 @@ public abstract class AbstractErrorController implements ErrorController { protected boolean getTraceParameter(HttpServletRequest request) { String parameter = request.getParameter("trace"); + if (parameter == null) { + return false; + } return !"false".equalsIgnoreCase(parameter); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorControllerIntegrationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorControllerIntegrationTests.java index 6b32f333c8..3432258d77 100755 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorControllerIntegrationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorControllerIntegrationTests.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.function.Supplier; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -94,15 +95,30 @@ public class BasicErrorControllerIntegrationTests { } @Test + public void testErrorForMachineClientTraceParamTrue() { + errorForMachineClientOnTraceParam(() -> createUrl("?trace=true"), true); + } + + @Test + public void testErrorForMachineClientTraceParamFalse() { + errorForMachineClientOnTraceParam(() -> createUrl("?trace=false"), false); + } + + @Test + public void testErrorForMachineClientTraceParamAbsent() { + errorForMachineClientOnTraceParam(() -> createUrl(""), false); + } + @SuppressWarnings("rawtypes") - public void testErrorForMachineClientTraceParamStacktrace() { + private void errorForMachineClientOnTraceParam(Supplier url, + boolean expectedTrace) { load("--server.error.include-exception=true", "--server.error.include-stacktrace=on-trace-param"); - ResponseEntity entity = new TestRestTemplate() - .getForEntity(createUrl("?trace=true"), Map.class); + ResponseEntity entity = new TestRestTemplate().getForEntity(url.get(), + Map.class); assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", IllegalStateException.class, "Expected!", "/"); - assertThat(entity.getBody().containsKey("trace")).isTrue(); + assertThat(entity.getBody().containsKey("trace")).isEqualTo(expectedTrace); } @Test