Consistent data class constructor resolution with clear error message
MVC data class processor constructs target instance even in case of binding failure, as long as the corresponding method parameter is not marked as optional. Closes gh-24372
This commit is contained in:
@@ -201,21 +201,7 @@ public class ModelAttributeMethodArgumentResolver extends HandlerMethodArgumentR
|
||||
private Mono<?> createAttribute(
|
||||
String attributeName, Class<?> clazz, BindingContext context, ServerWebExchange exchange) {
|
||||
|
||||
Constructor<?> ctor = BeanUtils.findPrimaryConstructor(clazz);
|
||||
if (ctor == null) {
|
||||
Constructor<?>[] ctors = clazz.getConstructors();
|
||||
if (ctors.length == 1) {
|
||||
ctor = ctors[0];
|
||||
}
|
||||
else {
|
||||
try {
|
||||
ctor = clazz.getDeclaredConstructor();
|
||||
}
|
||||
catch (NoSuchMethodException ex) {
|
||||
throw new IllegalStateException("No primary or default constructor found for " + clazz, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Constructor<?> ctor = BeanUtils.getResolvableConstructor(clazz);
|
||||
return constructAttribute(ctor, attributeName, context, exchange);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user