From 09a47c9aad4f424151da68440c5170a2a35b6da5 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 2 Jun 2020 10:53:12 -0700 Subject: [PATCH] Fix containsDescendantOf for random sources Fix the `containsDescendantOf` logic for random property sources to ensure that ancestors are correctly matched. Closes gh-21654 --- .../SpringConfigurationPropertySource.java | 2 +- ...pringConfigurationPropertySourceTests.java | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySource.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySource.java index f83af8c7d28..7074b83f17c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySource.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySource.java @@ -190,7 +190,7 @@ class SpringConfigurationPropertySource implements ConfigurationPropertySource { } private static ConfigurationPropertyState containsDescendantOfForRandom(ConfigurationPropertyName name) { - if (name.isAncestorOf(RANDOM) || name.equals(RANDOM)) { + if (RANDOM.isAncestorOf(name) || name.equals(RANDOM)) { return ConfigurationPropertyState.PRESENT; } return ConfigurationPropertyState.ABSENT; diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySourceTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySourceTests.java index 722718b0ac4..2caf8337333 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySourceTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySourceTests.java @@ -21,6 +21,7 @@ import java.util.Map; import org.junit.jupiter.api.Test; +import org.springframework.boot.env.RandomValuePropertySource; import org.springframework.boot.origin.Origin; import org.springframework.boot.origin.OriginLookup; import org.springframework.core.env.MapPropertySource; @@ -148,6 +149,30 @@ class SpringConfigurationPropertySourceTests { .isInstanceOf(IterableConfigurationPropertySource.class); } + @Test + void containsDescendantOfWhenRandomSourceAndRandomPropertyReturnsPresent() { + SpringConfigurationPropertySource source = SpringConfigurationPropertySource + .from(new RandomValuePropertySource()); + assertThat(source.containsDescendantOf(ConfigurationPropertyName.of("random"))) + .isEqualTo(ConfigurationPropertyState.PRESENT); + } + + @Test + void containsDescendantOfWhenRandomSourceAndRandomPrefixedPropertyReturnsPresent() { + SpringConfigurationPropertySource source = SpringConfigurationPropertySource + .from(new RandomValuePropertySource()); + assertThat(source.containsDescendantOf(ConfigurationPropertyName.of("random.something"))) + .isEqualTo(ConfigurationPropertyState.PRESENT); + } + + @Test + void containsDescendantOfWhenRandomSourceAndNonRandomPropertyReturnsAbsent() { + SpringConfigurationPropertySource source = SpringConfigurationPropertySource + .from(new RandomValuePropertySource()); + assertThat(source.containsDescendantOf(ConfigurationPropertyName.of("abandon.something"))) + .isEqualTo(ConfigurationPropertyState.ABSENT); + } + /** * Test {@link PropertySource} that's also an {@link OriginLookup}. *