Skip to content

Commit cbd8a0a

Browse files
authored
Merge pull request #2007 from Shopify/gg-change-key-behavior
Don't raise if no variable found when `using context.key?` with `strict_variables`
2 parents 248f3a4 + 9973f33 commit cbd8a0a

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/liquid/context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def [](expression)
184184
end
185185

186186
def key?(key)
187-
self[key] != nil
187+
find_variable(key, raise_on_not_found: false) != nil
188188
end
189189

190190
def evaluate(object)

test/integration/context_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,21 @@ def test_has_key_will_not_add_an_error_for_missing_keys
639639
end
640640
end
641641

642+
def test_key_lookup_will_raise_for_missing_keys_when_strict_variables_is_enabled
643+
context = Context.new
644+
context.strict_variables = true
645+
assert_raises(Liquid::UndefinedVariable) do
646+
context['unknown']
647+
end
648+
end
649+
650+
def test_has_key_will_not_raise_for_missing_keys_when_strict_variables_is_enabled
651+
context = Context.new
652+
context.strict_variables = true
653+
refute(context.key?('unknown'))
654+
assert_empty(context.errors)
655+
end
656+
642657
def test_context_always_uses_static_registers
643658
registers = {
644659
my_register: :my_value,

0 commit comments

Comments
 (0)