Affects: 5.2.23
Hi
Today I faced with the following problem: ternary operator loses parenthesis after I call .toStringAST()
. I implemented the test reproducing the bug:
var spelExpressionParser = new SpelExpressionParser();
var expression = (SpelExpression) spelExpressionParser.parseExpression("(a ? 1 : 0) * 10");
assertEquals("((a ? 1 : 0) * 10)", expression.getAST().toStringAST());
And it fails with the error
org.opentest4j.AssertionFailedError: expected: <((a ? 1 : 0) * 10)> but was: <(a ? 1 : 0 * 10)>
In fact, the actual expression is different since it multiplies 0 to 10 first and then applies ternary operator
Comment From: sbrannen
Hi @MisterRnobe,
Thanks for bringing this to our attention, and congratulations on creating your first issue for the Spring Framework.
In fact, the actual expression is different since it multiplies 0 to 10 first and then applies ternary operator
The following test demonstrates that the expression is evaluated successfully, but the assertion for toStringAST()
indeed fails as you pointed out.
@Test
void ternaryExpressionEnclosedInParentheses() {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expression =
(SpelExpression) parser.parseExpression("((4 % 2 == 0) ? 2 : 1) * 10");
assertThat(expression.getValue()).isEqualTo(20);
assertThat(expression.getAST().toStringAST()).isEqualTo("(((4 % 2) == 0) ? 2 : 1) * 10");
}
So the issue seems to only be the output of toStringAST()
.
We will look into it.
Comment From: sbrannen
It turns our that Elvis expressions are also missing enclosing parentheses in the generated AST string representation, so I'll address that as well.
Comment From: sbrannen
This has been addressed in 27f3feea1a2488df17c3b6fbcbbf416768e7a5b3 for inclusion in 5.3.24 and 6.0 GA.