Skip to content

Commit

Permalink
Also allow {% if obj[missing] %} checks in SANE strictVariablesMode
Browse files Browse the repository at this point in the history
Previously, only "obj.missing" was allowed.
  • Loading branch information
kohlschuetter committed Dec 27, 2023
1 parent 9afde46 commit 6f40bda
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/main/java/liqp/nodes/LookupNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,13 +54,14 @@ public Object render(TemplateContext context) {
}
}

boolean foundAllButLastOne = false;
boolean foundAllButLastOne = okIfMissing;
for (Iterator<Indexable> 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 {
Expand Down Expand Up @@ -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();
Expand Down
31 changes: 29 additions & 2 deletions src/test/java/liqp/blocks/IfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

0 comments on commit 6f40bda

Please sign in to comment.