From 80210e93d337be686b01b222a41e718e8ea22c9b Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 13 Nov 2023 10:40:24 -0800 Subject: [PATCH] Make LogCorrelationPropertySource an EnumerablePropertySource Change `LogCorrelationPropertySource` to an `EnumerablePropertySource` to reduce the likelihood of `Binder` errors. Closes gh-38349 --- .../LogCorrelationEnvironmentPostProcessor.java | 8 +++++++- .../LogCorrelationEnvironmentPostProcessorTests.java | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/LogCorrelationEnvironmentPostProcessor.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/LogCorrelationEnvironmentPostProcessor.java index 50a92939f4f..b0479bd29c3 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/LogCorrelationEnvironmentPostProcessor.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/LogCorrelationEnvironmentPostProcessor.java @@ -20,6 +20,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.logging.LoggingSystem; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.EnumerablePropertySource; import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; import org.springframework.util.ClassUtils; @@ -45,7 +46,7 @@ class LogCorrelationEnvironmentPostProcessor implements EnvironmentPostProcessor /** * Log correlation {@link PropertySource}. */ - private static class LogCorrelationPropertySource extends PropertySource { + private static class LogCorrelationPropertySource extends EnumerablePropertySource { private static final String NAME = "logCorrelation"; @@ -56,6 +57,11 @@ class LogCorrelationEnvironmentPostProcessor implements EnvironmentPostProcessor this.environment = environment; } + @Override + public String[] getPropertyNames() { + return new String[] { LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY }; + } + @Override public Object getProperty(String name) { if (name.equals(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY)) { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/LogCorrelationEnvironmentPostProcessorTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/LogCorrelationEnvironmentPostProcessorTests.java index 4bbfa4a0e8d..3cf84a10db6 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/LogCorrelationEnvironmentPostProcessorTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/LogCorrelationEnvironmentPostProcessorTests.java @@ -23,6 +23,8 @@ import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.testsupport.classpath.ClassPathExclusions; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.PropertySource; import org.springframework.core.env.StandardEnvironment; import static org.assertj.core.api.Assertions.assertThat; @@ -64,4 +66,13 @@ class LogCorrelationEnvironmentPostProcessorTests { .isFalse(); } + @Test + void postProcessEnvironmentAddsEnumerablePropertySource() { + this.postProcessor.postProcessEnvironment(this.environment, this.application); + PropertySource propertySource = this.environment.getPropertySources().get("logCorrelation"); + assertThat(propertySource).isInstanceOf(EnumerablePropertySource.class); + assertThat(((EnumerablePropertySource) propertySource).getPropertyNames()) + .containsExactly(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY); + } + }