mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
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]>
This commit is contained in:
co-authored by
Marco Concetto Rudilosso
parent
87bc6e7cfa
commit
2b75bfaff4
@@ -3,7 +3,6 @@
|
|||||||
require 'cgi'
|
require 'cgi'
|
||||||
require 'base64'
|
require 'base64'
|
||||||
require 'bigdecimal'
|
require 'bigdecimal'
|
||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
module StandardFilters
|
module StandardFilters
|
||||||
MAX_I32 = (1 << 31) - 1
|
MAX_I32 = (1 << 31) - 1
|
||||||
@@ -538,6 +537,10 @@ module Liquid
|
|||||||
# @liquid_return [array[untyped]]
|
# @liquid_return [array[untyped]]
|
||||||
def map(input, property)
|
def map(input, property)
|
||||||
property = Utils.to_s(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|
|
InputIterator.new(input, context).map do |e|
|
||||||
e = e.call if e.is_a?(Proc)
|
e = e.call if e.is_a?(Proc)
|
||||||
|
|
||||||
@@ -986,12 +989,11 @@ module Liquid
|
|||||||
attr_reader :context
|
attr_reader :context
|
||||||
|
|
||||||
def filter_array(input, property, target_value, default_value = [], &block)
|
def filter_array(input, property, target_value, default_value = [], &block)
|
||||||
property = Liquid::Utils.to_s(property)
|
|
||||||
return default_value if property.empty?
|
|
||||||
|
|
||||||
ary = InputIterator.new(input, context)
|
ary = InputIterator.new(input, context)
|
||||||
return default_value if ary.empty?
|
return default_value if ary.empty?
|
||||||
|
|
||||||
|
property = Utils.to_s(property)
|
||||||
|
|
||||||
block.call(ary) do |item|
|
block.call(ary) do |item|
|
||||||
if target_value.nil?
|
if target_value.nil?
|
||||||
item[property]
|
item[property]
|
||||||
|
|||||||
@@ -2,5 +2,5 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
VERSION = "5.8.2"
|
VERSION = "5.8.3"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -560,15 +560,47 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_map_returns_empty_with_no_property
|
def test_map_with_nil_property
|
||||||
foo = [
|
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" },
|
||||||
|
{ "handle" => "beta", "value" => "B" },
|
||||||
|
{ "handle" => "gamma", "value" => "C" }
|
||||||
|
]
|
||||||
|
|
||||||
|
assert_template_result("A B C", "{{ array | map: 'value' | join: ' ' }}", { "array" => array })
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_map_returns_input_with_no_property
|
||||||
|
input = [
|
||||||
[1],
|
[1],
|
||||||
[2],
|
[2],
|
||||||
[3],
|
[3],
|
||||||
]
|
]
|
||||||
assert_raises(Liquid::ArgumentError) do
|
result = @filters.map(input, nil)
|
||||||
@filters.map(foo, nil)
|
assert_equal(input.flatten, result)
|
||||||
end
|
|
||||||
|
result = @filters.map(input, '')
|
||||||
|
assert_equal(input.flatten, result)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_sort_works_on_enumerables
|
def test_sort_works_on_enumerables
|
||||||
@@ -1033,6 +1065,22 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_template_result(expected_output, template, { "array" => array })
|
assert_template_result(expected_output, template, { "array" => array })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_where_with_empty_string_is_a_no_op
|
||||||
|
environment = { "array" => ["alpha", "beta", "gamma"] }
|
||||||
|
expected_output = "alpha beta gamma"
|
||||||
|
template = "{{ array | where: '' | join: ' ' }}"
|
||||||
|
|
||||||
|
assert_template_result(expected_output, template, environment)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_where_with_nil_is_a_no_op
|
||||||
|
environment = { "array" => ["alpha", "beta", "gamma"] }
|
||||||
|
expected_output = "alpha beta gamma"
|
||||||
|
template = "{{ array | where: nil | join: ' ' }}"
|
||||||
|
|
||||||
|
assert_template_result(expected_output, template, environment)
|
||||||
|
end
|
||||||
|
|
||||||
def test_where_with_value
|
def test_where_with_value
|
||||||
array = [
|
array = [
|
||||||
{ "handle" => "alpha", "ok" => true },
|
{ "handle" => "alpha", "ok" => true },
|
||||||
|
|||||||
Reference in New Issue
Block a user