diff --git a/src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java b/src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java index 9e6cd9a17..89fbcaebf 100644 --- a/src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java +++ b/src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java @@ -202,6 +202,10 @@ public int hashCode() { ); } + public MacroFunction cloneWithNewName(String name) { + return new MacroFunction(this, name); + } + private boolean alreadyDeferredInEarlierCall( String key, JinjavaInterpreter interpreter 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 9e805daa3..bea14deb2 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 @@ -58,6 +58,10 @@ public EagerMacroFunction( ); } + EagerMacroFunction(MacroFunction source, String name) { + super(source, name); + } + public Object doEvaluate( Map argMap, Map kwargMap, @@ -334,4 +338,9 @@ public boolean equals(Object o) { public int hashCode() { return super.hashCode(); } + + @Override + public EagerMacroFunction cloneWithNewName(String name) { + return new EagerMacroFunction(this, name); + } } diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java index ce00eceef..8bbb4df65 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java @@ -170,7 +170,7 @@ public static boolean integrateChild( if (val != null) { MacroFunction toImport = (MacroFunction) val; if (!importMapping.getKey().equals(importMapping.getValue())) { - toImport = new MacroFunction(toImport, importMapping.getValue()); + toImport = toImport.cloneWithNewName(importMapping.getValue()); } interpreter.getContext().addGlobalMacro(toImport); } else { diff --git a/src/test/java/com/hubspot/jinjava/EagerTest.java b/src/test/java/com/hubspot/jinjava/EagerTest.java index d62d359a2..713d15fca 100644 --- a/src/test/java/com/hubspot/jinjava/EagerTest.java +++ b/src/test/java/com/hubspot/jinjava/EagerTest.java @@ -1634,4 +1634,9 @@ public void prepareContext(Context context) { "keeps-meta-context-variables-through-import/test" ); } + + @Test + public void itReconstructsFromedMacro() { + expectedTemplateInterpreter.assertExpectedOutput("reconstructs-fromed-macro/test"); + } } diff --git a/src/test/resources/eager/handles-deferred-modification-in-caller/test.expected.jinja b/src/test/resources/eager/handles-deferred-modification-in-caller/test.expected.jinja index 7047aafb9..5933433f9 100644 --- a/src/test/resources/eager/handles-deferred-modification-in-caller/test.expected.jinja +++ b/src/test/resources/eager/handles-deferred-modification-in-caller/test.expected.jinja @@ -1,7 +1,10 @@ {% set my_list = ['a', 'b'] %}\ {% set __macro_callerino_729568755_temp_variable_0__ %}\ +{% set __macro_caller_172086791_temp_variable_0__ %}\ {% do my_list.append(deferred) %}\ {{ my_list }}\ +{% endset %}\ +{{ __macro_caller_172086791_temp_variable_0__ }}\ {% do my_list.append('d') %}\ {% endset %}\ {% call __macro_callerino_729568755_temp_variable_0__ %}\ diff --git a/src/test/resources/eager/reconstructs-fromed-macro/has-macro.jinja b/src/test/resources/eager/reconstructs-fromed-macro/has-macro.jinja new file mode 100644 index 000000000..f08325779 --- /dev/null +++ b/src/test/resources/eager/reconstructs-fromed-macro/has-macro.jinja @@ -0,0 +1,3 @@ +{% macro upper(param) %} + {{ param|upper }} +{% endmacro %} \ No newline at end of file diff --git a/src/test/resources/eager/reconstructs-fromed-macro/test.expected.jinja b/src/test/resources/eager/reconstructs-fromed-macro/test.expected.jinja new file mode 100644 index 000000000..6c934e61a --- /dev/null +++ b/src/test/resources/eager/reconstructs-fromed-macro/test.expected.jinja @@ -0,0 +1,3 @@ +{% from './has-macro.jinja' import upper as to_upper %} + +{{ to_upper(deferred) }} \ No newline at end of file diff --git a/src/test/resources/eager/reconstructs-fromed-macro/test.jinja b/src/test/resources/eager/reconstructs-fromed-macro/test.jinja new file mode 100644 index 000000000..6c934e61a --- /dev/null +++ b/src/test/resources/eager/reconstructs-fromed-macro/test.jinja @@ -0,0 +1,3 @@ +{% from './has-macro.jinja' import upper as to_upper %} + +{{ to_upper(deferred) }} \ No newline at end of file