Commit 85493367 authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent bc9d8bb1
...@@ -20,7 +20,6 @@ import java.util.Collections; ...@@ -20,7 +20,6 @@ import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonUnwrapped; import com.fasterxml.jackson.annotation.JsonUnwrapped;
...@@ -80,7 +79,6 @@ public final class Health { ...@@ -80,7 +79,6 @@ public final class Health {
* Return the details of the health. * Return the details of the health.
* @return the details (or an empty map) * @return the details (or an empty map)
*/ */
@JsonAnyGetter
public Map<String, Object> getDetails() { public Map<String, Object> getDetails() {
return this.details; return this.details;
} }
......
...@@ -49,8 +49,9 @@ public class HealthEndpointWebIntegrationTests { ...@@ -49,8 +49,9 @@ public class HealthEndpointWebIntegrationTests {
@Test @Test
public void whenHealthIsUp200ResponseIsReturned() throws Exception { public void whenHealthIsUp200ResponseIsReturned() throws Exception {
client.get().uri("/application/health").exchange().expectStatus().isOk() client.get().uri("/application/health").exchange().expectStatus().isOk()
.expectBody().jsonPath("status").isEqualTo("UP").jsonPath("alpha.status") .expectBody().jsonPath("status").isEqualTo("UP")
.isEqualTo("UP").jsonPath("bravo.status").isEqualTo("UP"); .jsonPath("details.alpha.status").isEqualTo("UP")
.jsonPath("details.bravo.status").isEqualTo("UP");
} }
@Test @Test
...@@ -59,8 +60,8 @@ public class HealthEndpointWebIntegrationTests { ...@@ -59,8 +60,8 @@ public class HealthEndpointWebIntegrationTests {
.setHealth(Health.down().build()); .setHealth(Health.down().build());
client.get().uri("/application/health").exchange().expectStatus() client.get().uri("/application/health").exchange().expectStatus()
.isEqualTo(HttpStatus.SERVICE_UNAVAILABLE).expectBody().jsonPath("status") .isEqualTo(HttpStatus.SERVICE_UNAVAILABLE).expectBody().jsonPath("status")
.isEqualTo("DOWN").jsonPath("alpha.status").isEqualTo("DOWN") .isEqualTo("DOWN").jsonPath("details.alpha.status").isEqualTo("DOWN")
.jsonPath("bravo.status").isEqualTo("UP"); .jsonPath("details.bravo.status").isEqualTo("UP");
} }
@Configuration @Configuration
......
...@@ -120,10 +120,11 @@ public class CompositeHealthIndicatorTests { ...@@ -120,10 +120,11 @@ public class CompositeHealthIndicatorTests {
composite.addHealthIndicator("db", innerComposite); composite.addHealthIndicator("db", innerComposite);
Health result = composite.health(); Health result = composite.health();
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
assertThat(mapper.writeValueAsString(result)) assertThat(mapper.writeValueAsString(result)).isEqualTo(
.isEqualTo("{\"status\":\"UNKNOWN\",\"db\":{\"status\":\"UNKNOWN\"" "{\"status\":\"UNKNOWN\",\"details\":{\"db\":{\"status\":\"UNKNOWN\""
+ ",\"db1\":{\"status\":\"UNKNOWN\",\"1\":\"1\"}," + ",\"details\":{\"db1\":{\"status\":\"UNKNOWN\",\"details\""
+ "\"db2\":{\"status\":\"UNKNOWN\",\"2\":\"2\"}}}"); + ":{\"1\":\"1\"}},\"db2\":{\"status\":\"UNKNOWN\",\"details\""
+ ":{\"2\":\"2\"}}}}}}");
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment