Skip to content

Commit bb97ca3

Browse files
committedNov 25, 2009
added configurable "autoGrowNestedPaths" property to DataBinder (SPR-6430)
1 parent 9a2f9cc commit bb97ca3

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed
 

‎org.springframework.beans/src/main/java/org/springframework/beans/BeanWrapper.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ public interface BeanWrapper extends ConfigurablePropertyAccessor {
7979
PropertyDescriptor getPropertyDescriptor(String propertyName) throws InvalidPropertyException;
8080

8181
/**
82-
* Set if this BeanWrapper should attempt to "auto-grow" a nested path that contains a null value.
83-
* <p>If true, a null path location will be populated with a default object value and traversed
82+
* Set whether this BeanWrapper should attempt to "auto-grow" a nested path that contains a null value.
83+
* <p>If "true", a null path location will be populated with a default object value and traversed
8484
* instead of resulting in a {@link NullValueInNestedPathException}. Turning this flag on also
85-
* enables auto-growth of collection elements when an index that is out of bounds is accessed.
86-
* <p>Default is false.
85+
* enables auto-growth of collection elements when accessing an out-of-bounds index.
86+
* <p>Default is "false" on a plain BeanWrapper.
8787
*/
8888
void setAutoGrowNestedPaths(boolean autoGrowNestedPaths);
8989

‎org.springframework.context/src/main/java/org/springframework/validation/BeanPropertyBindingResult.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public class BeanPropertyBindingResult extends AbstractPropertyBindingResult imp
4444

4545
private final Object target;
4646

47+
private final boolean autoGrowNestedPaths;
48+
4749
private transient BeanWrapper beanWrapper;
4850

4951

@@ -53,8 +55,19 @@ public class BeanPropertyBindingResult extends AbstractPropertyBindingResult imp
5355
* @param objectName the name of the target object
5456
*/
5557
public BeanPropertyBindingResult(Object target, String objectName) {
58+
this(target, objectName, true);
59+
}
60+
61+
/**
62+
* Creates a new instance of the {@link BeanPropertyBindingResult} class.
63+
* @param target the target bean to bind onto
64+
* @param objectName the name of the target object
65+
* @param autoGrowNestedPaths whether to "auto-grow" a nested path that contains a null value
66+
*/
67+
public BeanPropertyBindingResult(Object target, String objectName, boolean autoGrowNestedPaths) {
5668
super(objectName);
5769
this.target = target;
70+
this.autoGrowNestedPaths = autoGrowNestedPaths;
5871
}
5972

6073

@@ -73,7 +86,7 @@ public final ConfigurablePropertyAccessor getPropertyAccessor() {
7386
if (this.beanWrapper == null) {
7487
this.beanWrapper = createBeanWrapper();
7588
this.beanWrapper.setExtractOldValueForEditor(true);
76-
this.beanWrapper.setAutoGrowNestedPaths(true);
89+
this.beanWrapper.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
7790
}
7891
return this.beanWrapper;
7992
}

‎org.springframework.context/src/main/java/org/springframework/validation/DataBinder.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
125125

126126
private boolean ignoreInvalidFields = false;
127127

128+
private boolean autoGrowNestedPaths = true;
129+
128130
private String[] allowedFields;
129131

130132
private String[] disallowedFields;
@@ -182,7 +184,7 @@ public String getObjectName() {
182184
public void initBeanPropertyAccess() {
183185
Assert.isNull(this.bindingResult,
184186
"DataBinder is already initialized - call initBeanPropertyAccess before any other configuration methods");
185-
this.bindingResult = new BeanPropertyBindingResult(getTarget(), getObjectName());
187+
this.bindingResult = new BeanPropertyBindingResult(getTarget(), getObjectName(), isAutoGrowNestedPaths());
186188
if (this.conversionService != null) {
187189
this.bindingResult.initConversion(this.conversionService);
188190
}
@@ -330,6 +332,25 @@ public boolean isIgnoreInvalidFields() {
330332
return this.ignoreInvalidFields;
331333
}
332334

335+
/**
336+
* Set whether this binder should attempt to "auto-grow" a nested path that contains a null value.
337+
* <p>If "true", a null path location will be populated with a default object value and traversed
338+
* instead of resulting in an exception. This flag also enables auto-growth of collection elements
339+
* when accessing an out-of-bounds index.
340+
* <p>Default is "true" on a standard DataBinder.
341+
* @see org.springframework.beans.BeanWrapper#setAutoGrowNestedPaths
342+
*/
343+
public void setAutoGrowNestedPaths(boolean autoGrowNestedPaths) {
344+
this.autoGrowNestedPaths = autoGrowNestedPaths;
345+
}
346+
347+
/**
348+
* Return whether "auto-growing" of nested paths has been activated.
349+
*/
350+
public boolean isAutoGrowNestedPaths() {
351+
return this.autoGrowNestedPaths;
352+
}
353+
333354
/**
334355
* Register fields that should be allowed for binding. Default is all
335356
* fields. Restrict this for example to avoid unwanted modifications

0 commit comments

Comments
 (0)
Please sign in to comment.