Skip to content

Resolve default value when argument converts to null #30636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ else if ("".equals(arg) && namedValueInfo.defaultValue != null) {
WebDataBinder binder = binderFactory.createBinder(webRequest, null, namedValueInfo.name);
try {
arg = binder.convertIfNecessary(arg, parameter.getParameterType(), parameter);
if (arg == null && namedValueInfo.defaultValue != null) {
arg = binder.convertIfNecessary(
resolveEmbeddedValuesAndExpressions(namedValueInfo.defaultValue),
parameter.getParameterType(), parameter);
}
}
catch (ConversionNotSupportedException ex) {
throw new MethodArgumentConversionNotSupportedException(arg, ex.getRequiredType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public void supportsParameter() {
MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
assertThat(resolver.supportsParameter(param)).isTrue();

param = this.testMethod.annotPresent(RequestParam.class).arg(Integer.class);
assertThat(resolver.supportsParameter(param)).isTrue();

param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
assertThat(resolver.supportsParameter(param)).isTrue();

Expand Down Expand Up @@ -504,6 +507,20 @@ public void resolveEmptyValueRequiredWithoutDefault() throws Exception {
assertThat(result).isEqualTo("");
}

@Test
public void resolveBlankValueToDefault() throws Exception {
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
initializer.setConversionService(new DefaultConversionService());
WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

request.addParameter("name", " ");
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Integer.class);
Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
boolean condition = result instanceof Integer;
assertThat(condition).isTrue();
assertThat(result).isEqualTo(20);
}

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void resolveOptionalParamValue() throws Exception {
Expand Down Expand Up @@ -662,6 +679,7 @@ public void handle(
@RequestParam("pfilelist") List<Part> param8,
@RequestParam("pfilearray") Part[] param9,
@RequestParam Map<?, ?> param10,
@RequestParam(name = "name", defaultValue = "20") Integer param11,
String stringNotAnnot,
MultipartFile multipartFileNotAnnot,
List<MultipartFile> multipartFileList,
Expand Down