Polish contribution
See gh-25200
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2018 the original author or authors.
|
* Copyright 2002-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -40,6 +40,7 @@ import org.springframework.core.ParameterNameDiscoverer;
|
|||||||
import org.springframework.core.annotation.AnnotationUtils;
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
import org.springframework.validation.BindException;
|
import org.springframework.validation.BindException;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.validation.Errors;
|
import org.springframework.validation.Errors;
|
||||||
@@ -277,6 +278,14 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
|
|||||||
String paramName = paramNames[i];
|
String paramName = paramNames[i];
|
||||||
Class<?> paramType = paramTypes[i];
|
Class<?> paramType = paramTypes[i];
|
||||||
Object value = webRequest.getParameterValues(paramName);
|
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 (value == null) {
|
||||||
if (fieldDefaultPrefix != null) {
|
if (fieldDefaultPrefix != null) {
|
||||||
value = webRequest.getParameter(fieldDefaultPrefix + paramName);
|
value = webRequest.getParameter(fieldDefaultPrefix + paramName);
|
||||||
@@ -288,11 +297,6 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 {
|
try {
|
||||||
MethodParameter methodParam = new FieldAwareConstructorParameter(ctor, i, paramName);
|
MethodParameter methodParam = new FieldAwareConstructorParameter(ctor, i, paramName);
|
||||||
if (value == null && methodParam.isOptional()) {
|
if (value == null && methodParam.isOptional()) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2020 the original author or authors.
|
* Copyright 2002-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,7 +19,6 @@ package org.springframework.web.method.annotation;
|
|||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
@@ -271,7 +270,7 @@ public class ModelAttributeMethodProcessorTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test // gh-25182
|
@Test // gh-25182
|
||||||
public void testResolveConstructorListParameter() throws Exception {
|
public void resolveConstructorListArgumentFromCommaSeparatedRequestParameter() throws Exception {
|
||||||
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
|
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
|
||||||
mockRequest.addParameter("listOfStrings", "1,2");
|
mockRequest.addParameter("listOfStrings", "1,2");
|
||||||
ServletWebRequest requestWithParam = new ServletWebRequest(mockRequest);
|
ServletWebRequest requestWithParam = new ServletWebRequest(mockRequest);
|
||||||
@@ -281,14 +280,14 @@ public class ModelAttributeMethodProcessorTests {
|
|||||||
.willAnswer(invocation -> {
|
.willAnswer(invocation -> {
|
||||||
WebRequestDataBinder binder = new WebRequestDataBinder(invocation.getArgument(1));
|
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());
|
binder.setConversionService(new DefaultFormattingConversionService());
|
||||||
return binder;
|
return binder;
|
||||||
});
|
});
|
||||||
|
|
||||||
Object resolved = this.processor.resolveArgument(this.beanWithConstructorArgs, this.container, requestWithParam, factory);
|
Object resolved = this.processor.resolveArgument(this.beanWithConstructorArgs, this.container, requestWithParam, factory);
|
||||||
assertThat(resolved).isNotNull();
|
assertThat(resolved).isInstanceOf(TestBeanWithConstructorArgs.class);
|
||||||
assertThat(((TestBeanWithConstructorArgs) resolved).listOfStrings).isEqualTo(Arrays.asList("1", "2"));
|
assertThat(((TestBeanWithConstructorArgs) resolved).listOfStrings).containsExactly("1", "2");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testGetAttributeFromModel(String expectedAttrName, MethodParameter param) throws Exception {
|
private void testGetAttributeFromModel(String expectedAttrName, MethodParameter param) throws Exception {
|
||||||
|
|||||||
Reference in New Issue
Block a user