From 1f6983c4c9e8fa200df79b4ab0964e22389e1291 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 8 Jun 2021 18:14:50 -0700 Subject: [PATCH] Make CompositeHealth.getDetails() public Change `CompositeHealth.getDetails()` to public so that it serializes correctly when `MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS` is disabled. Fixes gh-26797 --- .../boot/actuate/health/CompositeHealth.java | 4 ++-- .../actuate/health/CompositeHealthTests.java | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/CompositeHealth.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/CompositeHealth.java index 8e897dd148b5..767de0e21cbb 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/CompositeHealth.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/CompositeHealth.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -66,7 +66,7 @@ public Map getComponents() { @JsonInclude(Include.NON_EMPTY) @JsonProperty - Map getDetails() { + public Map getDetails() { return this.details; } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/CompositeHealthTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/CompositeHealthTests.java index 3bdb8a02d77a..987edf0aee2b 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/CompositeHealthTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/CompositeHealthTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import java.util.LinkedHashMap; import java.util.Map; +import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; @@ -80,4 +81,17 @@ void serializeV2WithJacksonReturnsValidJson() throws Exception { + "\"db2\":{\"status\":\"DOWN\",\"details\":{\"a\":\"b\"}}}}"); } + @Test // gh-26797 + void serializeV2WithJacksonAndDisabledCanOverrideAccessModifiersReturnsValidJson() throws Exception { + Map components = new LinkedHashMap<>(); + components.put("db1", Health.up().build()); + components.put("db2", Health.down().withDetail("a", "b").build()); + CompositeHealth health = new CompositeHealth(ApiVersion.V2, Status.UP, components); + ObjectMapper mapper = new ObjectMapper(); + mapper.disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS); + String json = mapper.writeValueAsString(health); + assertThat(json).isEqualTo("{\"status\":\"UP\",\"details\":{\"db1\":{\"status\":\"UP\"}," + + "\"db2\":{\"status\":\"DOWN\",\"details\":{\"a\":\"b\"}}}}"); + } + }