Skip to content

Commit

Permalink
Do not precede indexed access with a dot in SpEL AST representation
Browse files Browse the repository at this point in the history
Prior to this commit, if a Spring Expression Language (SpEL) expression
contained indexed access to an object, the generated AST String
representation incorrectly included a dot ('.') before the index access.

For example, 'property[0]' had a generated AST string representation of
'property.[0]'.

This commit addresses this by reworking the logic in
CompoundExpression.toStringAST().

Closes spring-projectsgh-30610
  • Loading branch information
sbrannen authored and mdeinum committed Jun 29, 2023
1 parent bcc6d24 commit 821f59d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.expression.spel.ast;

import java.util.StringJoiner;
import java.util.function.Supplier;

import org.springframework.asm.MethodVisitor;
Expand All @@ -25,10 +24,14 @@
import org.springframework.expression.spel.CodeFlow;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelNode;

/**
* Represents a DOT separated expression sequence, such as
* {@code 'property1.property2.methodOne()'}.
* {@code property1.property2.methodOne()}.
*
* <p>May also contain array/collection/map indexers, such as
* {@code property1[0].property2['key']}.
*
* @author Andy Clement
* @author Sam Brannen
Expand Down Expand Up @@ -111,11 +114,19 @@ public boolean isWritable(ExpressionState state) throws EvaluationException {

@Override
public String toStringAST() {
StringJoiner sj = new StringJoiner(".");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < getChildCount(); i++) {
sj.add(getChild(i).toStringAST());
sb.append(getChild(i).toStringAST());
if (i < getChildCount() - 1) {
SpelNode nextChild = getChild(i + 1);
// Don't append a '.' if the next child is an Indexer.
// For example, we want 'myVar[0]' instead of 'myVar.[0]'.
if (!(nextChild instanceof Indexer)) {
sb.append('.');
}
}
}
return sj.toString();
return sb.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class ParsingTests {
@Nested
class Miscellaneous {

@Test
void compoundExpressions() {
parseCheck("property1.property2.methodOne()");
parseCheck("property1[0].property2['key'].methodOne()");
}

@Test
void supportedCharactersInIdentifiers() {
parseCheck("#var='value'");
Expand Down

0 comments on commit 821f59d

Please sign in to comment.