|
20 | 20 | import java.lang.reflect.Executable;
|
21 | 21 | import java.lang.reflect.Method;
|
22 | 22 | import java.util.List;
|
| 23 | +import java.util.Optional; |
23 | 24 |
|
24 | 25 | import org.springframework.core.MethodParameter;
|
25 | 26 | import org.springframework.core.convert.TypeDescriptor;
|
@@ -290,21 +291,29 @@ static boolean convertArguments(TypeConverter converter, Object[] arguments, Exe
|
290 | 291 | Object argument = arguments[varargsPosition];
|
291 | 292 | TypeDescriptor targetType = new TypeDescriptor(methodParam);
|
292 | 293 | TypeDescriptor sourceType = TypeDescriptor.forObject(argument);
|
293 |
| - // If the argument is null or the argument type is equal to the varargs element type, |
294 |
| - // there is no need to convert it or wrap it in an array. For example, using |
295 |
| - // StringToArrayConverter to convert a String containing a comma would result in the |
296 |
| - // String being split and repackaged in an array when it should be used as-is. |
297 |
| - if (argument != null && !sourceType.equals(targetType.getElementTypeDescriptor())) { |
| 294 | + if (argument == null) { |
| 295 | + // Perform the equivalent of GenericConversionService.convertNullSource() for a single argument. |
| 296 | + if (targetType.getElementTypeDescriptor().getObjectType() == Optional.class) { |
| 297 | + arguments[varargsPosition] = Optional.empty(); |
| 298 | + conversionOccurred = true; |
| 299 | + } |
| 300 | + } |
| 301 | + // If the argument type is equal to the varargs element type, there is no need to |
| 302 | + // convert it or wrap it in an array. For example, using StringToArrayConverter to |
| 303 | + // convert a String containing a comma would result in the String being split and |
| 304 | + // repackaged in an array when it should be used as-is. |
| 305 | + else if (!sourceType.equals(targetType.getElementTypeDescriptor())) { |
298 | 306 | arguments[varargsPosition] = converter.convertValue(argument, sourceType, targetType);
|
299 | 307 | }
|
300 |
| - // Possible outcomes of the above if-block: |
| 308 | + // Possible outcomes of the above if-else block: |
301 | 309 | // 1) the input argument was null, and nothing was done.
|
302 |
| - // 2) the input argument was correct type but not wrapped in an array, and nothing was done. |
303 |
| - // 3) the input argument was already compatible (i.e., array of valid type), and nothing was done. |
304 |
| - // 4) the input argument was the wrong type and got converted and wrapped in an array. |
| 310 | + // 2) the input argument was null; the varargs element type is Optional; and the argument was converted to Optional.empty(). |
| 311 | + // 3) the input argument was correct type but not wrapped in an array, and nothing was done. |
| 312 | + // 4) the input argument was already compatible (i.e., array of valid type), and nothing was done. |
| 313 | + // 5) the input argument was the wrong type and got converted and wrapped in an array. |
305 | 314 | if (argument != arguments[varargsPosition] &&
|
306 | 315 | !isFirstEntryInArray(argument, arguments[varargsPosition])) {
|
307 |
| - conversionOccurred = true; // case 3 |
| 316 | + conversionOccurred = true; // case 5 |
308 | 317 | }
|
309 | 318 | }
|
310 | 319 | // Otherwise, convert remaining arguments to the varargs element type.
|
|
0 commit comments