Allow treating empty @RequestParam as missing value

If type conversion turns an empty request parameter value (i.e. "") to
null, we should treat it as a missing value. By default the
ConversionService doesn't change empty strings and therefore one must
explicitly convert them to null for example by registering a
StringTrimmerEditor with emptyAsNull=true.

Issue: SPR-10402
This commit is contained in:
Rossen Stoyanchev
2013-05-10 12:31:00 -04:00
parent c0cacfcd68
commit e39fe1822d
2 changed files with 32 additions and 7 deletions

View File

@@ -74,6 +74,7 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
this.expressionContext = (beanFactory != null) ? new BeanExpressionContext(beanFactory, new RequestScope()) : null;
}
@Override
public final Object resolveArgument(
MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory)
@@ -96,11 +97,17 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
arg = resolveDefaultValue(namedValueInfo.defaultValue);
}
boolean emptyArgValue = "".equals(arg);
if (binderFactory != null) {
WebDataBinder binder = binderFactory.createBinder(webRequest, null, namedValueInfo.name);
arg = binder.convertIfNecessary(arg, paramType, parameter);
}
if (emptyArgValue && (arg == null)) {
handleMissingValue(namedValueInfo.name, parameter);
}
handleResolvedValue(arg, namedValueInfo.name, parameter, mavContainer, webRequest);
return arg;