From 73d9e0e0edd75ecc74b74dcf7b1bf73c0170c000 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 23 Mar 2022 14:43:57 +0000 Subject: [PATCH] Fix equals when adapt removes trailing characters from element Closes gh-30317 --- .../source/ConfigurationPropertyName.java | 24 ++++++++++++------- .../ConfigurationPropertyNameTests.java | 9 +++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java index dfaa435cf58c..2a6fe30c35ab 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java @@ -448,7 +448,7 @@ private boolean defaultElementEquals(Elements e1, Elements e2, int i) { int i2 = 0; while (i1 < l1) { if (i2 >= l2) { - return false; + return remainderIsNotAlphaNumberic(e1, i, i1); } char ch1 = indexed1 ? e1.charAt(i, i1) : Character.toLowerCase(e1.charAt(i, i1)); char ch2 = indexed2 ? e2.charAt(i, i2) : Character.toLowerCase(e2.charAt(i, i2)); @@ -467,17 +467,23 @@ else if (ch1 != ch2) { } } if (i2 < l2) { - if (indexed2) { + return remainderIsNotAlphaNumberic(e2, i, i2); + } + return true; + } + + private boolean remainderIsNotAlphaNumberic(Elements elements, int element, int index) { + if (elements.getType(element).isIndexed()) { + return false; + } + int length = elements.getLength(element); + do { + char c = Character.toLowerCase(elements.charAt(element, index++)); + if (ElementsParser.isAlphaNumeric(c)) { return false; } - do { - char ch2 = Character.toLowerCase(e2.charAt(i, i2++)); - if (ElementsParser.isAlphaNumeric(ch2)) { - return false; - } - } - while (i2 < l2); } + while (index < length); return true; } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java index 0a59ede1a124..7a13947cddaf 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java @@ -685,6 +685,15 @@ void equalsWhenNameStartsTheSameUsingDashedCompare() { assertThat(n2).isNotEqualTo(n1); } + @Test + void equalsWhenAdaptedNameMatchesDueToRemovalOfTrailingCharacters() { + // gh-30317 + ConfigurationPropertyName name1 = ConfigurationPropertyName.of("example.demo"); + ConfigurationPropertyName name2 = ConfigurationPropertyName.adapt("example.demo$$", '.'); + assertThat(name1).isEqualTo(name2); + assertThat(name2).isEqualTo(name1); + } + @Test void isValidWhenValidShouldReturnTrue() { assertThat(ConfigurationPropertyName.isValid("")).isTrue();