From 4c07ff920b15f16d4e31008c57429217d0eb37d1 Mon Sep 17 00:00:00 2001 From: Marco Concetto Rudilosso Date: Wed, 2 Apr 2025 15:08:34 +0200 Subject: [PATCH 1/6] Revert "Always stringify properties in all array filters (#1936)" This reverts commit aa1640035f172bcbd064959d8e7d00aac07243d7. --- lib/liquid/standardfilters.rb | 11 ----------- lib/liquid/version.rb | 2 +- 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index daa2c580..1864742e 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -386,7 +386,6 @@ module Liquid 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) @@ -416,7 +415,6 @@ module Liquid 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) @@ -504,7 +502,6 @@ module Liquid 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 @@ -536,11 +533,6 @@ module Liquid # @liquid_syntax array | map: string # @liquid_return [array[untyped]] def map(input, property) - property = Utils.to_s(property) - - # Return the input array if property is empty (no-op) - return InputIterator.new(input, context).to_a if property.empty? - InputIterator.new(input, context).map do |e| e = e.call if e.is_a?(Proc) @@ -570,7 +562,6 @@ module Liquid 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 @@ -960,8 +951,6 @@ module Liquid # @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? diff --git a/lib/liquid/version.rb b/lib/liquid/version.rb index 854073d0..2eee338a 100644 --- a/lib/liquid/version.rb +++ b/lib/liquid/version.rb @@ -2,5 +2,5 @@ # frozen_string_literal: true module Liquid - VERSION = "5.8.4" + VERSION = "5.8.5" end From 4dae678c631dd73468d59bfd216ca506d62888ed Mon Sep 17 00:00:00 2001 From: Marco Concetto Rudilosso Date: Wed, 2 Apr 2025 15:08:42 +0200 Subject: [PATCH 2/6] Revert "Stringify properties before filtering (#1929)" This reverts commit f5d6a36574b7ddf5e1a00395f553a0f872fb9f16. --- lib/liquid/standardfilters.rb | 1 + test/integration/standard_filter_test.rb | 13 ------------- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 1864742e..3bbee4a5 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -979,6 +979,7 @@ module Liquid def filter_array(input, property, target_value, default_value = [], &block) ary = InputIterator.new(input, context) + return default_value if ary.empty? property = Utils.to_s(property) diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index 6a2737bb..62ec5d2d 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -1109,19 +1109,6 @@ class StandardFiltersTest < Minitest::Test assert_template_result(expected_output, template, { "array" => array }) end - def test_where_with_non_string_property - array = [ - { "handle" => "alpha", "{}" => true }, - { "handle" => "beta", "{}" => false }, - { "handle" => "gamma", "{}" => false }, - { "handle" => "delta", "{}" => true }, - ] - template = "{{ array | where: some_property, true | map: 'handle' | join: ' ' }}" - expected_output = "alpha delta" - - assert_template_result(expected_output, template, { "array" => array, "some_property" => {} }) - end - def test_where_string_keys input = [ "alpha", "beta", "gamma", "delta" From c6dcf3e714a2fe189a0b54c45efcd9cc29e49f50 Mon Sep 17 00:00:00 2001 From: Marco Concetto Rudilosso Date: Wed, 2 Apr 2025 15:09:28 +0200 Subject: [PATCH 3/6] add test --- test/integration/standard_filter_test.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index 62ec5d2d..6a2737bb 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -1109,6 +1109,19 @@ class StandardFiltersTest < Minitest::Test assert_template_result(expected_output, template, { "array" => array }) end + def test_where_with_non_string_property + array = [ + { "handle" => "alpha", "{}" => true }, + { "handle" => "beta", "{}" => false }, + { "handle" => "gamma", "{}" => false }, + { "handle" => "delta", "{}" => true }, + ] + template = "{{ array | where: some_property, true | map: 'handle' | join: ' ' }}" + expected_output = "alpha delta" + + assert_template_result(expected_output, template, { "array" => array, "some_property" => {} }) + end + def test_where_string_keys input = [ "alpha", "beta", "gamma", "delta" From b459eb656f95008ade68f76ac7c8816bb95e6cfd Mon Sep 17 00:00:00 2001 From: Marco Concetto Rudilosso Date: Wed, 2 Apr 2025 15:10:21 +0200 Subject: [PATCH 4/6] remove test --- test/integration/standard_filter_test.rb | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index 6a2737bb..62ec5d2d 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -1109,19 +1109,6 @@ class StandardFiltersTest < Minitest::Test assert_template_result(expected_output, template, { "array" => array }) end - def test_where_with_non_string_property - array = [ - { "handle" => "alpha", "{}" => true }, - { "handle" => "beta", "{}" => false }, - { "handle" => "gamma", "{}" => false }, - { "handle" => "delta", "{}" => true }, - ] - template = "{{ array | where: some_property, true | map: 'handle' | join: ' ' }}" - expected_output = "alpha delta" - - assert_template_result(expected_output, template, { "array" => array, "some_property" => {} }) - end - def test_where_string_keys input = [ "alpha", "beta", "gamma", "delta" From fc0f7f44c1026c9c5a0df095be30df55af358e26 Mon Sep 17 00:00:00 2001 From: Marco Concetto Rudilosso Date: Wed, 2 Apr 2025 17:52:13 +0200 Subject: [PATCH 5/6] use `Utils.to_s` on property error --- lib/liquid/standardfilters.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 3bbee4a5..7359062e 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -999,7 +999,7 @@ module Liquid end def raise_property_error(property) - raise Liquid::ArgumentError, "cannot select the property '#{property}'" + raise Liquid::ArgumentError, "cannot select the property '#{Utils.to_s(property)}'" end def apply_operation(input, operand, operation) From f676e699a4d707e19f780ea29f9add9971d91280 Mon Sep 17 00:00:00 2001 From: Marco Concetto Rudilosso Date: Mon, 14 Apr 2025 11:24:01 +0200 Subject: [PATCH 6/6] rollback changes to tests --- test/integration/standard_filter_test.rb | 31 ++++-------------------- 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index 62ec5d2d..6b931de5 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -560,26 +560,6 @@ class StandardFiltersTest < Minitest::Test end end - def test_map_with_nil_property - array = [ - { "handle" => "alpha", "value" => "A" }, - { "handle" => "beta", "value" => "B" }, - { "handle" => "gamma", "value" => "C" } - ] - - assert_template_result("alpha beta gamma", "{{ array | map: nil | map: 'handle' | join: ' ' }}", { "array" => array }) - end - - def test_map_with_empty_string_property - array = [ - { "handle" => "alpha", "value" => "A" }, - { "handle" => "beta", "value" => "B" }, - { "handle" => "gamma", "value" => "C" } - ] - - assert_template_result("alpha beta gamma", "{{ array | map: '' | map: 'handle' | join: ' ' }}", { "array" => array }) - end - def test_map_with_value_property array = [ { "handle" => "alpha", "value" => "A" }, @@ -591,16 +571,15 @@ class StandardFiltersTest < Minitest::Test end def test_map_returns_input_with_no_property - input = [ + foo = [ [1], [2], [3], ] - result = @filters.map(input, nil) - assert_equal(input.flatten, result) - result = @filters.map(input, '') - assert_equal(input.flatten, result) + assert_raises(Liquid::ArgumentError) do + @filters.map(foo, nil) + end end def test_sort_works_on_enumerables @@ -1317,7 +1296,7 @@ class StandardFiltersTest < Minitest::Test end def test_sum_with_non_string_property - input = [{ "true" => 1 }, { "1.0" => 0.2, "1" => -0.3 }, { "1..5" => 0.4 }] + 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))