From 299adf9a3a27eb8b601930f6898a38cda713ed6c Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Mon, 13 Jan 2025 13:57:02 -0500 Subject: [PATCH] Don't make new call stacks when interpreting a CallTag so that the max render depth is tracked properly --- .../com/hubspot/jinjava/lib/tag/CallTag.java | 2 +- .../hubspot/jinjava/lib/tag/CallTagTest.java | 25 +++++++++++++++++++ .../resources/tags/calltag/multiple.jinja | 4 +++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/tags/calltag/multiple.jinja diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/CallTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/CallTag.java index b6107a864..fa4294e87 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/CallTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/CallTag.java @@ -67,7 +67,7 @@ public String getName() { public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) { String macroExpr = "{{" + tagNode.getHelpers().trim() + "}}"; - try (InterpreterScopeClosable c = interpreter.enterScope()) { + try (InterpreterScopeClosable c = interpreter.enterNonStackingScope()) { LinkedHashMap args = new LinkedHashMap<>(); MacroFunction caller = new MacroFunction( tagNode.getChildren(), diff --git a/src/test/java/com/hubspot/jinjava/lib/tag/CallTagTest.java b/src/test/java/com/hubspot/jinjava/lib/tag/CallTagTest.java index 76d419d8b..4a4dbd3ab 100644 --- a/src/test/java/com/hubspot/jinjava/lib/tag/CallTagTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/tag/CallTagTest.java @@ -4,6 +4,9 @@ import com.google.common.io.Resources; import com.hubspot.jinjava.BaseInterpretingTest; +import com.hubspot.jinjava.Jinjava; +import com.hubspot.jinjava.JinjavaConfig; +import com.hubspot.jinjava.interpret.JinjavaInterpreter; import java.io.IOException; import java.nio.charset.StandardCharsets; import org.jsoup.Jsoup; @@ -20,6 +23,28 @@ public void testSimpleFn() { .isEqualTo("This is a simple dialog rendered by using a macro and a call block."); } + @Test + public void itDoesNotDoubleCountCallTagTowardsDepth() throws IOException { + interpreter = + new Jinjava( + JinjavaConfig + .newBuilder() + .withEnableRecursiveMacroCalls(true) + .withMaxMacroRecursionDepth(6) // There are 3 call tags, but a total of 6 "macro" calls happening in this file as each call to `caller()` counts too + .build() + ) + .newInterpreter(); + JinjavaInterpreter.pushCurrent(interpreter); + + try { + String template = fixture("multiple"); + interpreter.render(template); + assertThat(interpreter.getErrorsCopy()).isEmpty(); + } finally { + JinjavaInterpreter.popCurrent(); + } + } + private String fixture(String name) { try { return Resources.toString( diff --git a/src/test/resources/tags/calltag/multiple.jinja b/src/test/resources/tags/calltag/multiple.jinja new file mode 100644 index 000000000..2bbd3beae --- /dev/null +++ b/src/test/resources/tags/calltag/multiple.jinja @@ -0,0 +1,4 @@ +{% macro test1() %}1{{ caller() }}{% endmacro %} +{% macro test2() %}2{{ caller() }}{% endmacro %} +{% macro test3() %}3{{ caller() }}{% endmacro %} +{% call test1() %}{% call test2() %}{% call test3() %}{% endcall %}{% endcall %}{% endcall %} \ No newline at end of file