Avoid nested constructor binding if there are no request parameters

Closes gh-31821
This commit is contained in:
rstoyanchev
2023-12-13 18:03:30 +00:00
parent 0970b1dc7a
commit ec0ec7a0d6
5 changed files with 103 additions and 7 deletions

View File

@@ -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();
}