Skip to content

Correctly parse property name in path "map[key[foo]]" #21855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ private PropertyTokenHolder getPropertyNameTokens(String propertyName) {
int keyStart = propertyName.indexOf(PROPERTY_KEY_PREFIX, searchIndex);
searchIndex = -1;
if (keyStart != -1) {
int keyEnd = propertyName.indexOf(PROPERTY_KEY_SUFFIX, keyStart + PROPERTY_KEY_PREFIX.length());
int keyEnd = getPropertyNameKeyEnd(propertyName, keyStart + PROPERTY_KEY_PREFIX.length());
if (keyEnd != -1) {
if (actualName == null) {
actualName = propertyName.substring(0, keyStart);
Expand All @@ -958,6 +958,29 @@ private PropertyTokenHolder getPropertyNameTokens(String propertyName) {
return tokens;
}

private static int getPropertyNameKeyEnd(String propertyName, int startIndex) {
int unclosedPrefixes = 0;
int length = propertyName.length();
for (int i = startIndex; i < length; i++) {
switch (propertyName.charAt(i)) {
case PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR:
// The property name contains opening prefix(es)
unclosedPrefixes++;
break;
case PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR:
if (unclosedPrefixes == 0) {
// No unclosed prefix(es) in the property name (left), this is the suffix we are looking for
return i;
} else {
// This suffix does not close the initial prefix, but one that occurred within the property name
unclosedPrefixes--;
}
break;
}
}
return -1;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder(getClass().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,7 @@ public void getAndSetIndexedProperties() {
TestBean tb7 = ((TestBean) target.getSet().toArray()[1]);
TestBean tb4 = ((TestBean) target.getMap().get("key1"));
TestBean tb5 = ((TestBean) target.getMap().get("key.3"));
TestBean tb8 = ((TestBean) target.getMap().get("key5[foo]"));
assertEquals("name0", tb0.getName());
assertEquals("name1", tb1.getName());
assertEquals("name2", tb2.getName());
Expand All @@ -1607,6 +1608,7 @@ public void getAndSetIndexedProperties() {
assertEquals("name7", tb7.getName());
assertEquals("name4", tb4.getName());
assertEquals("name5", tb5.getName());
assertEquals("name8", tb8.getName());
assertEquals("name0", accessor.getPropertyValue("array[0].name"));
assertEquals("name1", accessor.getPropertyValue("array[1].name"));
assertEquals("name2", accessor.getPropertyValue("list[0].name"));
Expand All @@ -1619,6 +1621,9 @@ public void getAndSetIndexedProperties() {
assertEquals("name5", accessor.getPropertyValue("map[\"key.3\"].name"));
assertEquals("nameX", accessor.getPropertyValue("map[key4][0].name"));
assertEquals("nameY", accessor.getPropertyValue("map[key4][1].name"));
assertEquals("name8", accessor.getPropertyValue("map[key5[foo]].name"));
assertEquals("name8", accessor.getPropertyValue("map['key5[foo]'].name"));
assertEquals("name8", accessor.getPropertyValue("map[\"key5[foo]\"].name"));

MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("array[0].name", "name5");
Expand All @@ -1631,6 +1636,7 @@ public void getAndSetIndexedProperties() {
pvs.add("map['key.3'].name", "name0");
pvs.add("map[key4][0].name", "nameA");
pvs.add("map[key4][1].name", "nameB");
pvs.add("map[key5[foo]].name", "name10");
accessor.setPropertyValues(pvs);
assertEquals("name5", tb0.getName());
assertEquals("name4", tb1.getName());
Expand All @@ -1648,6 +1654,7 @@ public void getAndSetIndexedProperties() {
assertEquals("name0", accessor.getPropertyValue("map['key.3'].name"));
assertEquals("nameA", accessor.getPropertyValue("map[key4][0].name"));
assertEquals("nameB", accessor.getPropertyValue("map[key4][1].name"));
assertEquals("name10", accessor.getPropertyValue("map[key5[foo]].name"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void populate() {
TestBean tb5 = new TestBean("name5", 0);
TestBean tb6 = new TestBean("name6", 0);
TestBean tb7 = new TestBean("name7", 0);
TestBean tb8 = new TestBean("name8", 0);
TestBean tbX = new TestBean("nameX", 0);
TestBean tbY = new TestBean("nameY", 0);
this.array = new TestBean[] {tb0, tb1};
Expand All @@ -83,6 +84,7 @@ public void populate() {
list.add(tbX);
list.add(tbY);
this.map.put("key4", list);
this.map.put("key5[foo]", tb8);
}


Expand Down