diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySources.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySources.java index 09ccf172359..6f9051965f5 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySources.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySources.java @@ -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 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); + } + } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySourcesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySourcesTests.java index 6fed843b7c8..4899ad64373 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySourcesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySourcesTests.java @@ -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 iterator = new SpringConfigurationPropertySources(sources).iterator(); + ConfigurationPropertyName name = ConfigurationPropertyName.of("a"); + assertThat(iterator.next().getConfigurationProperty(name).getValue()).isEqualTo("b"); + assertThat(iterator.hasNext()).isFalse(); + } + }