Skip to content

Commit

Permalink
RequestParam resolver supports empty array suffix
Browse files Browse the repository at this point in the history
Closes gh-32577
  • Loading branch information
rstoyanchev committed Apr 9, 2024
1 parent 4a68c44 commit 8d05028
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -179,6 +179,9 @@ protected Object resolveName(String name, MethodParameter parameter, NativeWebRe
}
if (arg == null) {
String[] paramValues = request.getParameterValues(name);
if (paramValues == null) {
paramValues = request.getParameterValues(name + "[]");
}
if (paramValues != null) {
arg = (paramValues.length == 1 ? paramValues[0] : paramValues);
}
Expand Down
Expand Up @@ -66,11 +66,11 @@ class RequestParamMethodArgumentResolverTests {

private RequestParamMethodArgumentResolver resolver = new RequestParamMethodArgumentResolver(null, true);

private MockHttpServletRequest request = new MockHttpServletRequest();
private final MockHttpServletRequest request = new MockHttpServletRequest();

private NativeWebRequest webRequest = new ServletWebRequest(request, new MockHttpServletResponse());

private ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handle").build();
private final ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handle").build();


@Test
Expand Down Expand Up @@ -167,6 +167,19 @@ void resolveStringArray() throws Exception {
assertThat((String[]) result).as("Invalid result").isEqualTo(expected);
}

@Test // gh-32577
void resolveStringArrayWithEmptyArraySuffix() throws Exception {
String[] expected = new String[] {"foo", "bar"};
request.addParameter("name[]", expected[0]);
request.addParameter("name[]", expected[1]);

MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof String[];
assertThat(condition).isTrue();
assertThat((String[]) result).isEqualTo(expected);
}

@Test
void resolveMultipartFile() throws Exception {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -100,8 +100,11 @@ protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
@Override
@Nullable
protected Object resolveNamedValue(String name, MethodParameter parameter, ServerWebExchange exchange) {
List<String> paramValues = exchange.getRequest().getQueryParams().get(name);
Object result = null;
List<String> paramValues = exchange.getRequest().getQueryParams().get(name);
if (paramValues == null) {
paramValues = exchange.getRequest().getQueryParams().get(name + "[]");
}
if (paramValues != null) {
result = (paramValues.size() == 1 ? paramValues.get(0) : paramValues);
}
Expand Down
Expand Up @@ -53,7 +53,7 @@ class RequestParamMethodArgumentResolverTests {

private BindingContext bindContext;

private ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handle").build();
private final ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handle").build();


@BeforeEach
Expand Down Expand Up @@ -130,6 +130,16 @@ void resolveStringArray() {
assertThat((String[]) result).isEqualTo(new String[] {"foo", "bar"});
}

@Test // gh-32577
void resolveStringArrayWithEmptyArraySuffix() {
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
MockServerHttpRequest request = MockServerHttpRequest.get("/path?name[]=foo&name[]=bar").build();
Object result = resolve(param, MockServerWebExchange.from(request));
boolean condition = result instanceof String[];
assertThat(condition).isTrue();
assertThat((String[]) result).isEqualTo(new String[] {"foo", "bar"});
}

@Test
void resolveDefaultValue() {
MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
Expand Down

0 comments on commit 8d05028

Please sign in to comment.