Skip to content
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 expression is missing fallback part during parsing. #34117

Open
Mitsuki0710 opened this issue Dec 18, 2024 · 5 comments
Open

SpEL ternary expression is missing fallback part during parsing. #34117

Mitsuki0710 opened this issue Dec 18, 2024 · 5 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged or decided on

Comments

@Mitsuki0710
Copy link

Having a Spring ternary expression "Did he win the race? ${['win'] ? 'Yes.' : "No.'}", PlaceholderParser will break it into two parts.

  1. TextPart - { text : "Did he win the race?" }
  2. SimplePlaceholderPart - { key : "['win'] ? 'Yes.'", fallback : "'No.'", text : "['win'] ? "'Yes.' : 'No.'" }

But while resolving the SimplePlaceholderPart context, it returns the key value of it which misses the fallback part (PlaceholderParser.class : L376).

Now, if try SpelExpression.doParseExpression("['win'] ? 'Yes.'"), this.eatToken(TokenKind.COLON);(InternalSpelExpressionParser.class : L155) will throw the exception.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 18, 2024
@Mitsuki0710 Mitsuki0710 changed the title Spel ternary expression is missing fallback part during parsing. SpEL ternary expression is missing fallback part during parsing. Dec 18, 2024
@sbrannen
Copy link
Member

The ${...} syntax is for a property placeholder, and the result you are getting seems to be appropriate.

If you intend for that be evaluated as a SpEL expression, you need to use the #{['win'] ? 'Yes.' : "No.'} syntax.

Does switching to the #{...} syntax solve your problem?

If not, what is it you are trying to achieve?

@sbrannen sbrannen added status: waiting-for-feedback We need additional information before we can continue in: core Issues in core modules (aop, beans, core, context, expression) labels Dec 18, 2024
@Mitsuki0710
Copy link
Author

Unfortunately, #{...} doesn't work.

We are using SpEL expression to parse the expression and replace placeholders with property values.

['win'] this will be replaced by a true value from properties, and should return 'Yes.'.
So the final result we are expecting is "Did he win the race? Yes.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Dec 20, 2024
@sbrannen
Copy link
Member

Does the following work for you?

"Did he win the race? #{${win} ? 'Yes.' : "No.'}"

@Mitsuki0710
Copy link
Author

"Did he win the race? #{${win} ? 'Yes.' : "No.'}"
has the following error
ERROR [main] [SpelPropertyResolver] EL1008E: Property or field 'win' cannot be found on object of type 'java.util.Properties' - maybe not public or not valid?ERROR [main] [SpelPropertyResolver] EL1008E: Property or field 'win' cannot be found on object of type 'java.util.Properties' - maybe not public or not valid?

If I wrap up with [] make it replaceable with property value
"Did he win the race? #{${[win]} ? 'Yes.' : "No.'}"
gets the following result:
Expected :Did he win the race? Yes. Actual :Did he win the race? #{true ? 'Yes.' : 'No.'}

And I am constructing the property placeholder helper via PropertyPlaceholderHelper("${", "}", ":", null, true)

@sbrannen
Copy link
Member

Please provide a minimal sample application that demonstrates what you are trying to achieve, one that we can download and run ourselves, such as a public Git repository or a ZIP file.

Without that, I'm afraid we cannot determine if what you are trying to accomplish is supported.

@sbrannen sbrannen added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

No branches or pull requests

3 participants