Skip to content

Commit 0588e98

Browse files
committed
Don't adapt RandomPropertySource
Stop adapting `RandomPropertySource` to `ConfigurationPropertySource` since it's not useful as a binding source. Closes gh-21659
1 parent 10ab477 commit 0588e98

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySources.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Iterator;
2222
import java.util.Map;
2323
import java.util.NoSuchElementException;
24+
import java.util.Random;
2425
import java.util.function.Function;
2526

2627
import org.springframework.core.env.ConfigurableEnvironment;
@@ -123,10 +124,16 @@ private void push(ConfigurableEnvironment environment) {
123124
}
124125

125126
private boolean isIgnored(PropertySource<?> candidate) {
126-
return (candidate instanceof StubPropertySource
127+
return (isRandomPropertySource(candidate) || candidate instanceof StubPropertySource
127128
|| candidate instanceof ConfigurationPropertySourcesPropertySource);
128129
}
129130

131+
private boolean isRandomPropertySource(PropertySource<?> candidate) {
132+
Object source = candidate.getSource();
133+
return (source instanceof Random) || (source instanceof PropertySource<?>
134+
&& ((PropertySource<?>) source).getSource() instanceof Random);
135+
}
136+
130137
}
131138

132139
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySourcesTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.junit.jupiter.api.Test;
2323

24+
import org.springframework.boot.env.RandomValuePropertySource;
2425
import org.springframework.core.env.Environment;
2526
import org.springframework.core.env.MapPropertySource;
2627
import org.springframework.core.env.MutablePropertySources;
@@ -161,4 +162,15 @@ void shouldTrackWhenSourceHasIdenticalName() {
161162
assertThat(configurationSources.iterator().next().getConfigurationProperty(name).getValue()).isEqualTo("s2");
162163
}
163164

165+
@Test
166+
void shouldNotAdaptRandomePropertySource() {
167+
MutablePropertySources sources = new MutablePropertySources();
168+
sources.addFirst(new RandomValuePropertySource());
169+
sources.addFirst(new MapPropertySource("test", Collections.singletonMap("a", "b")));
170+
Iterator<ConfigurationPropertySource> iterator = new SpringConfigurationPropertySources(sources).iterator();
171+
ConfigurationPropertyName name = ConfigurationPropertyName.of("a");
172+
assertThat(iterator.next().getConfigurationProperty(name).getValue()).isEqualTo("b");
173+
assertThat(iterator.hasNext()).isFalse();
174+
}
175+
164176
}

0 commit comments

Comments
 (0)