Make LogCorrelationPropertySource an EnumerablePropertySource

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

Closes gh-38349
This commit is contained in:
Phillip Webb 2023-11-13 10:40:24 -08:00
parent ba56953ea5
commit 80210e93d3
2 changed files with 18 additions and 1 deletions

View File

@ -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<Object> {
private static class LogCorrelationPropertySource extends EnumerablePropertySource<Object> {
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)) {

View File

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