Avoid nested constructor binding if there are no request parameters
Closes gh-31821
This commit is contained in:
@@ -948,7 +948,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
|
||||
Class<?> paramType = paramTypes[i];
|
||||
Object value = valueResolver.resolveValue(paramPath, paramType);
|
||||
|
||||
if (value == null && shouldConstructArgument(param)) {
|
||||
if (value == null && shouldConstructArgument(param) && hasValuesFor(paramPath, valueResolver)) {
|
||||
ResolvableType type = ResolvableType.forMethodParameter(param);
|
||||
args[i] = createObject(type, paramPath + ".", valueResolver);
|
||||
}
|
||||
@@ -1022,6 +1022,15 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
|
||||
type.getPackageName().startsWith("java."));
|
||||
}
|
||||
|
||||
private boolean hasValuesFor(String paramPath, ValueResolver resolver) {
|
||||
for (String name : resolver.getNames()) {
|
||||
if (name.startsWith(paramPath + ".")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void validateConstructorArgument(
|
||||
Class<?> constructorClass, String nestedPath, String name, @Nullable Object value) {
|
||||
|
||||
@@ -1293,7 +1302,6 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
|
||||
* Strategy for {@link #construct constructor binding} to look up the values
|
||||
* to bind to a given constructor parameter.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ValueResolver {
|
||||
|
||||
/**
|
||||
@@ -1305,6 +1313,13 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
|
||||
*/
|
||||
@Nullable
|
||||
Object resolveValue(String name, Class<?> type);
|
||||
|
||||
/**
|
||||
* Return the names of all property values.
|
||||
* @since 6.1.2
|
||||
*/
|
||||
Set<String> getNames();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user