Skip to content

Commit 2b89b87

Browse files
committed
Merge pull request #34804 from eydunn
* gh-34804: Polish "Fix asymmetry of equals when element has trailing dashes" Fix asymmetry of equals when element has trailing dashes Closes gh-34804
2 parents 7b565bd + b06e7e6 commit 2b89b87

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -406,7 +406,7 @@ private boolean dashIgnoringElementEquals(Elements e1, Elements e2, int i) {
406406
int i2 = 0;
407407
while (i1 < l1) {
408408
if (i2 >= l2) {
409-
return false;
409+
return remainderIsDashes(e1, i, i1);
410410
}
411411
char ch1 = e1.charAt(i, i1);
412412
char ch2 = e2.charAt(i, i2);
@@ -487,6 +487,21 @@ private boolean remainderIsNotAlphanumeric(Elements elements, int element, int i
487487
return true;
488488
}
489489

490+
private boolean remainderIsDashes(Elements elements, int element, int index) {
491+
if (elements.getType(element).isIndexed()) {
492+
return false;
493+
}
494+
int length = elements.getLength(element);
495+
do {
496+
char c = Character.toLowerCase(elements.charAt(element, index++));
497+
if (c != '-') {
498+
return false;
499+
}
500+
}
501+
while (index < length);
502+
return true;
503+
}
504+
490505
@Override
491506
public int hashCode() {
492507
int hashCode = this.hashCode;

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -693,15 +693,22 @@ void equalsWhenNameStartsTheSameUsingDashedCompare() {
693693
assertThat(n2).isNotEqualTo(n1);
694694
}
695695

696-
@Test
697-
void equalsWhenAdaptedNameMatchesDueToRemovalOfTrailingCharacters() {
698-
// gh-30317
696+
@Test // gh-30317
697+
void equalsWhenAdaptedNameMatchesDueToRemovalOfTrailingNonUniformCharacters() {
699698
ConfigurationPropertyName name1 = ConfigurationPropertyName.of("example.demo");
700699
ConfigurationPropertyName name2 = ConfigurationPropertyName.adapt("example.demo$$", '.');
701700
assertThat(name1).isEqualTo(name2);
702701
assertThat(name2).isEqualTo(name1);
703702
}
704703

704+
@Test // gh-34804
705+
void equalsSymmetricWhenNameMatchesDueToIgnoredTrailingDashes() {
706+
ConfigurationPropertyName n1 = ConfigurationPropertyName.of("example.demo");
707+
ConfigurationPropertyName n2 = ConfigurationPropertyName.of("example.demo--");
708+
assertThat(n2).isEqualTo(n1);
709+
assertThat(n1).isEqualTo(n2);
710+
}
711+
705712
@Test
706713
void isValidWhenValidShouldReturnTrue() {
707714
assertThat(ConfigurationPropertyName.isValid("")).isTrue();

0 commit comments

Comments
 (0)