Skip to content
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

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

Closed
sbrannen opened this issue Dec 10, 2021 · 0 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@sbrannen
Copy link
Member

Overview

While testing the regression raised in #27719, I noticed that our support for java.util.Optional is inconsistent when invoking a method or function that accepts a varargs array of type Optional in a SpEL expression.

As can be seen in MethodInvocationTests, if null is supplied along with other values, each null value gets converted to Optional.empty(); whereas, a null value supplied by itself gets passed through as null to the invoked method.

// 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,'a')", "[Optional.empty, Optional[a]]", String.class);
evaluate("optionalVarargsMethod('a',null,'b')", "[Optional[a], Optional.empty, Optional[b]]", String.class);

Although this is a very specific use case that potentially does not even exist in a real project, we would like to ensure consistent semantics for Optional arguments in SpEL expressions.

@sbrannen sbrannen self-assigned this Dec 10, 2021
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels Dec 10, 2021
@sbrannen sbrannen added this to the 5.3.14 milestone Dec 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant