Skip to content

Mustache template evaluation failure for non-existent array indices #55200

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

Closed
ywangd opened this issue Apr 15, 2020 · 3 comments · Fixed by #126550
Closed

Mustache template evaluation failure for non-existent array indices #55200

ywangd opened this issue Apr 15, 2020 · 3 comments · Fixed by #126550
Assignees
Labels
:Core/Infra/Scripting Scripting abstractions, Painless, and Mustache Team:Core/Infra Meta label for core/infra team triaged Issue has been looked at, and is being left open

Comments

@ywangd
Copy link
Member

ywangd commented Apr 15, 2020

When mustache expression requests an item with a non-existent index from a Collection object, an IndexOutOfBoundsException will be thrown which in turn results in template execution failure.

The mustache java library does not support indexing into Collection object. This feature is provided by a custom CustomReflectionObjectHandler which wraps a Collection object inside a specialised map of class CustomReflectionObjectHandler#CollectionMap. The exception is thrown when checking whether the given index is available in the collection through the Map#containsKey method, whose implementation is simply trying to retrieve the item at given index. Therefore when the index does not exist, an IndexOutOfBoundsException will be thrown.

Mustache is in general very lenient of missing items. When an item does not exist, it will be simply rendered as empty string. Hence there are values to have a consistent behaviour for collection items. On the flipside, it is worth note that silently ignore all missing items could be a separate issue on its own.

@ywangd ywangd added :Core/Infra/Scripting Scripting abstractions, Painless, and Mustache team-discuss labels Apr 15, 2020
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (:Core/Infra/Scripting)

@ywangd ywangd changed the title Do not fail mustache template evaluation for non-existent array indices Mustache template evaluation failure for non-existent array indices Apr 15, 2020
@ywangd
Copy link
Member Author

ywangd commented Apr 17, 2020

Currentlty, an expression of a.b results into an empty string if a is a list. But a.9 results an MustacheException due to IndexOutOfBoundsException. This feels inconsistent to me.

The discussion point is around whether this failure behaviour is what we want or even intentional. Is it a bug since the exception happens when performing a key existence check (containsKey)?

@stu-elastic stu-elastic self-assigned this Apr 29, 2020
@rjernst rjernst added Team:Core/Infra Meta label for core/infra team and removed team-discuss labels May 4, 2020
@rjernst rjernst added the needs:triage Requires assignment of a team area label label Dec 3, 2020
@stu-elastic stu-elastic removed the needs:triage Requires assignment of a team area label label Dec 9, 2020
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

@arpadkiraly arpadkiraly removed their assignment Mar 11, 2024
@rjernst rjernst added the triaged Issue has been looked at, and is being left open label Apr 4, 2025
jdconrad added a commit that referenced this issue Apr 9, 2025
In mustache, this change returns null values which convert to empty strings 
instead of throwing an exception when users have a template with 
something like a.8 where the index 8 is out of bounds. This matches the 
behavior for non-existent keys like a.d.

Closes #55200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/Scripting Scripting abstractions, Painless, and Mustache Team:Core/Infra Meta label for core/infra team triaged Issue has been looked at, and is being left open
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants