spring-framework/framework-docs
Sam Brannen 68fce29ae9 Support Optional with null-safe and Elvis operators in SpEL expressions
This commit introduces null-safe support for java.util.Optional in the
following SpEL operators:

- PropertyOrFieldReference
- MethodReference
- Indexer
- Projection
- Selection
- Elvis

Specifically, when a null-safe operator is applied to an empty
`Optional`, it will be treated as if the `Optional` were `null`, and
the subsequent operation will evaluate to `null`. However, if a
null-safe operator is applied to a non-empty `Optional`, the subsequent
operation will be applied to the object contained in the `Optional`,
thereby effectively unwrapping the `Optional`.

For example, if `user` is of type `Optional<User>`, the expression
`user?.name` will evaluate to `null` if `user` is either `null` or an
empty `Optional` and will otherwise evaluate to the `name` of the
`user`, effectively `user.get().getName()` for property access.

Note, however, that invocations of methods defined in the `Optional`
API are still supported on an empty `Optional`. For example, if `name`
is of type `Optional<String>`, the expression `name?.orElse('Unknown')`
will evaluate to "Unknown" if `name` is an empty `Optional` and will
otherwise evaluate to the `String` contained in the `Optional` if
`name` is a non-empty `Optional`, effectively `name.get()`.

Closes gh-20433
2025-03-12 14:53:06 +01:00
..
modules/ROOT Support Optional with null-safe and Elvis operators in SpEL expressions 2025-03-12 14:53:06 +01:00
src Merge branch '6.2.x' 2025-02-18 11:57:15 +01:00
antora-playbook.yml Upgrade to antora-ui-spring version 0.4.18 2024-11-15 14:32:01 +01:00
antora.yml Rework links to Spring Boot in reference docs 2024-10-23 16:10:37 +02:00
framework-docs.gradle Remove Java samples from Kotlin samples compilation 2025-01-09 11:36:48 +01:00
package.json Fix broken antora task 2025-02-19 16:40:50 +01:00