From d3c3088c6ba27a5a6d7f458a76f5d91320aaf828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Basl=C3=A9?= Date: Fri, 5 May 2023 16:13:06 +0200 Subject: [PATCH] Clarify behavior of the Elvis SpEL operator in documentation (#30352) This commit improves both the javadoc and the reference guide section on the Elvis SpEL operator to clarify that in addition to `null` objects, empty Strings also lead the operator to evaluate to its second operand. The reference guide's advanced snippet is modified to use such an empty String instead of `null` to make that behavior prominent with some code. See gh-30318 Closes gh-30352 --- .../core/expressions/language-ref/operator-elvis.adoc | 8 ++++++-- .../org/springframework/expression/spel/ast/Elvis.java | 10 +++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/framework-docs/modules/ROOT/pages/core/expressions/language-ref/operator-elvis.adoc b/framework-docs/modules/ROOT/pages/core/expressions/language-ref/operator-elvis.adoc index 8f7e69a0b6..3884dcadf2 100644 --- a/framework-docs/modules/ROOT/pages/core/expressions/language-ref/operator-elvis.adoc +++ b/framework-docs/modules/ROOT/pages/core/expressions/language-ref/operator-elvis.adoc @@ -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] @@ -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 ---- @@ -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 ---- diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/Elvis.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/Elvis.java index 55e2267c4c..5cbc2b594c 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/Elvis.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/Elvis.java @@ -26,9 +26,9 @@ import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; /** - * Represents the elvis operator ?:. For an expression a?:b if a is not null, - * the value of the expression is a, if a is null then the value of the expression is - * b. + * Represents the Elvis operator ?:. For an expression a?:b if a is neither null + * nor an empty String, the value of the expression is a. + * If a is null or the empty String, then the value of the expression is b. * * @author Andy Clement * @author Juergen Hoeller @@ -43,8 +43,8 @@ public class Elvis extends SpelNodeImpl { /** - * 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