RequestParam resolver supports empty array suffix
Closes gh-32577
This commit is contained in:
parent
4a68c44a27
commit
8d05028c2e
|
|
@ -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.
|
||||
|
|
@ -179,6 +179,9 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
|
|||
}
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -167,6 +167,19 @@ class RequestParamMethodArgumentResolverTests {
|
|||
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();
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
@ -100,8 +100,11 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueSyncAr
|
|||
@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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -130,6 +130,16 @@ class RequestParamMethodArgumentResolverTests {
|
|||
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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue