Skip to content

Fix inconsistent query parameter conversion for String param #34711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import jakarta.servlet.http.Part;
import org.jspecify.annotations.Nullable;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.*;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand All @@ -36,6 +36,10 @@
import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.multipart.support.StandardServletPartUtils;

import java.beans.PropertyDescriptor;

import static org.springframework.beans.BeanUtils.getPropertyDescriptor;

/**
* Special {@link org.springframework.validation.DataBinder} to perform data binding
* from web request parameters to JavaBeans, including support for multipart files.
Expand Down Expand Up @@ -167,6 +171,34 @@ else if (StringUtils.startsWithIgnoreCase(
doBind(mpvs);
}

@Override
protected void doBind(MutablePropertyValues mpvs) {
Object target = getTarget();
if (target == null) {
super.doBind(mpvs);
return;
}

ConfigurablePropertyAccessor accessor = getPropertyAccessor();

for (PropertyValue pv : mpvs.getPropertyValues()) {
String name = pv.getName();
Object value = pv.getValue();
if (!name.endsWith("[]") && value instanceof String[] array && array.length > 0) {
try {
Class<?> propertyType = accessor.getPropertyType(name);
if (propertyType == null || !propertyType.isArray()) {
mpvs.add(name, new String[] { array[0] });
}
}
catch (InvalidPropertyException ex) {
mpvs.add(name, new String[] { array[0] });
}
}
}
super.doBind(mpvs);
}

/**
* Treats errors as fatal.
* <p>Use this method only if it's an error if the input isn't valid.
Expand All @@ -178,5 +210,4 @@ public void closeNoCatch() throws BindException {
throw new BindException(getBindingResult());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

/**
* @author Juergen Hoeller
* @author Sumin Kim
*/
class WebRequestDataBinderTests {

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

@Test
public void testFieldWithEmptyArrayIndex1() {
TestBean target = new TestBean();
WebRequestDataBinder binder = new WebRequestDataBinder(target);

MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("touchy", "ONE");
request.addParameter("touchy", "TWO");
binder.bind(new ServletWebRequest(request));
assertThat(target.getTouchy()).isEqualTo("ONE");
}

@Test
void testFieldDefault() {
TestBean target = new TestBean();
Expand Down