From 6f40bda29af12a777709c06089d9c0be5677e60d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Kohlschu=CC=88tter?= Date: Wed, 27 Dec 2023 19:13:33 +0100 Subject: [PATCH] Also allow {% if obj[missing] %} checks in SANE strictVariablesMode Previously, only "obj.missing" was allowed. --- src/main/java/liqp/nodes/LookupNode.java | 12 +++++++-- src/test/java/liqp/blocks/IfTest.java | 31 ++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/main/java/liqp/nodes/LookupNode.java b/src/main/java/liqp/nodes/LookupNode.java index 5df3a3a9..dd157b2c 100644 --- a/src/main/java/liqp/nodes/LookupNode.java +++ b/src/main/java/liqp/nodes/LookupNode.java @@ -28,7 +28,10 @@ public void add(Indexable indexable) { @Override public Object render(TemplateContext context) { + return render(context, false); + } + public Object render(TemplateContext context, boolean okIfMissing) { Object value = null; String realId; @@ -51,13 +54,14 @@ public Object render(TemplateContext context) { } } - boolean foundAllButLastOne = false; + boolean foundAllButLastOne = okIfMissing; for (Iterator it = indexes.iterator(); it.hasNext();) { Indexable index = it.next(); if (it.hasNext()) { value = index.get(value, context, found); if (value == null) { found.set(false); + foundAllButLastOne = false; break; } } else { @@ -212,7 +216,11 @@ public Object get(Object value, TemplateContext context, AtomicBoolean found) { return null; } - key = expression.render(context); + if (expression instanceof LookupNode) { + key = ((LookupNode)expression).render(context, true); + } else { + key = expression.render(context); + } if (key instanceof Number) { int index = ((Number)key).intValue(); diff --git a/src/test/java/liqp/blocks/IfTest.java b/src/test/java/liqp/blocks/IfTest.java index 8bb3204a..34853ef0 100644 --- a/src/test/java/liqp/blocks/IfTest.java +++ b/src/test/java/liqp/blocks/IfTest.java @@ -427,23 +427,50 @@ public void strictVariablesTest() throws RecognitionException { "{% if obj.missing %}true{% else %}false{% endif %}").render(Collections.singletonMap( "obj", Collections.singletonMap("var", "val"))); }); + assertThrows(VariableNotExistException.class, () -> { + new TemplateParser.Builder().withStrictVariables(StrictVariablesMode.STRICT).build().parse( + "{% if obj.nested.missing %}true{% else %}false{% endif %}").render(Collections.singletonMap( + "obj", Collections.singletonMap("var", "val"))); + }); + assertThrows(VariableNotExistException.class, () -> { + new TemplateParser.Builder().withStrictVariables(StrictVariablesMode.STRICT).build().parse( + "{% if obj[missing] %}true{% else %}false{% endif %}").render(Collections.singletonMap( + "obj", Collections.singletonMap("var", "val"))); + }); assertThat(new TemplateParser.Builder().withStrictVariables(StrictVariablesMode.SANE).build() .parse("{% if obj.missing %}true{% else %}false{% endif %}").render(Collections.singletonMap( "obj", Collections.singletonMap("var", "val"))), is("false")); - + assertThat(new TemplateParser.Builder().withStrictVariables(StrictVariablesMode.SANE).build() + .parse("{% if obj[missing] %}true{% else %}false{% endif %}").render(Collections.singletonMap( + "obj", Collections.singletonMap("var", "val"))), is("false")); assertThrows(VariableNotExistException.class, () -> { new TemplateParser.Builder().withStrictVariables(StrictVariablesMode.SANE).build().parse( "{% if obj.nested.missing %}true{% else %}false{% endif %}").render(Collections .singletonMap("obj", Collections.singletonMap("var", "val"))); }); + assertThrows(VariableNotExistException.class, () -> { + new TemplateParser.Builder().withStrictVariables(StrictVariablesMode.SANE).build().parse( + "{% if obj[nested].missing %}true{% else %}false{% endif %}").render(Collections + .singletonMap("obj", Collections.singletonMap("var", "val"))); + }); + assertThrows(VariableNotExistException.class, () -> { + new TemplateParser.Builder().withStrictVariables(StrictVariablesMode.SANE).build().parse( + "{% if obj[nested][missing] %}true{% else %}false{% endif %}").render(Collections + .singletonMap("obj", Collections.singletonMap("var", "val"))); + }); assertThat(new TemplateParser.Builder().withStrictVariables(StrictVariablesMode.OFF).build() .parse("{% if obj.missing %}true{% else %}false{% endif %}").render(Collections.singletonMap( "obj", Collections.singletonMap("var", "val"))), is("false")); - assertThat(new TemplateParser.Builder().withStrictVariables(StrictVariablesMode.OFF).build() .parse("{% if obj.nested.missing %}true{% else %}false{% endif %}").render(Collections .singletonMap("obj", Collections.singletonMap("var", "val"))), is("false")); + assertThat(new TemplateParser.Builder().withStrictVariables(StrictVariablesMode.OFF).build() + .parse("{% if obj[nested].missing %}true{% else %}false{% endif %}").render(Collections + .singletonMap("obj", Collections.singletonMap("var", "val"))), is("false")); + assertThat(new TemplateParser.Builder().withStrictVariables(StrictVariablesMode.OFF).build() + .parse("{% if obj[nested][missing] %}true{% else %}false{% endif %}").render(Collections + .singletonMap("obj", Collections.singletonMap("var", "val"))), is("false")); } }