BeanWrapper avoids StringIndexOutOfBoundsException for incompletely quoted keys

Issue: SPR-14293
(cherry picked from commit cf0a0cd)
This commit is contained in:
Juergen Hoeller
2016-05-30 15:15:27 +02:00
parent 62199e82ef
commit 1d0c305a25
2 changed files with 18 additions and 3 deletions

View File

@@ -800,7 +800,7 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA
/**
* Recursively navigate to return a property accessor for the nested property path.
* @param propertyPath property property path, which may be nested
* @param propertyPath property path, which may be nested
* @return a property accessor for the target bean
*/
@SuppressWarnings("unchecked") // avoid nested generic
@@ -942,7 +942,8 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA
actualName = propertyName.substring(0, keyStart);
}
String key = propertyName.substring(keyStart + PROPERTY_KEY_PREFIX.length(), keyEnd);
if ((key.startsWith("'") && key.endsWith("'")) || (key.startsWith("\"") && key.endsWith("\""))) {
if (key.length() > 1 && (key.startsWith("'") && key.endsWith("'")) ||
(key.startsWith("\"") && key.endsWith("\""))) {
key = key.substring(1, key.length() - 1);
}
keys.add(key);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -194,6 +194,19 @@ public class BeanWrapperTests extends AbstractPropertyAccessorTests {
assertEquals("x", accessor.getPropertyValue("object.name"));
}
@Test
public void incompletelyQuotedKeyLeadsToPropertyException() {
TestBean target = new TestBean();
try {
BeanWrapper accessor = createAccessor(target);
accessor.setPropertyValue("[']", "foobar");
fail("Should throw exception on invalid property");
}
catch (NotWritablePropertyException ex) {
assertNull(ex.getPossibleMatches());
}
}
@SuppressWarnings("unused")
private static class GetterBean {
@@ -212,6 +225,7 @@ public class BeanWrapperTests extends AbstractPropertyAccessorTests {
}
}
@SuppressWarnings("unused")
private static class IntelliBean {