From 55406d91d20e43ba3e01d4b882ef036b16eb798b Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Tue, 21 Aug 2018 12:50:24 -0700 Subject: [PATCH] Consider dash & underscore when equating indexed elements Fixes gh-14136 --- .../source/ConfigurationPropertyName.java | 6 +- .../ConfigurationPropertiesTests.java | 60 +++++++++++++++++++ .../ConfigurationPropertyNameTests.java | 6 ++ 3 files changed, 69 insertions(+), 3 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 e176dd52a5..152833efd3 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 @@ -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; } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java index dab87db0f9..940616e6ae 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java @@ -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 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 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> map; + + public Map> getMap() { + return this.map; + } + + public void setMap(Map> 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; + } + + } + } 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 1d2c30d1d7..17a858a8ce 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 @@ -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