Skip to content

Commit 62bf49b

Browse files
committed
Add cloneWithNewName to let successfully from-imported macros be
imported as EagerMacroFunction to avoid ClassCastException
1 parent e212f2f commit 62bf49b

File tree

8 files changed

+34
-1
lines changed

8 files changed

+34
-1
lines changed

src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ public int hashCode() {
202202
);
203203
}
204204

205+
public MacroFunction cloneWithNewName(String name) {
206+
return new MacroFunction(this, name);
207+
}
208+
205209
private boolean alreadyDeferredInEarlierCall(
206210
String key,
207211
JinjavaInterpreter interpreter

src/main/java/com/hubspot/jinjava/lib/fn/eager/EagerMacroFunction.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public EagerMacroFunction(
5858
);
5959
}
6060

61+
EagerMacroFunction(MacroFunction source, String name) {
62+
super(source, name);
63+
}
64+
6165
public Object doEvaluate(
6266
Map<String, Object> argMap,
6367
Map<String, Object> kwargMap,
@@ -334,4 +338,9 @@ public boolean equals(Object o) {
334338
public int hashCode() {
335339
return super.hashCode();
336340
}
341+
342+
@Override
343+
public EagerMacroFunction cloneWithNewName(String name) {
344+
return new EagerMacroFunction(this, name);
345+
}
337346
}

src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public static boolean integrateChild(
170170
if (val != null) {
171171
MacroFunction toImport = (MacroFunction) val;
172172
if (!importMapping.getKey().equals(importMapping.getValue())) {
173-
toImport = new MacroFunction(toImport, importMapping.getValue());
173+
toImport = toImport.cloneWithNewName(importMapping.getValue());
174174
}
175175
interpreter.getContext().addGlobalMacro(toImport);
176176
} else {

src/test/java/com/hubspot/jinjava/EagerTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,4 +1634,9 @@ public void prepareContext(Context context) {
16341634
"keeps-meta-context-variables-through-import/test"
16351635
);
16361636
}
1637+
1638+
@Test
1639+
public void itReconstructsFromedMacro() {
1640+
expectedTemplateInterpreter.assertExpectedOutput("reconstructs-fromed-macro/test");
1641+
}
16371642
}

src/test/resources/eager/handles-deferred-modification-in-caller/test.expected.jinja

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{% set my_list = ['a', 'b'] %}\
22
{% set __macro_callerino_729568755_temp_variable_0__ %}\
3+
{% set __macro_caller_172086791_temp_variable_0__ %}\
34
{% do my_list.append(deferred) %}\
45
{{ my_list }}\
6+
{% endset %}\
7+
{{ __macro_caller_172086791_temp_variable_0__ }}\
58
{% do my_list.append('d') %}\
69
{% endset %}\
710
{% call __macro_callerino_729568755_temp_variable_0__ %}\
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% macro upper(param) %}
2+
{{ param|upper }}
3+
{% endmacro %}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{% set deferred_import_resource_path = 'eager/reconstructs-fromed-macro/has-macro.jinja' %}\
2+
{% macro to_upper(param) %}
3+
{{ filter:upper.filter(param, ____int3rpr3t3r____) }}
4+
{% endmacro %}\
5+
{% set deferred_import_resource_path = null %}\
6+
{{ to_upper(deferred) }}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% from './has-macro.jinja' import upper as to_upper %}
2+
3+
{{ to_upper(deferred) }}

0 commit comments

Comments
 (0)