Skip to content

Commit

Permalink
Polish contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed May 8, 2021
1 parent 9ddab9e commit 355d394
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
Expand Down Expand Up @@ -257,6 +258,14 @@ protected Object constructAttribute(Constructor<?> ctor, String attributeName, M
String paramName = paramNames[i];
Class<?> paramType = paramTypes[i];
Object value = webRequest.getParameterValues(paramName);

// Since WebRequest#getParameter exposes a single-value parameter as an array
// with a single element, we unwrap the single value in such cases, analogous
// to WebExchangeDataBinder.addBindValue(Map<String, Object>, String, List<?>).
if (ObjectUtils.isArray(value) && Array.getLength(value) == 1) {
value = Array.get(value, 0);
}

if (value == null) {
if (fieldDefaultPrefix != null) {
value = webRequest.getParameter(fieldDefaultPrefix + paramName);
Expand All @@ -271,11 +280,6 @@ protected Object constructAttribute(Constructor<?> ctor, String attributeName, M
}
}

// Singular web parameters are wrapped with array, extract it so it can be picked up by conversion service later on
if (value != null && value.getClass().isArray() && Array.getLength(value) == 1) {
value = Array.get(value, 0);
}

try {
MethodParameter methodParam = new FieldAwareConstructorParameter(ctor, i, paramName);
if (value == null && methodParam.isOptional()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -271,7 +270,7 @@ public void handleNotAnnotatedReturnValue() throws Exception {
}

@Test // gh-25182
public void testResolveConstructorListParameter() throws Exception {
public void resolveConstructorListArgumentFromCommaSeparatedRequestParameter() throws Exception {
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
mockRequest.addParameter("listOfStrings", "1,2");
ServletWebRequest requestWithParam = new ServletWebRequest(mockRequest);
Expand All @@ -281,14 +280,14 @@ public void testResolveConstructorListParameter() throws Exception {
.willAnswer(invocation -> {
WebRequestDataBinder binder = new WebRequestDataBinder(invocation.getArgument(1));

// Add conversion service which will convert "1,2" to List of size 2
// Add conversion service which will convert "1,2" to a list
binder.setConversionService(new DefaultFormattingConversionService());
return binder;
});

Object resolved = this.processor.resolveArgument(this.beanWithConstructorArgs, this.container, requestWithParam, factory);
assertThat(resolved).isNotNull();
assertThat(((TestBeanWithConstructorArgs) resolved).listOfStrings).isEqualTo(Arrays.asList("1", "2"));
assertThat(resolved).isInstanceOf(TestBeanWithConstructorArgs.class);
assertThat(((TestBeanWithConstructorArgs) resolved).listOfStrings).containsExactly("1", "2");
}

private void testGetAttributeFromModel(String expectedAttrName, MethodParameter param) throws Exception {
Expand Down

0 comments on commit 355d394

Please sign in to comment.