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

Clarify behavior of the Elvis SpEL operator in documentation #30352

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Kotlin::
----
======

NOTE: The SpEL Elvis operator also checks for _empty_ Strings in addition to `null` objects.
The original snippet is thus only close to emulating the semantics of the operator (it would need an
additional `!name.isEmpty()` check).

The following listing shows a more complex example:

[tabs]
Expand All @@ -53,7 +57,7 @@ Java::
String name = parser.parseExpression("name?:'Elvis Presley'").getValue(context, tesla, String.class);
System.out.println(name); // Nikola Tesla

tesla.setName(null);
tesla.setName("");
name = parser.parseExpression("name?:'Elvis Presley'").getValue(context, tesla, String.class);
System.out.println(name); // Elvis Presley
----
Expand All @@ -69,7 +73,7 @@ Kotlin::
var name = parser.parseExpression("name?:'Elvis Presley'").getValue(context, tesla, String::class.java)
println(name) // Nikola Tesla

tesla.setName(null)
tesla.setName("")
name = parser.parseExpression("name?:'Elvis Presley'").getValue(context, tesla, String::class.java)
println(name) // Elvis Presley
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import org.springframework.util.ObjectUtils;

/**
* Represents the elvis operator <code>?:</code>. For an expression <code>a?:b</code> if <code>a</code> is not null,
* the value of the expression is <code>a</code>, if <code>a</code> is null then the value of the expression is
* <code>b</code>.
* Represents the Elvis operator <code>?:</code>. For an expression <code>a?:b</code> if <code>a</code> is neither null
* nor an empty String, the value of the expression is <code>a</code>.
* If <code>a</code> is null or the empty String, then the value of the expression is <code>b</code>.
*
* @author Andy Clement
* @author Juergen Hoeller
Expand All @@ -43,8 +43,8 @@ public Elvis(int startPos, int endPos, SpelNodeImpl... args) {


/**
* Evaluate the condition and if not null, return it.
* If it is null, return the other value.
* Evaluate the condition and if neither null nor an empty String, return it.
* If it is null or an empty String, return the other value.
* @param state the expression state
* @throws EvaluationException if the condition does not evaluate correctly
* to a boolean or there is a problem executing the chosen alternative
Expand Down