Browse Source

Make LogCorrelationPropertySource an EnumerablePropertySource

Change `LogCorrelationPropertySource` to an `EnumerablePropertySource`
to reduce the likelihood of `Binder` errors.

Closes gh-38349
pull/38350/head
Phillip Webb 2 years ago
parent
commit
80210e93d3
  1. 8
      spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/LogCorrelationEnvironmentPostProcessor.java
  2. 11
      spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/LogCorrelationEnvironmentPostProcessorTests.java

8
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; @@ -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 @@ -45,7 +46,7 @@ class LogCorrelationEnvironmentPostProcessor implements EnvironmentPostProcessor
/**
* Log correlation {@link PropertySource}.
*/
private static class LogCorrelationPropertySource extends PropertySource<Object> {
private static class LogCorrelationPropertySource extends EnumerablePropertySource<Object> {
private static final String NAME = "logCorrelation";
@ -56,6 +57,11 @@ class LogCorrelationEnvironmentPostProcessor implements EnvironmentPostProcessor @@ -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)) {

11
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; @@ -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 { @@ -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);
}
}

Loading…
Cancel
Save