Skip to content

Commit 2b75bfa

Browse files
ianksMaaarcocr
andauthored
Fix regression when using empty/nil properties with array filters (#1944)
* Make all array filters that use `filter_array` util process empty string and nil correctly * up version * fix ordering of checks * also do it for map * Do not raise property error * Gracefully empty property in map filter --------- Co-authored-by: Marco Concetto Rudilosso <[email protected]>
1 parent 87bc6e7 commit 2b75bfa

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

lib/liquid/standardfilters.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require 'cgi'
44
require 'base64'
55
require 'bigdecimal'
6-
76
module Liquid
87
module StandardFilters
98
MAX_I32 = (1 << 31) - 1
@@ -538,6 +537,10 @@ def reverse(input)
538537
# @liquid_return [array[untyped]]
539538
def map(input, property)
540539
property = Utils.to_s(property)
540+
541+
# Return the input array if property is empty (no-op)
542+
return InputIterator.new(input, context).to_a if property.empty?
543+
541544
InputIterator.new(input, context).map do |e|
542545
e = e.call if e.is_a?(Proc)
543546

@@ -986,12 +989,11 @@ def sum(input, property = nil)
986989
attr_reader :context
987990

988991
def filter_array(input, property, target_value, default_value = [], &block)
989-
property = Liquid::Utils.to_s(property)
990-
return default_value if property.empty?
991-
992992
ary = InputIterator.new(input, context)
993993
return default_value if ary.empty?
994994

995+
property = Utils.to_s(property)
996+
995997
block.call(ary) do |item|
996998
if target_value.nil?
997999
item[property]

lib/liquid/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
# frozen_string_literal: true
33

44
module Liquid
5-
VERSION = "5.8.2"
5+
VERSION = "5.8.3"
66
end

test/integration/standard_filter_test.rb

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,15 +560,47 @@ def test_map_returns_empty_on_2d_input_array
560560
end
561561
end
562562

563-
def test_map_returns_empty_with_no_property
564-
foo = [
563+
def test_map_with_nil_property
564+
array = [
565+
{ "handle" => "alpha", "value" => "A" },
566+
{ "handle" => "beta", "value" => "B" },
567+
{ "handle" => "gamma", "value" => "C" }
568+
]
569+
570+
assert_template_result("alpha beta gamma", "{{ array | map: nil | map: 'handle' | join: ' ' }}", { "array" => array })
571+
end
572+
573+
def test_map_with_empty_string_property
574+
array = [
575+
{ "handle" => "alpha", "value" => "A" },
576+
{ "handle" => "beta", "value" => "B" },
577+
{ "handle" => "gamma", "value" => "C" }
578+
]
579+
580+
assert_template_result("alpha beta gamma", "{{ array | map: '' | map: 'handle' | join: ' ' }}", { "array" => array })
581+
end
582+
583+
def test_map_with_value_property
584+
array = [
585+
{ "handle" => "alpha", "value" => "A" },
586+
{ "handle" => "beta", "value" => "B" },
587+
{ "handle" => "gamma", "value" => "C" }
588+
]
589+
590+
assert_template_result("A B C", "{{ array | map: 'value' | join: ' ' }}", { "array" => array })
591+
end
592+
593+
def test_map_returns_input_with_no_property
594+
input = [
565595
[1],
566596
[2],
567597
[3],
568598
]
569-
assert_raises(Liquid::ArgumentError) do
570-
@filters.map(foo, nil)
571-
end
599+
result = @filters.map(input, nil)
600+
assert_equal(input.flatten, result)
601+
602+
result = @filters.map(input, '')
603+
assert_equal(input.flatten, result)
572604
end
573605

574606
def test_sort_works_on_enumerables
@@ -1033,6 +1065,22 @@ def test_where
10331065
assert_template_result(expected_output, template, { "array" => array })
10341066
end
10351067

1068+
def test_where_with_empty_string_is_a_no_op
1069+
environment = { "array" => ["alpha", "beta", "gamma"] }
1070+
expected_output = "alpha beta gamma"
1071+
template = "{{ array | where: '' | join: ' ' }}"
1072+
1073+
assert_template_result(expected_output, template, environment)
1074+
end
1075+
1076+
def test_where_with_nil_is_a_no_op
1077+
environment = { "array" => ["alpha", "beta", "gamma"] }
1078+
expected_output = "alpha beta gamma"
1079+
template = "{{ array | where: nil | join: ' ' }}"
1080+
1081+
assert_template_result(expected_output, template, environment)
1082+
end
1083+
10361084
def test_where_with_value
10371085
array = [
10381086
{ "handle" => "alpha", "ok" => true },

0 commit comments

Comments
 (0)