Test for change to add conversion of defaultValue

This commit adds a test and polishing for a change in
AbstractNamedValueMethodArgumentResolver erroneously committed
with (unrelated) commit e57b942b.

If an argument becomes null after conversion and a default value is
applied, that default value should also pass through conversion.

Closes gh-31336
This commit is contained in:
rstoyanchev 2023-10-24 12:23:51 +01:00
parent e0ac000415
commit 796080abb8
2 changed files with 18 additions and 2 deletions

View File

@ -274,9 +274,10 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
return value;
}
@Nullable
private static Object convertIfNecessary(
MethodParameter parameter, NativeWebRequest webRequest, WebDataBinderFactory binderFactory,
NamedValueInfo namedValueInfo, Object arg) throws Exception {
NamedValueInfo namedValueInfo, @Nullable Object arg) throws Exception {
WebDataBinder binder = binderFactory.createBinder(webRequest, null, namedValueInfo.name);
try {

View File

@ -447,6 +447,20 @@ public class RequestParamMethodArgumentResolverTests {
assertThat(arg).isNull();
}
@Test // gh-31336
public void missingRequestParamAfterConversionWithDefaultValue() throws Exception {
WebDataBinder binder = new WebRequestDataBinder(null);
WebDataBinderFactory binderFactory = mock();
given(binderFactory.createBinder(webRequest, null, "booleanParam")).willReturn(binder);
request.addParameter("booleanParam", " ");
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Boolean.class);
Object arg = resolver.resolveArgument(param, null, webRequest, binderFactory);
assertThat(arg).isEqualTo(Boolean.FALSE);
}
@Test
public void missingRequestParamEmptyValueNotRequired() throws Exception {
WebDataBinder binder = new WebRequestDataBinder(null);
@ -687,7 +701,8 @@ public class RequestParamMethodArgumentResolverTests {
@RequestParam("name") Optional<Integer> paramOptional,
@RequestParam("name") Optional<Integer[]> paramOptionalArray,
@RequestParam("name") Optional<List<?>> paramOptionalList,
@RequestParam("mfile") Optional<MultipartFile> multipartFileOptional) {
@RequestParam("mfile") Optional<MultipartFile> multipartFileOptional,
@RequestParam(defaultValue = "false") Boolean booleanParam) {
}
}