Skip to content

Commit

Permalink
Refine decision to create object for constructor injection
Browse files Browse the repository at this point in the history
Closes gh-31488
  • Loading branch information
rstoyanchev committed Oct 25, 2023
1 parent d7e6b79 commit aa4f09d
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -947,7 +948,7 @@ private Object createObject(ResolvableType objectType, String nestedPath, ValueR
Class<?> paramType = paramTypes[i];
Object value = valueResolver.resolveValue(paramPath, paramType);

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

private static boolean shouldCreateObject(MethodParameter param) {
Class<?> type = param.nestedIfOptional().getNestedParameterType();
return !(BeanUtils.isSimpleValueType(type) ||
Collection.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type) || type.isArray());
}

private void validateConstructorArgument(
Class<?> constructorClass, String nestedPath, String name, @Nullable Object value) {

Expand Down

0 comments on commit aa4f09d

Please sign in to comment.