From 85493367b6412fa8b161c4c219ca0d31217259c6 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 11 Sep 2017 14:32:47 +0100 Subject: [PATCH] Rework health response structure to eliminate chance of key clashes Previously, if a health's details contained a key named status (either because an indicator bean was named statusHealthIndicator or an indicator added an entry named status to its details) this would clash with the health's own status as the details were serialized as siblings of the status field. This commit updates Health to remove @JsonAnyGetter from getDetails(). This means that all of a Health's details will now be nested within a separate details field, thereby preventing a possible clash with the status field. Closes gh-10249 --- .../org/springframework/boot/actuate/health/Health.java | 2 -- .../endpoint/web/HealthEndpointWebIntegrationTests.java | 9 +++++---- .../actuate/health/CompositeHealthIndicatorTests.java | 9 +++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java index 771e6a8805..da277125ff 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java @@ -20,7 +20,6 @@ import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; -import com.fasterxml.jackson.annotation.JsonAnyGetter; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonUnwrapped; @@ -80,7 +79,6 @@ public final class Health { * Return the details of the health. * @return the details (or an empty map) */ - @JsonAnyGetter public Map getDetails() { return this.details; } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/HealthEndpointWebIntegrationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/HealthEndpointWebIntegrationTests.java index 24f8bee059..2b95328e20 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/HealthEndpointWebIntegrationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/HealthEndpointWebIntegrationTests.java @@ -49,8 +49,9 @@ public class HealthEndpointWebIntegrationTests { @Test public void whenHealthIsUp200ResponseIsReturned() throws Exception { client.get().uri("/application/health").exchange().expectStatus().isOk() - .expectBody().jsonPath("status").isEqualTo("UP").jsonPath("alpha.status") - .isEqualTo("UP").jsonPath("bravo.status").isEqualTo("UP"); + .expectBody().jsonPath("status").isEqualTo("UP") + .jsonPath("details.alpha.status").isEqualTo("UP") + .jsonPath("details.bravo.status").isEqualTo("UP"); } @Test @@ -59,8 +60,8 @@ public class HealthEndpointWebIntegrationTests { .setHealth(Health.down().build()); client.get().uri("/application/health").exchange().expectStatus() .isEqualTo(HttpStatus.SERVICE_UNAVAILABLE).expectBody().jsonPath("status") - .isEqualTo("DOWN").jsonPath("alpha.status").isEqualTo("DOWN") - .jsonPath("bravo.status").isEqualTo("UP"); + .isEqualTo("DOWN").jsonPath("details.alpha.status").isEqualTo("DOWN") + .jsonPath("details.bravo.status").isEqualTo("UP"); } @Configuration diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/CompositeHealthIndicatorTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/CompositeHealthIndicatorTests.java index 1652553209..dd09a358b2 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/CompositeHealthIndicatorTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/CompositeHealthIndicatorTests.java @@ -120,10 +120,11 @@ public class CompositeHealthIndicatorTests { composite.addHealthIndicator("db", innerComposite); Health result = composite.health(); ObjectMapper mapper = new ObjectMapper(); - assertThat(mapper.writeValueAsString(result)) - .isEqualTo("{\"status\":\"UNKNOWN\",\"db\":{\"status\":\"UNKNOWN\"" - + ",\"db1\":{\"status\":\"UNKNOWN\",\"1\":\"1\"}," - + "\"db2\":{\"status\":\"UNKNOWN\",\"2\":\"2\"}}}"); + assertThat(mapper.writeValueAsString(result)).isEqualTo( + "{\"status\":\"UNKNOWN\",\"details\":{\"db\":{\"status\":\"UNKNOWN\"" + + ",\"details\":{\"db1\":{\"status\":\"UNKNOWN\",\"details\"" + + ":{\"1\":\"1\"}},\"db2\":{\"status\":\"UNKNOWN\",\"details\"" + + ":{\"2\":\"2\"}}}}}}"); } }