From 3153117429756fc94489c1c9350b2149435a3b3a Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 1 May 2017 22:03:05 -0700 Subject: [PATCH] Fix ConfigurationPropertyName ancestor bug Fix an issue with `ConfigurationPropertyName` where the `isAncesorOf` method would not work with `ConfigurationPropertyName.EMPTY` See gh-9000 --- .../properties/source/ConfigurationPropertyName.java | 3 +++ .../source/ConfigurationPropertyNameTests.java | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java b/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java index c0f19d972c..a7fa9d8a69 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java @@ -145,6 +145,9 @@ public final class ConfigurationPropertyName * @return {@code true} if this name is an ancestor */ public boolean isAncestorOf(ConfigurationPropertyName name) { + if (this.equals(EMPTY)) { + return true; + } ConfigurationPropertyName candidate = (name == null ? null : name.getParent()); while (candidate != null) { if (candidate.equals(this)) { diff --git a/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java b/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java index a0df87b141..695995515a 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java @@ -274,6 +274,15 @@ public class ConfigurationPropertyNameTests { assertThat(grandchild.isAncestorOf(parent)).isFalse(); } + @Test + public void isAncestorOfWhenRootReturnTrue() throws Exception { + ConfigurationPropertyName parent = ConfigurationPropertyName.of(""); + ConfigurationPropertyName grandchild = ConfigurationPropertyName + .of("foo.bar.baz"); + assertThat(parent.isAncestorOf(grandchild)).isTrue(); + assertThat(grandchild.isAncestorOf(parent)).isFalse(); + } + @Test public void appendWhenNotIndexedShouldAppendWithDot() throws Exception { ConfigurationPropertyName name = ConfigurationPropertyName.of("foo");