|
21 | 21 | import jakarta.servlet.http.Part; |
22 | 22 | import org.jspecify.annotations.Nullable; |
23 | 23 |
|
| 24 | +import org.springframework.beans.ConfigurablePropertyAccessor; |
| 25 | +import org.springframework.beans.InvalidPropertyException; |
24 | 26 | import org.springframework.beans.MutablePropertyValues; |
| 27 | +import org.springframework.beans.PropertyValue; |
25 | 28 | import org.springframework.core.MethodParameter; |
26 | 29 | import org.springframework.http.HttpHeaders; |
27 | 30 | import org.springframework.http.HttpMethod; |
|
73 | 76 | * |
74 | 77 | * @author Juergen Hoeller |
75 | 78 | * @author Brian Clozel |
| 79 | + * @author Sumin Kim |
76 | 80 | * @since 2.5.2 |
77 | 81 | * @see #bind(org.springframework.web.context.request.WebRequest) |
78 | 82 | * @see #registerCustomEditor |
@@ -167,6 +171,46 @@ else if (StringUtils.startsWithIgnoreCase( |
167 | 171 | doBind(mpvs); |
168 | 172 | } |
169 | 173 |
|
| 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 | + |
170 | 214 | /** |
171 | 215 | * Treats errors as fatal. |
172 | 216 | * <p>Use this method only if it's an error if the input isn't valid. |
|
0 commit comments