-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
SpEL ternary and Elvis expressions are missing enclosing parentheses in toStringAST() #29463
Comments
Hi @MisterRnobe, Thanks for bringing this to our attention, and congratulations on creating your first issue for the Spring Framework.
The following test demonstrates that the expression is evaluated successfully, but the assertion for @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 We will look into it. |
It turns our that Elvis expressions are also missing enclosing parentheses in the generated AST string representation, so I'll address that as well. |
… in toStringAST() Prior to this commit, ternary and Elvis expressions enclosed in parentheses (to account for operator precedence) were properly parsed and evaluated; however, the corresponding toStringAST() implementations did not enclose the results in parentheses. Consequently, the string representation of the ASTs did not reflect the original semantics of such expressions. For example, given "(4 % 2 == 0 ? 1 : 0) * 10" as the expression to parse and evaluate, the result of toStringAST() was previously "(((4 % 2) == 0) ? 1 : 0 * 10)" instead of "((((4 % 2) == 0) ? 1 : 0) * 10)", implying that 0 should be multiplied by 10 instead of multiplying the result of the ternary expression by 10. This commit addresses this by ensuring that SpEL ternary and Elvis expressions are enclosed in parentheses in toStringAST(). Closes spring-projectsgh-29463
This has been addressed in 27f3fee for inclusion in 5.3.24 and 6.0 GA. |
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:And it fails with the error
In fact, the actual expression is different since it multiplies 0 to 10 first and then applies ternary operator
The text was updated successfully, but these errors were encountered: