Skip to content

Commit 796080a

Browse files
committed
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 e57b942. If an argument becomes null after conversion and a default value is applied, that default value should also pass through conversion. Closes gh-31336
1 parent e0ac000 commit 796080a

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,10 @@ else if (paramType.isPrimitive()) {
274274
return value;
275275
}
276276

277+
@Nullable
277278
private static Object convertIfNecessary(
278279
MethodParameter parameter, NativeWebRequest webRequest, WebDataBinderFactory binderFactory,
279-
NamedValueInfo namedValueInfo, Object arg) throws Exception {
280+
NamedValueInfo namedValueInfo, @Nullable Object arg) throws Exception {
280281

281282
WebDataBinder binder = binderFactory.createBinder(webRequest, null, namedValueInfo.name);
282283
try {

spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,20 @@ public void missingRequestParamEmptyValueConvertedToNull() throws Exception {
447447
assertThat(arg).isNull();
448448
}
449449

450+
@Test // gh-31336
451+
public void missingRequestParamAfterConversionWithDefaultValue() throws Exception {
452+
WebDataBinder binder = new WebRequestDataBinder(null);
453+
454+
WebDataBinderFactory binderFactory = mock();
455+
given(binderFactory.createBinder(webRequest, null, "booleanParam")).willReturn(binder);
456+
457+
request.addParameter("booleanParam", " ");
458+
459+
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Boolean.class);
460+
Object arg = resolver.resolveArgument(param, null, webRequest, binderFactory);
461+
assertThat(arg).isEqualTo(Boolean.FALSE);
462+
}
463+
450464
@Test
451465
public void missingRequestParamEmptyValueNotRequired() throws Exception {
452466
WebDataBinder binder = new WebRequestDataBinder(null);
@@ -687,7 +701,8 @@ public void handle(
687701
@RequestParam("name") Optional<Integer> paramOptional,
688702
@RequestParam("name") Optional<Integer[]> paramOptionalArray,
689703
@RequestParam("name") Optional<List<?>> paramOptionalList,
690-
@RequestParam("mfile") Optional<MultipartFile> multipartFileOptional) {
704+
@RequestParam("mfile") Optional<MultipartFile> multipartFileOptional,
705+
@RequestParam(defaultValue = "false") Boolean booleanParam) {
691706
}
692707

693708
}

0 commit comments

Comments
 (0)