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

Stringify properties before filtering arrays #1929

Merged
merged 1 commit into from
Mar 17, 2025
Merged

Conversation

ianks
Copy link
Contributor

@ianks ianks commented Mar 13, 2025

Previously, filtering code allowed would blindly pass the property when calling #[] on the target input. While this usually works, it falls apart with non-string keys and can lead to some strange behaviors:

template = Liquid::Template.parse("{{ input | where: property, 0 }}")
template.render({ "input" => [5, 10, 15, 20], "property" => 1 }) # => "520"
template.render({ "input" => [5, 10, 15, 20], "property" => 0 }) # => "1020"

Let's just always coerce properties to strings, first.

@ianks ianks requested review from karreiro and Maaarcocr March 13, 2025 22:33
Copy link
Contributor

@karreiro karreiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR, @ianks!

We have a minor issue in this scenario:

{% assign fruits = 'banana apple tomato' | split: ' ' %}
{{ fruits | where: '' }}

With that fixed, I believe we're going to be ready to merge :)

@jg-rp
Copy link
Contributor

jg-rp commented Mar 14, 2025

it falls apart with non-string keys and can lead to some strange behaviors

It took me a while to figure out what was going on there, and I'd been struggling to explain this example too.

require 'liquid'

source = <<LIQUID
{{ some_array | find: 3 }}
{{ some_array | find_index: 1 }}
LIQUID

template = Liquid::Template.parse(source, error_mode: :lax)
puts template.render!("some_array" => [-1, 42, 1, 2, 3, 4])

I had no idea that Integer#[] was a bit access method. 😃

@ianks
Copy link
Contributor Author

ianks commented Mar 14, 2025

Verified this change against shadowed traffic, and this change did not impact any existing Liquid. So it should be safe to change this behavior.

@ianks ianks requested a review from ggmichaelgo March 14, 2025 20:30
Copy link
Contributor

@karreiro karreiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for verifying, @ianks!

@ianks ianks merged commit f5d6a36 into main Mar 17, 2025
23 checks passed
@ianks ianks deleted the filter-array-property-key branch March 17, 2025 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants