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

Always stringify properties in all array filters #1936

Merged
merged 5 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/liquid/standardfilters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ def sort(input, property = nil)
end
elsif ary.all? { |el| el.respond_to?(:[]) }
begin
property = Utils.to_s(property)
ary.sort { |a, b| nil_safe_compare(a[property], b[property]) }
rescue TypeError
raise_property_error(property)
Expand Down Expand Up @@ -416,6 +417,7 @@ def sort_natural(input, property = nil)
end
elsif ary.all? { |el| el.respond_to?(:[]) }
begin
property = Utils.to_s(property)
ary.sort { |a, b| nil_safe_casecmp(a[property], b[property]) }
rescue TypeError
raise_property_error(property)
Expand Down Expand Up @@ -503,6 +505,7 @@ def uniq(input, property = nil)
elsif ary.empty? # The next two cases assume a non-empty array.
[]
else
property = Utils.to_s(property)
ary.uniq do |item|
item[property]
rescue TypeError
Expand Down Expand Up @@ -534,6 +537,7 @@ def reverse(input)
# @liquid_syntax array | map: string
# @liquid_return [array[untyped]]
def map(input, property)
property = Utils.to_s(property)
InputIterator.new(input, context).map do |e|
e = e.call if e.is_a?(Proc)

Expand Down Expand Up @@ -563,6 +567,7 @@ def compact(input, property = nil)
elsif ary.empty? # The next two cases assume a non-empty array.
[]
else
property = Liquid::Utils.to_s(property)
ary.reject do |item|
item[property].nil?
rescue TypeError
Expand Down Expand Up @@ -952,6 +957,8 @@ def default(input, default_value = '', options = {})
# @liquid_syntax array | sum
# @liquid_return [number]
def sum(input, property = nil)
property = property.nil? ? nil : Utils.to_s(property)

ary = InputIterator.new(input, context)
return 0 if ary.empty?

Expand Down
2 changes: 1 addition & 1 deletion lib/liquid/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# frozen_string_literal: true

module Liquid
VERSION = "5.8.1"
VERSION = "5.8.2"
end
11 changes: 11 additions & 0 deletions test/integration/standard_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,17 @@ def test_sum_with_floats_and_indexable_map_values
assert_template_result("0", "{{ input | sum: 'subtotal' }}", { "input" => input })
end

def test_sum_with_non_string_property
input = [{ "true" => 1 }, { "1.0" => 0.2, "1" => -0.3 }, { "1..5" => 0.4 }]

assert_equal(1, @filters.sum(input, true))
assert_equal(0.2, @filters.sum(input, 1.0))
assert_equal(-0.3, @filters.sum(input, 1))
assert_equal(0.4, @filters.sum(input, (1..5)))
assert_equal(0, @filters.sum(input, nil))
assert_equal(0, @filters.sum(input, ""))
end

private

def with_timezone(tz)
Expand Down
Loading