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

Attempt accessing only valid item properties #6992

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/jekyll/filters.rb
Expand Up @@ -347,7 +347,7 @@ def item_property(item, property)
@item_property_cache[property][item] ||= begin
if item.respond_to?(:to_liquid)
property.to_s.split(".").reduce(item.to_liquid) do |subvalue, attribute|
parse_sort_input(subvalue[attribute])
parse_sort_input(access_property(subvalue, attribute))
end
elsif item.respond_to?(:data)
parse_sort_input(item.data[property.to_s])
Expand All @@ -357,10 +357,19 @@ def item_property(item, property)
end
end

def access_property(object, key)
return object unless object.respond_to?(:[])

accessor = parse_sort_input(key)
return object if object.is_a?(Array) && !accessor.is_a?(Integer)

object[accessor]
end

# return numeric values as numbers for proper sorting
def parse_sort_input(property)
number_like = %r!\A\s*-?(?:\d+\.?\d*|\.\d+)\s*\Z!
return property.to_f if property =~ number_like
return property.to_f if property.to_s =~ number_like

property
end
Expand Down
14 changes: 14 additions & 0 deletions test/test_filters.rb
Expand Up @@ -849,6 +849,20 @@ def to_liquid
assert_equal 2, @filter.where(array, "color", nil).length
end

should "filter array and string objects appropriately" do
Copy link
Member

Choose a reason for hiding this comment

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

Based on this test, the functionality feels too complicated to fix the bug. The most straightforward way I know to test this is to write something like:

should "not throw exception when an item doesn't have the property" do
  array = [{}, { "color" => nil }, { "color" => "" }, { "color" => "text" }, { "foo" => "bar" }]
  assert_equal [{ "color" => "text" }], @filter.where(hash, "color", "text")
end

Also, filtering on sub properties, as in #6939 (context.type), isn't valid syntax. This feels like we need a better reproduction case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Your test-case passes on master. The issue outlined in #6939 is actually due to #item_property encountering a nil element.

Modifying your test-case to the following (will fail on current master:

should "not throw exception when an item doesn't have the property" do
  array = [{}, { "color" => "text" }, { "foo" => "bar" }, nil]
  assert_equal [{ "color" => "text" }], @filter.where(array, "color", "text")
end

Another case that'll fail on master:

should "not throw exception when an item doesn't have the property" do
  array = [{}, { "color" => "text" }, [], { "foo" => "bar" }]
  assert_equal [{ "color" => "text" }], @filter.where(array, "color", "text")
end

hash = {
"a" => [%w(x y), nil],
"b" => ["tags", ["x"]],
"c" => ["x", %w(y z)],
"d" => nil,
"e" => "x",
}
assert_equal [["x", %w(y z)], "x"], @filter.where(hash, 0, "x")
assert_equal [["tags", ["x"]]], @filter.where(hash, 0, "tags")
assert_equal [["tags", ["x"]]], @filter.where(hash, "0", "tags")
assert_equal [], @filter.where(hash, "tags", "0")
end

should "filter array properties appropriately" do
hash = {
"a" => { "tags"=>%w(x y) },
Expand Down