Browse Source

BeanWrapper avoids StringIndexOutOfBoundsException for incompletely quoted keys

Issue: SPR-14293
(cherry picked from commit cf0a0cd)
pull/1086/head
Juergen Hoeller 10 years ago
parent
commit
1d0c305a25
  1. 5
      spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java
  2. 16
      spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java

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

@ -800,7 +800,7 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA @@ -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 @@ -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);

16
spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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 { @@ -212,6 +225,7 @@ public class BeanWrapperTests extends AbstractPropertyAccessorTests {
}
}
@SuppressWarnings("unused")
private static class IntelliBean {

Loading…
Cancel
Save