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

index variable not usable in collection selection/projection in SpEL #23202

Closed
peterschubert opened this issue Jun 27, 2019 · 1 comment
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@peterschubert
Copy link

peterschubert commented Jun 27, 2019

Affects: 5.0.4

When iterating on a collection, both Selection and Projection set a variable called index that contains the index of the current element.

I would expect this variable to be usable in SpEL, e.g. for filtering one list based on elements of another list (or any other criterion that takes the index into account).

However, there doesn't seem to be any way to reference it.

The only code that makes use of VariableScope is never called.

Am I missing something or is this feature not really complete?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 27, 2019
@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 10, 2021
@sbrannen sbrannen self-assigned this Jan 10, 2024
@sbrannen sbrannen changed the title SPEL: "index" not usable in collection selection/projection? index variable not usable in collection selection/projection in SpEL Jan 10, 2024
@sbrannen
Copy link
Member

sbrannen commented Jan 10, 2024

Hi @peterschubert,

Thanks for raising the issue and apologies for the belated reply.

Am I missing something or is this feature not really complete?

No, you're not missing anything.

The team originally considered providing support for local variables in SpEL expressions, and that's evident by the code you noticed as well as various other methods in ExpressionState.

However, the Spring Framework does not make use of the local variable support in ExpressionState other than in a few unit tests.

If local variables were supported in the AST and evaluation, I believe you would have been able to access that index variable as $index, but unfortunately that's not the case.

In light of that, I am closing this issue.

@sbrannen sbrannen added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 10, 2024
@sbrannen sbrannen closed this as not planned Won't fix, can't repro, duplicate, stale Jan 11, 2024
sbrannen added a commit that referenced this issue Feb 16, 2024
Since the Spring Expression Language does not actually support local
variables in expressions, this commit deprecates all public APIs
related to local variables in ExpressionState (namely, the two
enterScope(...) variants that accept local variable data,
setLocalVariable(), and lookupLocalVariable()).

In addition, we no longer invoke `state.enterScope("index", ...)` in
the Projection and Selection AST nodes since the $index local variable
was never accessible within expressions anyway.

See gh-23202
Closes gh-32004
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: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

4 participants