BindingResult support for constructor argument mismatch on immutable data object

Issue: SPR-15542
This commit is contained in:
Juergen Hoeller
2017-08-17 11:02:40 +02:00
parent b8f5e97ae5
commit 61cdc842e0
4 changed files with 415 additions and 345 deletions

View File

@@ -45,6 +45,7 @@ import org.springframework.web.servlet.HandlerMapping;
* model attribute name and there is an appropriate type conversion strategy.
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 3.1
*/
public class ServletModelAttributeMethodProcessor extends ModelAttributeMethodProcessor {
@@ -68,19 +69,19 @@ public class ServletModelAttributeMethodProcessor extends ModelAttributeMethodPr
* @see #createAttributeFromRequestValue
*/
@Override
protected final Object createAttribute(String attributeName, MethodParameter methodParam,
protected final Object createAttribute(String attributeName, MethodParameter parameter,
WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception {
String value = getRequestValueForAttribute(attributeName, request);
if (value != null) {
Object attribute = createAttributeFromRequestValue(
value, attributeName, methodParam, binderFactory, request);
value, attributeName, parameter, binderFactory, request);
if (attribute != null) {
return attribute;
}
}
return super.createAttribute(attributeName, methodParam, binderFactory, request);
return super.createAttribute(attributeName, parameter, binderFactory, request);
}
/**
@@ -120,7 +121,7 @@ public class ServletModelAttributeMethodProcessor extends ModelAttributeMethodPr
* {@link Converter} that can perform the conversion.
* @param sourceValue the source value to create the model attribute from
* @param attributeName the name of the attribute (never {@code null})
* @param methodParam the method parameter
* @param parameter the method parameter
* @param binderFactory for creating WebDataBinder instance
* @param request the current request
* @return the created model attribute, or {@code null} if no suitable
@@ -129,16 +130,16 @@ public class ServletModelAttributeMethodProcessor extends ModelAttributeMethodPr
*/
@Nullable
protected Object createAttributeFromRequestValue(String sourceValue, String attributeName,
MethodParameter methodParam, WebDataBinderFactory binderFactory, NativeWebRequest request)
MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request)
throws Exception {
DataBinder binder = binderFactory.createBinder(request, null, attributeName);
ConversionService conversionService = binder.getConversionService();
if (conversionService != null) {
TypeDescriptor source = TypeDescriptor.valueOf(String.class);
TypeDescriptor target = new TypeDescriptor(methodParam);
TypeDescriptor target = new TypeDescriptor(parameter);
if (conversionService.canConvert(source, target)) {
return binder.convertIfNecessary(sourceValue, methodParam.getParameterType(), methodParam);
return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
}
}
return null;