Fix include tag used with strict_variables (#829)

Fixes https://github.com/Shopify/liquid/issues/828
This commit is contained in:
Lasse Skindstad Ebert
2017-03-22 21:00:31 +01:00
committed by Dylan Thacker-Smith
parent 22f2cec5de
commit 5149cde5c3
4 changed files with 14 additions and 6 deletions

View File

@@ -24,6 +24,7 @@
* Liquid::Template.register_filter raises when the module overrides registered public methods as private or protected (#705) [Gaurav Chande]
### Fixed
* Fix include tag used with strict_variables (#828) [QuickPay]
* Fix map filter when value is a Proc (#672) [Guillaume Malette]
* Fix truncate filter when value is not a string (#672) [Guillaume Malette]
* Fix behaviour of escape filter when input is nil (#665) [Tanel Jakobsoo]

View File

@@ -160,7 +160,7 @@ module Liquid
end
# Fetches an object starting at the local scope and then moving up the hierachy
def find_variable(key)
def find_variable(key, raise_on_not_found: true)
# This was changed from find() to find_index() because this is a very hot
# path and find_index() is optimized in MRI to reduce object allocation
index = @scopes.find_index { |s| s.key?(key) }
@@ -170,7 +170,7 @@ module Liquid
if scope.nil?
@environments.each do |e|
variable = lookup_and_evaluate(e, key)
variable = lookup_and_evaluate(e, key, raise_on_not_found: raise_on_not_found)
unless variable.nil?
scope = e
break
@@ -179,7 +179,7 @@ module Liquid
end
scope ||= @environments.last || @scopes.last
variable ||= lookup_and_evaluate(scope, key)
variable ||= lookup_and_evaluate(scope, key, raise_on_not_found: raise_on_not_found)
variable = variable.to_liquid
variable.context = self if variable.respond_to?(:context=)
@@ -187,8 +187,8 @@ module Liquid
variable
end
def lookup_and_evaluate(obj, key)
if @strict_variables && obj.respond_to?(:key?) && !obj.key?(key)
def lookup_and_evaluate(obj, key, raise_on_not_found: true)
if @strict_variables && raise_on_not_found && obj.respond_to?(:key?) && !obj.key?(key)
raise Liquid::UndefinedVariable, "undefined variable #{key}"
end

View File

@@ -50,7 +50,7 @@ module Liquid
variable = if @variable_name_expr
context.evaluate(@variable_name_expr)
else
context.find_variable(template_name)
context.find_variable(template_name, raise_on_not_found: false)
end
old_template_name = context.template_name

View File

@@ -235,4 +235,11 @@ class IncludeTagTest < Minitest::Test
assert_template_result "Product: Draft 151cm ", "{% assign page = 'product' %}{% include page for foo %}", "foo" => { 'title' => 'Draft 151cm' }
end
def test_including_with_strict_variables
template = Liquid::Template.parse("{% include 'simple' %}", error_mode: :warn)
template.render(nil, strict_variables: true)
assert_equal [], template.errors
end
end # IncludeTagTest