Don't adapt RandomPropertySource
Stop adapting `RandomPropertySource` to `ConfigurationPropertySource` since it's not useful as a binding source. Closes gh-21659
This commit is contained in:
parent
10ab477426
commit
0588e989af
|
@ -21,6 +21,7 @@ import java.util.Deque;
|
|||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Random;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
@ -123,10 +124,16 @@ class SpringConfigurationPropertySources implements Iterable<ConfigurationProper
|
|||
}
|
||||
|
||||
private boolean isIgnored(PropertySource<?> candidate) {
|
||||
return (candidate instanceof StubPropertySource
|
||||
return (isRandomPropertySource(candidate) || candidate instanceof StubPropertySource
|
||||
|| candidate instanceof ConfigurationPropertySourcesPropertySource);
|
||||
}
|
||||
|
||||
private boolean isRandomPropertySource(PropertySource<?> candidate) {
|
||||
Object source = candidate.getSource();
|
||||
return (source instanceof Random) || (source instanceof PropertySource<?>
|
||||
&& ((PropertySource<?>) source).getSource() instanceof Random);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import java.util.Iterator;
|
|||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.env.RandomValuePropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
|
@ -161,4 +162,15 @@ class SpringConfigurationPropertySourcesTests {
|
|||
assertThat(configurationSources.iterator().next().getConfigurationProperty(name).getValue()).isEqualTo("s2");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotAdaptRandomePropertySource() {
|
||||
MutablePropertySources sources = new MutablePropertySources();
|
||||
sources.addFirst(new RandomValuePropertySource());
|
||||
sources.addFirst(new MapPropertySource("test", Collections.singletonMap("a", "b")));
|
||||
Iterator<ConfigurationPropertySource> iterator = new SpringConfigurationPropertySources(sources).iterator();
|
||||
ConfigurationPropertyName name = ConfigurationPropertyName.of("a");
|
||||
assertThat(iterator.next().getConfigurationProperty(name).getValue()).isEqualTo("b");
|
||||
assertThat(iterator.hasNext()).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue