expression-language icon indicating copy to clipboard operation
expression-language copied to clipboard

ELResolver for java.util.Optional

Open commodis opened this issue 4 years ago • 0 comments

There are often times where a field is better modelled as an Optional<T> instead using a traditional getter for T. Using a getter allows expressions like #{domainObject.optionalProperty.requiredProperty} which resolves the requiredProperty even if the optionalProperty might be null.

  • if domainObject.optionalProperty is null then domainObject.optionalProperty.requiredProperty will resolve to null as well
  • if domainObject.optionalProperty is not null then domainObject.optionalProperty.requiredProperty` will resolve to its content

This behavior denotes exactly how Optional as map its resolving operation works.

  • if domainObject.optionalProperty is empty then domainObject.maybeOptionalProperty().map(OptionalProperty::getRequiredProperty) will resolve to empty as well
  • if domainObject.optionalProperty is present then domainObject.maybeOptionalProperty().map(OptionalProperty::getRequiredProperty) will resolve to its content

The current ELResolver does not support this natively and the current specification does not mention java.util.Optional in such way.

I would like to evaluate the expression #{domainObject.optionalProperty.requiredProperty} even though optionalProperty is of type Optional<T>.

commodis avatar Dec 02 '21 08:12 commodis