Skip to content

Commit 55afb59

Browse files
authored
Merge pull request #1233 from kazuki43zoo/SPR-14888
Detect invalid configuration for autoGrowCollectionLimit on DataBinder
2 parents dda9762 + 124f212 commit 55afb59

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

spring-context/src/main/java/org/springframework/validation/DataBinder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
* @author Juergen Hoeller
9696
* @author Rob Harrop
9797
* @author Stephane Nicoll
98+
* @author Kazuki Shimizu
9899
* @see #setAllowedFields
99100
* @see #setRequiredFields
100101
* @see #registerCustomEditor
@@ -213,8 +214,12 @@ public boolean isAutoGrowNestedPaths() {
213214
* Specify the limit for array and collection auto-growing.
214215
* <p>Default is 256, preventing OutOfMemoryErrors in case of large indexes.
215216
* Raise this limit if your auto-growing needs are unusually high.
217+
* @see #initBeanPropertyAccess()
218+
* @see org.springframework.beans.BeanWrapper#setAutoGrowCollectionLimit
216219
*/
217220
public void setAutoGrowCollectionLimit(int autoGrowCollectionLimit) {
221+
Assert.state(this.bindingResult == null,
222+
"DataBinder is already initialized - call setAutoGrowCollectionLimit before other configuration methods");
218223
this.autoGrowCollectionLimit = autoGrowCollectionLimit;
219224
}
220225

spring-context/src/test/java/org/springframework/validation/DataBinderTests.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
import java.util.Set;
3737
import java.util.TreeSet;
3838

39+
import org.junit.Rule;
3940
import org.junit.Test;
4041

42+
import org.junit.rules.ExpectedException;
4143
import org.springframework.beans.InvalidPropertyException;
4244
import org.springframework.beans.MutablePropertyValues;
4345
import org.springframework.beans.NotWritablePropertyException;
@@ -69,9 +71,13 @@
6971
* @author Rod Johnson
7072
* @author Juergen Hoeller
7173
* @author Rob Harrop
74+
* @author Kazuki Shimizu
7275
*/
7376
public class DataBinderTests {
7477

78+
@Rule
79+
public ExpectedException expectedException = ExpectedException.none();
80+
7581
@Test
7682
public void testBindingNoErrors() throws Exception {
7783
TestBean rod = new TestBean();
@@ -1982,6 +1988,30 @@ public void testFieldErrorAccessVariations() throws Exception {
19821988
assertEquals("age", binder.getBindingResult().getFieldError("age").getField());
19831989
}
19841990

1991+
@Test // SPR-14888
1992+
public void testSetAutoGrowCollectionLimit() {
1993+
BeanWithIntegerList tb = new BeanWithIntegerList();
1994+
DataBinder binder = new DataBinder(tb);
1995+
binder.setAutoGrowCollectionLimit(257);
1996+
MutablePropertyValues pvs = new MutablePropertyValues();
1997+
pvs.add("integerList[256]", "1");
1998+
1999+
binder.bind(pvs);
2000+
assertEquals(257, tb.getIntegerList().size());
2001+
assertEquals(Integer.valueOf(1), tb.getIntegerList().get(256));
2002+
assertEquals(Integer.valueOf(1), binder.getBindingResult().getFieldValue("integerList[256]"));
2003+
}
2004+
2005+
@Test // SPR-14888
2006+
public void testSetAutoGrowCollectionLimitAfterInitialization() {
2007+
2008+
expectedException.expect(IllegalStateException.class);
2009+
expectedException.expectMessage("DataBinder is already initialized - call setAutoGrowCollectionLimit before other configuration methods");
2010+
2011+
DataBinder binder = new DataBinder(new BeanWithIntegerList());
2012+
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
2013+
binder.setAutoGrowCollectionLimit(257);
2014+
}
19852015

19862016
@SuppressWarnings("unused")
19872017
private static class BeanWithIntegerList {

0 commit comments

Comments
 (0)