Commit b1656be3 authored by Phillip Webb's avatar Phillip Webb

Fix possible binder IndexOutOfBoundsException

Update RelaxedDataBinder.extendCollectionIfNecessary to use the current
index when checking if the path node is an array.

Fixes gh-5635
parent 1412eaa0
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-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.
......@@ -343,7 +343,7 @@ public class RelaxedDataBinder extends DataBinder {
return;
}
Object extend = new LinkedHashMap<String, Object>();
if (!elementDescriptor.isMap() && path.isArrayIndex(index + 1)) {
if (!elementDescriptor.isMap() && path.isArrayIndex(index)) {
extend = new ArrayList<Object>();
}
wrapper.setPropertyValue(path.prefix(index + 1), extend);
......
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-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.
......@@ -648,6 +648,16 @@ public class RelaxedDataBinderTests {
assertEquals("boo", target.getFooBaz());
}
@Test
public void testIndexBounds() throws Exception {
VanillaTarget target = new VanillaTarget();
RelaxedDataBinder binder = getBinder(target, "test");
MutablePropertyValues values = new MutablePropertyValues();
values.add("test.objects[0]", "teststring");
binder.bind(values);
assertEquals("teststring", target.getObjects().get(0));
}
private void doTestBindCaseInsensitiveEnums(VanillaTarget target) throws Exception {
BindingResult result = bind(target, "bingo: THIS");
assertThat(result.getErrorCount(), equalTo(0));
......@@ -1006,6 +1016,8 @@ public class RelaxedDataBinderTests {
private List<Bingo> bingos;
private List<Object> objects;
public char[] getBar() {
return this.bar;
}
......@@ -1061,6 +1073,15 @@ public class RelaxedDataBinderTests {
public void setBingos(List<Bingo> bingos) {
this.bingos = bingos;
}
public List<Object> getObjects() {
return this.objects;
}
public void setObjects(List<Object> objects) {
this.objects = objects;
}
}
enum Bingo {
......@@ -1081,4 +1102,5 @@ public class RelaxedDataBinderTests {
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment