Skip to content

Commit 6300fb3

Browse files
committed
Include '?' for null-safe navigation in SpEL AST representations
Prior to this commit, if a Spring Expression Language (SpEL) expression contained property, field, or method references using the null-safe navigation operator (?.), the generated AST String representation incorrectly omitted the '?' characters. For example, 'myProperty?.myMethod()' had a generated AST string representation of 'myProperty.myMethod()'. This commit addresses this by introducing isNullSafe() in MethodReference and reworking the logic in CompoundExpression.toStringAST(). Closes gh-31326
1 parent 0d22569 commit 6300fb3

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/ast/CompoundExpression.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828

2929
/**
3030
* Represents a DOT separated expression sequence, such as
31-
* {@code property1.property2.methodOne()}.
31+
* {@code property1.property2.methodOne()} or
32+
* {@code property1?.property2?.methodOne()} when the null-safe navigation
33+
* operator is used.
3234
*
3335
* <p>May also contain array/collection/map indexers, such as
3436
* {@code property1[0].property2['key']}.
@@ -122,6 +124,10 @@ public String toStringAST() {
122124
// Don't append a '.' if the next child is an Indexer.
123125
// For example, we want 'myVar[0]' instead of 'myVar.[0]'.
124126
if (!(nextChild instanceof Indexer)) {
127+
if ((nextChild instanceof MethodReference methodRef && methodRef.isNullSafe()) ||
128+
(nextChild instanceof PropertyOrFieldReference pofRef && pofRef.isNullSafe())) {
129+
sb.append('?');
130+
}
125131
sb.append('.');
126132
}
127133
}

spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
*
5151
* @author Andy Clement
5252
* @author Juergen Hoeller
53+
* @author Sam Brannen
5354
* @since 3.0
5455
*/
5556
public class MethodReference extends SpelNodeImpl {
@@ -72,6 +73,14 @@ public MethodReference(boolean nullSafe, String methodName, int startPos, int en
7273
}
7374

7475

76+
/**
77+
* Does this node represent a null-safe method reference?
78+
* @since 6.0.13
79+
*/
80+
public final boolean isNullSafe() {
81+
return this.nullSafe;
82+
}
83+
7584
/**
7685
* Get the name of the referenced method.
7786
*/

spring-expression/src/test/java/org/springframework/expression/spel/ParsingTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Miscellaneous {
4747
void compoundExpressions() {
4848
parseCheck("property1.property2.methodOne()");
4949
parseCheck("property1[0].property2['key'].methodOne()");
50+
parseCheck("property1?.methodOne()?.property2?.methodTwo()");
5051
}
5152

5253
@Test

0 commit comments

Comments
 (0)