From fbdaf83df23bab15bf389228371923bd65f4c5de Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Wed, 9 Aug 2023 16:35:11 -0400 Subject: [PATCH] Committing test case --- .../jinjava/lib/fn/eager/EagerMacroFunction.java | 11 ++++++++++- src/test/java/com/hubspot/jinjava/EagerTest.java | 7 +++++++ ...llows-modification-in-aliased-macro.expected.jinja | 6 ++++++ .../eager/allows-modification-in-aliased-macro.jinja | 6 ++++++ src/test/resources/tags/macrotag/eager/settings.jinja | 5 +++++ 5 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/eager/allows-modification-in-aliased-macro.expected.jinja create mode 100644 src/test/resources/eager/allows-modification-in-aliased-macro.jinja create mode 100644 src/test/resources/tags/macrotag/eager/settings.jinja diff --git a/src/main/java/com/hubspot/jinjava/lib/fn/eager/EagerMacroFunction.java b/src/main/java/com/hubspot/jinjava/lib/fn/eager/EagerMacroFunction.java index 7e56b7e9a..6893097fd 100644 --- a/src/main/java/com/hubspot/jinjava/lib/fn/eager/EagerMacroFunction.java +++ b/src/main/java/com/hubspot/jinjava/lib/fn/eager/EagerMacroFunction.java @@ -71,6 +71,15 @@ public Object doEvaluate( interpreter ); if (!result.getResult().isFullyResolved()) { + if ( + result + .getSpeculativeBindings() + .keySet() + .stream() + .anyMatch(key -> localContextScope.getScope().containsKey(key)) + ) { + throw new DeferredValueException("e"); + } result = eagerEvaluateInDeferredExecutionMode( () -> getEvaluationResultDirectly(argMap, kwargMap, varArgs, interpreter), @@ -294,7 +303,7 @@ private String getSetTagForAliasedVariables(String fullName) { .getCombinedScope() .entrySet() .stream() - .filter(entry -> entry.getValue() instanceof DeferredValue) + // .filter(entry -> entry.getValue() instanceof DeferredValue) .map(Entry::getKey) .collect(Collectors.toMap(Function.identity(), name -> aliasName + name)); return EagerReconstructionUtils.buildSetTag( diff --git a/src/test/java/com/hubspot/jinjava/EagerTest.java b/src/test/java/com/hubspot/jinjava/EagerTest.java index 618f08c4b..532e4593a 100644 --- a/src/test/java/com/hubspot/jinjava/EagerTest.java +++ b/src/test/java/com/hubspot/jinjava/EagerTest.java @@ -1499,4 +1499,11 @@ public void itAllowsVariableSharingAliasName() { "allows-variable-sharing-alias-name" ); } + + @Test + public void itAllowsModificationInAliasedMacro() { + expectedTemplateInterpreter.assertExpectedOutput( + "allows-modification-in-aliased-macro" + ); + } } diff --git a/src/test/resources/eager/allows-modification-in-aliased-macro.expected.jinja b/src/test/resources/eager/allows-modification-in-aliased-macro.expected.jinja new file mode 100644 index 000000000..4d01c9c84 --- /dev/null +++ b/src/test/resources/eager/allows-modification-in-aliased-macro.expected.jinja @@ -0,0 +1,6 @@ +{% import 'eager/settings.jinja' as shared %} + +{% if deferred %} +{{ shared.load_settings() }} +{% endif %} +{{ shared.settings }} diff --git a/src/test/resources/eager/allows-modification-in-aliased-macro.jinja b/src/test/resources/eager/allows-modification-in-aliased-macro.jinja new file mode 100644 index 000000000..4d01c9c84 --- /dev/null +++ b/src/test/resources/eager/allows-modification-in-aliased-macro.jinja @@ -0,0 +1,6 @@ +{% import 'eager/settings.jinja' as shared %} + +{% if deferred %} +{{ shared.load_settings() }} +{% endif %} +{{ shared.settings }} diff --git a/src/test/resources/tags/macrotag/eager/settings.jinja b/src/test/resources/tags/macrotag/eager/settings.jinja new file mode 100644 index 000000000..7c9feac3c --- /dev/null +++ b/src/test/resources/tags/macrotag/eager/settings.jinja @@ -0,0 +1,5 @@ +{% set settings = {} %} + +{% macro load_settings() %} +{% do settings.put('foo', 'bar') %} +{% endmacro %}