Skip to content

Commit aa4f09d

Browse files
committed
Refine decision to create object for constructor injection
Closes gh-31488
1 parent d7e6b79 commit aa4f09d

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.reflect.Field;
2323
import java.util.ArrayList;
2424
import java.util.Arrays;
25+
import java.util.Collection;
2526
import java.util.Collections;
2627
import java.util.HashMap;
2728
import java.util.HashSet;
@@ -947,7 +948,7 @@ private Object createObject(ResolvableType objectType, String nestedPath, ValueR
947948
Class<?> paramType = paramTypes[i];
948949
Object value = valueResolver.resolveValue(paramPath, paramType);
949950

950-
if (value == null && !BeanUtils.isSimpleValueType(param.nestedIfOptional().getNestedParameterType())) {
951+
if (value == null && shouldCreateObject(param)) {
951952
ResolvableType type = ResolvableType.forMethodParameter(param);
952953
args[i] = createObject(type, paramPath + ".", valueResolver);
953954
}
@@ -1007,6 +1008,12 @@ private Object createObject(ResolvableType objectType, String nestedPath, ValueR
10071008
return (isOptional && !nestedPath.isEmpty() ? Optional.ofNullable(result) : result);
10081009
}
10091010

1011+
private static boolean shouldCreateObject(MethodParameter param) {
1012+
Class<?> type = param.nestedIfOptional().getNestedParameterType();
1013+
return !(BeanUtils.isSimpleValueType(type) ||
1014+
Collection.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type) || type.isArray());
1015+
}
1016+
10101017
private void validateConstructorArgument(
10111018
Class<?> constructorClass, String nestedPath, String name, @Nullable Object value) {
10121019

0 commit comments

Comments
 (0)