Consider dash & underscore when equating indexed elements
Fixes gh-14136
This commit is contained in:
parent
3d7d1e070f
commit
55406d91d2
|
@ -330,10 +330,10 @@ public final class ConfigurationPropertyName
|
|||
}
|
||||
char ch1 = indexed1 ? e1.charAt(i1) : Character.toLowerCase(e1.charAt(i1));
|
||||
char ch2 = indexed2 ? e2.charAt(i2) : Character.toLowerCase(e2.charAt(i2));
|
||||
if (ch1 == '-' || ch1 == '_') {
|
||||
if (!indexed1 && (ch1 == '-' || ch1 == '_')) {
|
||||
i1++;
|
||||
}
|
||||
else if (ch2 == '-' || ch2 == '_') {
|
||||
else if (!indexed2 && (ch2 == '-' || ch2 == '_')) {
|
||||
i2++;
|
||||
}
|
||||
else if (ch1 != ch2) {
|
||||
|
@ -346,7 +346,7 @@ public final class ConfigurationPropertyName
|
|||
}
|
||||
while (i2 < l2 - offset2) {
|
||||
char ch = e2.charAt(i2++);
|
||||
if (ch != '-' && ch != '_') {
|
||||
if (indexed2 || (ch != '-' && ch != '_')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -772,6 +772,26 @@ public class ConfigurationPropertiesTests {
|
|||
load(PersonProperties.class, "test=boot");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadWhenConfigurationPropertiesContainsMapWithPositiveAndNegativeIntegerKeys() {
|
||||
// gh-14136
|
||||
MutablePropertySources sources = this.context.getEnvironment()
|
||||
.getPropertySources();
|
||||
Map<String, Object> source = new HashMap<>();
|
||||
source.put("test.map.x.[-1].a", "baz");
|
||||
source.put("test.map.x.1.a", "bar");
|
||||
source.put("test.map.x.1.b", 1);
|
||||
sources.addLast(new MapPropertySource("test", source));
|
||||
load(WithIntegerMapProperties.class);
|
||||
WithIntegerMapProperties bean = this.context
|
||||
.getBean(WithIntegerMapProperties.class);
|
||||
Map<Integer, Foo> x = bean.getMap().get("x");
|
||||
assertThat(x.get(-1).getA()).isEqualTo("baz");
|
||||
assertThat(x.get(-1).getB()).isEqualTo(0);
|
||||
assertThat(x.get(1).getA()).isEqualTo("bar");
|
||||
assertThat(x.get(1).getB()).isEqualTo(1);
|
||||
}
|
||||
|
||||
private AnnotationConfigApplicationContext load(Class<?> configuration,
|
||||
String... inlinedProperties) {
|
||||
return load(new Class<?>[] { configuration }, inlinedProperties);
|
||||
|
@ -1546,6 +1566,22 @@ public class ConfigurationPropertiesTests {
|
|||
|
||||
}
|
||||
|
||||
@EnableConfigurationProperties
|
||||
@ConfigurationProperties(prefix = "test")
|
||||
static class WithIntegerMapProperties {
|
||||
|
||||
private Map<String, Map<Integer, Foo>> map;
|
||||
|
||||
public Map<String, Map<Integer, Foo>> getMap() {
|
||||
return this.map;
|
||||
}
|
||||
|
||||
public void setMap(Map<String, Map<Integer, Foo>> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableConfigurationProperties
|
||||
@ConfigurationProperties(prefix = "com.example", ignoreUnknownFields = false)
|
||||
static class SimplePrefixedProperties {
|
||||
|
@ -1758,4 +1794,28 @@ public class ConfigurationPropertiesTests {
|
|||
|
||||
}
|
||||
|
||||
static class Foo {
|
||||
|
||||
private String a;
|
||||
|
||||
private int b;
|
||||
|
||||
public String getA() {
|
||||
return this.a;
|
||||
}
|
||||
|
||||
public void setA(String a) {
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public int getB() {
|
||||
return this.b;
|
||||
}
|
||||
|
||||
public void setB(int b) {
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -558,6 +558,10 @@ public class ConfigurationPropertyNameTests {
|
|||
ConfigurationPropertyName n09 = ConfigurationPropertyName.of("foo");
|
||||
ConfigurationPropertyName n10 = ConfigurationPropertyName.of("fo");
|
||||
ConfigurationPropertyName n11 = ConfigurationPropertyName.adapt("foo.BaR", '.');
|
||||
ConfigurationPropertyName n12 = ConfigurationPropertyName.of("f-o-o[b-a-r]");
|
||||
ConfigurationPropertyName n13 = ConfigurationPropertyName.of("f-o-o[b-a-r--]");
|
||||
ConfigurationPropertyName n14 = ConfigurationPropertyName.of("[1]");
|
||||
ConfigurationPropertyName n15 = ConfigurationPropertyName.of("[-1]");
|
||||
assertThat(n01.hashCode()).isEqualTo(n02.hashCode());
|
||||
assertThat(n01.hashCode()).isEqualTo(n02.hashCode());
|
||||
assertThat(n01.hashCode()).isEqualTo(n03.hashCode());
|
||||
|
@ -574,6 +578,8 @@ public class ConfigurationPropertyNameTests {
|
|||
assertThat((Object) n07).isNotEqualTo(n08);
|
||||
assertThat((Object) n09).isNotEqualTo(n10);
|
||||
assertThat((Object) n10).isNotEqualTo(n09);
|
||||
assertThat((Object) n12).isNotEqualTo(n13);
|
||||
assertThat((Object) n14).isNotEqualTo(n15);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue