Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public void initDirectFieldAccess() {
*/
protected AbstractPropertyBindingResult createDirectFieldBindingResult() {
DirectFieldBindingResult result = new DirectFieldBindingResult(getTarget(),
getObjectName(), isAutoGrowNestedPaths());
getObjectName(), isAutoGrowNestedPaths(), getAutoGrowCollectionLimit());

if (this.conversionService != null) {
result.initConversion(this.conversionService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.jspecify.annotations.Nullable;

import org.springframework.beans.ConfigurablePropertyAccessor;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.PropertyAccessorFactory;

/**
Expand All @@ -41,6 +42,8 @@ public class DirectFieldBindingResult extends AbstractPropertyBindingResult {

private final boolean autoGrowNestedPaths;

private final int autoGrowCollectionLimit;

private transient @Nullable ConfigurablePropertyAccessor directFieldAccessor;


Expand All @@ -60,9 +63,24 @@ public DirectFieldBindingResult(@Nullable Object target, String objectName) {
* @param autoGrowNestedPaths whether to "auto-grow" a nested path that contains a null value
*/
public DirectFieldBindingResult(@Nullable Object target, String objectName, boolean autoGrowNestedPaths) {
this(target, objectName, autoGrowNestedPaths, Integer.MAX_VALUE);
}

/**
* Create a new {@code DirectFieldBindingResult} for the given target.
* @param target the target object to bind onto
* @param objectName the name of the target object
* @param autoGrowNestedPaths whether to "auto-grow" a nested path that contains a null value
* @param autoGrowCollectionLimit the limit for array and collection auto-growing
* @since 7.1
*/
public DirectFieldBindingResult(@Nullable Object target, String objectName,
boolean autoGrowNestedPaths, int autoGrowCollectionLimit) {

super(objectName);
this.target = target;
this.autoGrowNestedPaths = autoGrowNestedPaths;
this.autoGrowCollectionLimit = autoGrowCollectionLimit;
}


Expand All @@ -82,6 +100,7 @@ public final ConfigurablePropertyAccessor getPropertyAccessor() {
this.directFieldAccessor = createDirectFieldAccessor();
this.directFieldAccessor.setExtractOldValueForEditor(true);
this.directFieldAccessor.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
((DirectFieldAccessor) this.directFieldAccessor).setAutoGrowCollectionLimit(this.autoGrowCollectionLimit);
}
return this.directFieldAccessor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
package org.springframework.validation;

import java.beans.PropertyEditorSupport;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;

import org.springframework.beans.InvalidPropertyException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.NotWritablePropertyException;
import org.springframework.beans.NullValueInNestedPathException;
Expand Down Expand Up @@ -134,6 +136,25 @@ void nestedBindingWithDisabledAutoGrow() {
binder.bind(pvs));
}

@Test
void directFieldAccessHonorsDefaultAutoGrowCollectionLimit() {
FieldAccessForm target = new FieldAccessForm();
DataBinder binder = new DataBinder(target);
binder.initDirectFieldAccess();

MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("items[255].name", "value");
binder.bind(pvs);

assertThat(target.items).hasSize(256);
assertThat(target.items.get(255).name).isEqualTo("value");

MutablePropertyValues outOfBounds = new MutablePropertyValues();
outOfBounds.add("items[256].name", "too-far");
assertThatExceptionOfType(InvalidPropertyException.class).isThrownBy(() ->
binder.bind(outOfBounds));
}

@Test
void bindingWithErrorsAndCustomEditors() {
FieldAccessBean rod = new FieldAccessBean();
Expand Down Expand Up @@ -176,4 +197,16 @@ public String getAsText() {
assertThat(tb.getSpouse()).isNotNull();
});
}


static class FieldAccessForm {

public List<FieldAccessItem> items;
}


static class FieldAccessItem {

public String name;
}
}