From 796080abb8ca1a112817c9b5a7a76bfb48af8c1c Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Tue, 24 Oct 2023 12:23:51 +0100 Subject: [PATCH] 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 --- ...bstractNamedValueMethodArgumentResolver.java | 3 ++- ...RequestParamMethodArgumentResolverTests.java | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java index 7c71ea6b81..d1c9f1ac1f 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java @@ -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 { diff --git a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java index 2f822820c3..f172297ee9 100644 --- a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java @@ -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 paramOptional, @RequestParam("name") Optional paramOptionalArray, @RequestParam("name") Optional> paramOptionalList, - @RequestParam("mfile") Optional multipartFileOptional) { + @RequestParam("mfile") Optional multipartFileOptional, + @RequestParam(defaultValue = "false") Boolean booleanParam) { } }