From 8f9fd9709518c48cd6867ec896dd1c7e85d5a0bd Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 26 Sep 2019 18:20:59 -0700 Subject: [PATCH] Don't reset defaults if source collection is empty Update `LegacyHealthEndpointCompatibilityConfiguration` to ensure that the default configuration is only overwritten when the user has explicitly set new values. Fixes gh-18354 --- ...lthEndpointCompatibilityConfiguration.java | 5 +++-- .../HealthEndpointAutoConfigurationTests.java | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/LegacyHealthEndpointCompatibilityConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/LegacyHealthEndpointCompatibilityConfiguration.java index 8a9ba893e39..0c5b32ff2ec 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/LegacyHealthEndpointCompatibilityConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/LegacyHealthEndpointCompatibilityConfiguration.java @@ -30,6 +30,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.util.CollectionUtils; /** * Configuration to adapt legacy deprecated health endpoint classes and interfaces. @@ -46,7 +47,7 @@ class LegacyHealthEndpointCompatibilityConfiguration { @ConditionalOnMissingBean HealthAggregator healthAggregator(HealthIndicatorProperties healthIndicatorProperties) { OrderedHealthAggregator aggregator = new OrderedHealthAggregator(); - if (healthIndicatorProperties.getOrder() != null) { + if (!CollectionUtils.isEmpty(healthIndicatorProperties.getOrder())) { aggregator.setStatusOrder(healthIndicatorProperties.getOrder()); } return aggregator; @@ -56,7 +57,7 @@ class LegacyHealthEndpointCompatibilityConfiguration { @ConditionalOnMissingBean HealthStatusHttpMapper healthStatusHttpMapper(HealthIndicatorProperties healthIndicatorProperties) { HealthStatusHttpMapper mapper = new HealthStatusHttpMapper(); - if (healthIndicatorProperties.getHttpMapping() != null) { + if (!CollectionUtils.isEmpty(healthIndicatorProperties.getHttpMapping())) { mapper.setStatusMapping(healthIndicatorProperties.getHttpMapping()); } return mapper; diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/health/HealthEndpointAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/health/HealthEndpointAutoConfigurationTests.java index a82e79331b4..ca1986172a5 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/health/HealthEndpointAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/health/HealthEndpointAutoConfigurationTests.java @@ -17,7 +17,9 @@ package org.springframework.boot.actuate.autoconfigure.health; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import org.junit.jupiter.api.Test; import reactor.core.publisher.Mono; @@ -271,6 +273,26 @@ class HealthEndpointAutoConfigurationTests { }); } + @Test // gh-18354 + void runCreatesLegacyHealthAggregator() { + this.contextRunner.run((context) -> { + HealthAggregator aggregator = context.getBean(HealthAggregator.class); + Map healths = new LinkedHashMap<>(); + healths.put("one", Health.up().build()); + healths.put("two", Health.down().build()); + Health result = aggregator.aggregate(healths); + assertThat(result.getStatus()).isEqualTo(Status.DOWN); + }); + } + + @Test // gh-18354 + void runCreatesLegacyHealthStatusHttpMapper() { + this.contextRunner.run((context) -> { + HealthStatusHttpMapper mapper = context.getBean(HealthStatusHttpMapper.class); + assertThat(mapper.mapStatus(Status.DOWN)).isEqualTo(503); + }); + } + @Configuration(proxyBeanMethods = false) static class HealthIndicatorsConfiguration {