Convert single null argument to Optional.empty() in SpEL varargs expression

Prior to this commit, if a single null value was passed to a method with
a varargs array of type java.util.Optional, that null value was passed
unmodified. On the contrary, a null passed with additional values to
such a method resulted in the null being converted to Optional.empty().

This commit ensures that a single null value is also converted to
Optional.empty() for such SpEL expressions.

Closes gh-27795
This commit is contained in:
Sam Brannen
2021-12-10 13:53:28 +01:00
parent ad7cdc5ce9
commit b2e94f611f
2 changed files with 20 additions and 14 deletions

View File

@@ -302,10 +302,7 @@ public class MethodInvocationTests extends AbstractExpressionTests {
evaluate("optionalVarargsMethod(2,3)", "[Optional[2], Optional[3]]", String.class);
evaluate("optionalVarargsMethod('a',3.0d)", "[Optional[a], Optional[3.0]]", String.class);
evaluate("optionalVarargsMethod(new String[]{'a','b','c'})", "[Optional[a], Optional[b], Optional[c]]", String.class);
// The following should actually evaluate to [Optional.empty] instead of [null],
// but ReflectionHelper.convertArguments() currently does not provide explicit
// Optional support for a single argument passed to a varargs array.
evaluate("optionalVarargsMethod(null)", "[null]", String.class);
evaluate("optionalVarargsMethod(null)", "[Optional.empty]", String.class);
evaluate("optionalVarargsMethod(null,'a')", "[Optional.empty, Optional[a]]", String.class);
evaluate("optionalVarargsMethod('a',null,'b')", "[Optional[a], Optional.empty, Optional[b]]", String.class);
}