Skip to content

Commit 425ce86

Browse files
committed
Consistently expose map key quotes
Closes gh-36765 (cherry picked from commit d3152c1)
1 parent 85c578b commit 425ce86

5 files changed

Lines changed: 13 additions & 3 deletions

File tree

spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -967,8 +967,8 @@ private PropertyTokenHolder getPropertyNameTokens(String propertyName) {
967967
actualName = propertyName.substring(0, keyStart);
968968
}
969969
String key = propertyName.substring(keyStart + PROPERTY_KEY_PREFIX.length(), keyEnd);
970-
if (key.length() > 1 && (key.startsWith("'") && key.endsWith("'")) ||
971-
(key.startsWith("\"") && key.endsWith("\""))) {
970+
if (key.length() > 1 && ((key.startsWith("'") && key.endsWith("'")) ||
971+
(key.startsWith("\"") && key.endsWith("\"")))) {
972972
key = key.substring(1, key.length() - 1);
973973
}
974974
keys.add(key);

spring-beans/src/main/java/org/springframework/beans/PropertyAccessorUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ public static String canonicalPropertyName(@Nullable String propertyName) {
154154
PropertyAccessor.PROPERTY_KEY_SUFFIX, keyStart + PropertyAccessor.PROPERTY_KEY_PREFIX.length());
155155
if (keyEnd != -1) {
156156
String key = sb.substring(keyStart + PropertyAccessor.PROPERTY_KEY_PREFIX.length(), keyEnd);
157-
if ((key.startsWith("'") && key.endsWith("'")) || (key.startsWith("\"") && key.endsWith("\""))) {
157+
if (key.length() > 1 && ((key.startsWith("'") && key.endsWith("'")) ||
158+
(key.startsWith("\"") && key.endsWith("\"")))) {
158159
sb.delete(keyStart + 1, keyStart + 2);
159160
sb.delete(keyEnd - 2, keyEnd - 1);
160161
keyEnd = keyEnd - 2;

spring-beans/src/test/java/org/springframework/beans/AbstractPropertyAccessorTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,8 @@ void getAndSetIndexedProperties() {
14131413
assertThat(accessor.getPropertyValue("map[key5[foo]].name")).isEqualTo("name8");
14141414
assertThat(accessor.getPropertyValue("map['key5[foo]'].name")).isEqualTo("name8");
14151415
assertThat(accessor.getPropertyValue("map[\"key5[foo]\"].name")).isEqualTo("name8");
1416+
assertThat(accessor.getPropertyValue("map['].name")).isEqualTo("name9");
1417+
assertThat(accessor.getPropertyValue("map[\"].name")).isEqualTo("name9");
14161418
assertThat(accessor.getPropertyValue("iterableMap[key1].name")).isEqualTo("nameC");
14171419
assertThat(accessor.getPropertyValue("iterableMap[key2][0].name")).isEqualTo("nameA");
14181420
assertThat(accessor.getPropertyValue("iterableMap[key2][1].name")).isEqualTo("nameB");

spring-beans/src/test/java/org/springframework/beans/PropertyAccessorUtilsTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ void canonicalPropertyName() {
7979
assertThat(PropertyAccessorUtils.canonicalPropertyName("map[key1].name")).isEqualTo("map[key1].name");
8080
assertThat(PropertyAccessorUtils.canonicalPropertyName("map['key1'].name")).isEqualTo("map[key1].name");
8181
assertThat(PropertyAccessorUtils.canonicalPropertyName("map[\"key1\"].name")).isEqualTo("map[key1].name");
82+
assertThat(PropertyAccessorUtils.canonicalPropertyName("map['key1]")).isEqualTo("map['key1]");
83+
assertThat(PropertyAccessorUtils.canonicalPropertyName("map[\"key1]")).isEqualTo("map[\"key1]");
84+
assertThat(PropertyAccessorUtils.canonicalPropertyName("map[']")).isEqualTo("map[']");
85+
assertThat(PropertyAccessorUtils.canonicalPropertyName("map[\"]")).isEqualTo("map[\"]");
8286
}
8387

8488
@Test

spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/IndexedTestBean.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public void populate() {
7676
TestBean tb6 = new TestBean("name6", 0);
7777
TestBean tb7 = new TestBean("name7", 0);
7878
TestBean tb8 = new TestBean("name8", 0);
79+
TestBean tb9 = new TestBean("name9", 0);
7980
TestBean tbA = new TestBean("nameA", 0);
8081
TestBean tbB = new TestBean("nameB", 0);
8182
TestBean tbC = new TestBean("nameC", 0);
@@ -104,6 +105,8 @@ public void populate() {
104105
list.add(tbY);
105106
this.map.put("key4", list);
106107
this.map.put("key5[foo]", tb8);
108+
this.map.put("'", tb9);
109+
this.map.put("\"", tb9);
107110
this.myTestBeans = new MyTestBeans(tbZ);
108111
}
109112

0 commit comments

Comments
 (0)