Skip to content

Commit 90f17a9

Browse files
author
kssumin
committed
Fix inconsistent query parameter conversion for String params
Signed-off-by: kssumin <201566@jnu.ac.kr>
1 parent 1898912 commit 90f17a9

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

spring-web/src/main/java/org/springframework/web/bind/support/WebRequestDataBinder.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
import jakarta.servlet.http.Part;
2222
import org.jspecify.annotations.Nullable;
2323

24+
import org.springframework.beans.ConfigurablePropertyAccessor;
25+
import org.springframework.beans.InvalidPropertyException;
2426
import org.springframework.beans.MutablePropertyValues;
27+
import org.springframework.beans.PropertyValue;
2528
import org.springframework.core.MethodParameter;
2629
import org.springframework.http.HttpHeaders;
2730
import org.springframework.http.HttpMethod;
@@ -73,6 +76,7 @@
7376
*
7477
* @author Juergen Hoeller
7578
* @author Brian Clozel
79+
* @author Sumin Kim
7680
* @since 2.5.2
7781
* @see #bind(org.springframework.web.context.request.WebRequest)
7882
* @see #registerCustomEditor
@@ -167,6 +171,46 @@ else if (StringUtils.startsWithIgnoreCase(
167171
doBind(mpvs);
168172
}
169173

174+
/**
175+
* Binds the given property values to this binder's target.
176+
* <p>This implementation handles the case where string array values need to be
177+
* converted to single string values if the property is not array-typed.
178+
* <p>For properties that don't have the "[]" suffix but contain string array values,
179+
* this method will use only the first array element when the target property
180+
* is not of array type.
181+
*
182+
* @param mpvs the property values to bind
183+
* @see #getTarget()
184+
* @see #getPropertyAccessor()
185+
*/
186+
@Override
187+
protected void doBind(MutablePropertyValues mpvs) {
188+
Object target = getTarget();
189+
if (target == null) {
190+
super.doBind(mpvs);
191+
return;
192+
}
193+
194+
ConfigurablePropertyAccessor accessor = getPropertyAccessor();
195+
196+
for (PropertyValue pv : mpvs.getPropertyValues()) {
197+
String propertyName = pv.getName();
198+
Object value = pv.getValue();
199+
if (!propertyName.endsWith("[]") && value instanceof String[] array && array.length > 0) {
200+
try {
201+
Class<?> propertyType = accessor.getPropertyType(propertyName);
202+
if (propertyType == null || !propertyType.isArray()) {
203+
mpvs.add(propertyName, new String[] { array[0] });
204+
}
205+
}
206+
catch (InvalidPropertyException ex) {
207+
mpvs.add(propertyName, new String[] { array[0] });
208+
}
209+
}
210+
}
211+
super.doBind(mpvs);
212+
}
213+
170214
/**
171215
* Treats errors as fatal.
172216
* <p>Use this method only if it's an error if the input isn't valid.

spring-web/src/test/java/org/springframework/web/bind/support/WebRequestDataBinderTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
/**
4242
* @author Juergen Hoeller
43+
* @author Sumin Kim
4344
*/
4445
class WebRequestDataBinderTests {
4546

@@ -124,6 +125,18 @@ public void testFieldWithEmptyArrayIndex() {
124125
assertThat(target.getStringArray()).containsExactly("ONE", "TWO");
125126
}
126127

128+
@Test
129+
public void testSameNameParameterFirstValueConversion() {
130+
TestBean target = new TestBean();
131+
WebRequestDataBinder binder = new WebRequestDataBinder(target);
132+
133+
MockHttpServletRequest request = new MockHttpServletRequest();
134+
request.addParameter("touchy", "ONE");
135+
request.addParameter("touchy", "TWO");
136+
binder.bind(new ServletWebRequest(request));
137+
assertThat(target.getTouchy()).isEqualTo("ONE");
138+
}
139+
127140
@Test
128141
void testFieldDefault() {
129142
TestBean target = new TestBean();

0 commit comments

Comments
 (0)