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:
parent
e0ac000415
commit
796080abb8
|
|
@ -274,9 +274,10 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private static Object convertIfNecessary(
|
private static Object convertIfNecessary(
|
||||||
MethodParameter parameter, NativeWebRequest webRequest, WebDataBinderFactory binderFactory,
|
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);
|
WebDataBinder binder = binderFactory.createBinder(webRequest, null, namedValueInfo.name);
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -447,6 +447,20 @@ public class RequestParamMethodArgumentResolverTests {
|
||||||
assertThat(arg).isNull();
|
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
|
@Test
|
||||||
public void missingRequestParamEmptyValueNotRequired() throws Exception {
|
public void missingRequestParamEmptyValueNotRequired() throws Exception {
|
||||||
WebDataBinder binder = new WebRequestDataBinder(null);
|
WebDataBinder binder = new WebRequestDataBinder(null);
|
||||||
|
|
@ -687,7 +701,8 @@ public class RequestParamMethodArgumentResolverTests {
|
||||||
@RequestParam("name") Optional<Integer> paramOptional,
|
@RequestParam("name") Optional<Integer> paramOptional,
|
||||||
@RequestParam("name") Optional<Integer[]> paramOptionalArray,
|
@RequestParam("name") Optional<Integer[]> paramOptionalArray,
|
||||||
@RequestParam("name") Optional<List<?>> paramOptionalList,
|
@RequestParam("name") Optional<List<?>> paramOptionalList,
|
||||||
@RequestParam("mfile") Optional<MultipartFile> multipartFileOptional) {
|
@RequestParam("mfile") Optional<MultipartFile> multipartFileOptional,
|
||||||
|
@RequestParam(defaultValue = "false") Boolean booleanParam) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue