Skip to content

Commit cf70af7

Browse files
authored
Merge pull request #822 from HubSpot/revert-821-revert-820-handle-nested-import-file-paths
Revert 821 revert 820 handle nested import file paths
2 parents 9b06e3b + 13e9254 commit cf70af7

11 files changed

Lines changed: 116 additions & 19 deletions

File tree

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,20 @@ public boolean equals(Object o) {
182182
MacroFunction that = (MacroFunction) o;
183183
return (
184184
caller == that.caller &&
185-
definitionLineNumber == that.definitionLineNumber &&
186-
definitionStartPosition == that.definitionStartPosition &&
187-
content.equals(that.content)
185+
Objects.equals(getName(), that.getName()) &&
186+
Objects.equals(
187+
localContextScope.get(Context.IMPORT_RESOURCE_PATH_KEY),
188+
that.localContextScope.get(Context.IMPORT_RESOURCE_PATH_KEY)
189+
)
188190
);
189191
}
190192

191193
@Override
192194
public int hashCode() {
193-
return Objects.hash(content, caller, definitionLineNumber, definitionStartPosition);
195+
return Objects.hash(
196+
getName(),
197+
localContextScope.get(Context.IMPORT_RESOURCE_PATH_KEY),
198+
caller
199+
);
194200
}
195201
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.hubspot.jinjava.el.ext.AstMacroFunction;
66
import com.hubspot.jinjava.interpret.Context;
77
import com.hubspot.jinjava.interpret.DeferredMacroValueImpl;
8+
import com.hubspot.jinjava.interpret.DeferredValue;
89
import com.hubspot.jinjava.interpret.DeferredValueException;
910
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
1011
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
@@ -106,11 +107,15 @@ public String reconstructImage() {
106107
String prefix = "";
107108
String suffix = "";
108109
Optional<String> importFile = macroFunction.getImportFile(interpreter);
110+
Object currentDeferredImportResource = null;
109111
if (importFile.isPresent()) {
110112
interpreter.getContext().getCurrentPathStack().pop();
111-
String currentDeferredImportResource = (String) interpreter
112-
.getContext()
113-
.get(Context.DEFERRED_IMPORT_RESOURCE_PATH_KEY);
113+
currentDeferredImportResource =
114+
interpreter.getContext().get(Context.DEFERRED_IMPORT_RESOURCE_PATH_KEY);
115+
if (currentDeferredImportResource instanceof DeferredValue) {
116+
currentDeferredImportResource =
117+
((DeferredValue) currentDeferredImportResource).getOriginalValue();
118+
}
114119
prefix =
115120
EagerReconstructionUtils.buildSetTag(
116121
ImmutableMap.of(
@@ -120,6 +125,9 @@ public String reconstructImage() {
120125
interpreter,
121126
false
122127
);
128+
interpreter
129+
.getContext()
130+
.put(Context.DEFERRED_IMPORT_RESOURCE_PATH_KEY, importFile.get());
123131
suffix =
124132
EagerReconstructionUtils.buildSetTag(
125133
ImmutableMap.of(
@@ -150,6 +158,10 @@ public String reconstructImage() {
150158
.map(arg -> DeferredMacroValueImpl.instance())
151159
.toArray()
152160
);
161+
162+
interpreter
163+
.getContext()
164+
.put(Context.DEFERRED_IMPORT_RESOURCE_PATH_KEY, currentDeferredImportResource);
153165
if (interpreter.getContext().getEagerTokens().size() > numEagerTokensStart) {
154166
evaluation =
155167
(String) evaluate(

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.hubspot.jinjava.tree.parse.TagToken;
2424
import com.hubspot.jinjava.util.HelperStringTokenizer;
2525
import java.io.IOException;
26+
import java.util.HashMap;
2627
import java.util.List;
2728
import java.util.Map;
2829
import java.util.Optional;
@@ -151,19 +152,29 @@ public static void integrateChild(
151152
parent.getContext().addGlobalMacro(macro);
152153
}
153154
childBindings.remove(Context.GLOBAL_MACROS_SCOPE_KEY);
154-
parent.getContext().putAll(childBindings);
155-
} else {
156-
for (Map.Entry<String, MacroFunction> macro : child
155+
parent
157156
.getContext()
158-
.getGlobalMacros()
159-
.entrySet()) {
160-
childBindings.put(macro.getKey(), macro.getValue());
161-
}
157+
.putAll(getChildBindingsWithoutImportResourcePath(childBindings));
158+
} else {
159+
childBindings.putAll(child.getContext().getGlobalMacros());
162160
childBindings.remove(Context.GLOBAL_MACROS_SCOPE_KEY);
163161
parent.getContext().put(contextVar, childBindings);
164162
}
165163
}
166164

165+
public static Map<String, Object> getChildBindingsWithoutImportResourcePath(
166+
Map<String, Object> childBindings
167+
) {
168+
Map<String, Object> filteredMap = new HashMap<>();
169+
// Don't remove them from childBindings, because it is needed in a macro function's localContextScope
170+
childBindings
171+
.entrySet()
172+
.stream()
173+
.filter(entry -> !entry.getKey().equals(Context.IMPORT_RESOURCE_PATH_KEY))
174+
.forEach(entry -> filteredMap.put(entry.getKey(), entry.getValue()));
175+
return filteredMap;
176+
}
177+
167178
public static void handleDeferredNodesDuringImport(
168179
Node node,
169180
String contextVar,

src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTag.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,11 @@ public static void integrateChild(
338338
}
339339
childBindings.remove(Context.GLOBAL_MACROS_SCOPE_KEY);
340340
childBindings.remove(Context.IMPORT_RESOURCE_ALIAS_KEY);
341-
parent.getContext().putAll(childBindings);
341+
parent
342+
.getContext()
343+
.putAll(ImportTag.getChildBindingsWithoutImportResourcePath(childBindings));
342344
} else {
343-
Map<String, MacroFunction> globalMacros = child.getContext().getGlobalMacros();
344-
for (Map.Entry<String, MacroFunction> macro : globalMacros.entrySet()) {
345-
childBindings.put(macro.getKey(), macro.getValue());
346-
}
345+
childBindings.putAll(child.getContext().getGlobalMacros());
347346
Map<String, Object> mapForCurrentContextAlias = getMapForCurrentContextAlias(
348347
currentImportAlias,
349348
child

src/test/java/com/hubspot/jinjava/lib/tag/ImportTagTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
1212
import com.hubspot.jinjava.interpret.RenderResult;
1313
import com.hubspot.jinjava.lib.fn.MacroFunction;
14+
import com.hubspot.jinjava.lib.tag.eager.EagerImportTagTest.PrintPathFilter;
1415
import com.hubspot.jinjava.loader.LocationResolver;
1516
import com.hubspot.jinjava.loader.RelativePathResolver;
1617
import com.hubspot.jinjava.loader.ResourceLocator;
@@ -39,6 +40,7 @@ public void setup() {
3940
);
4041

4142
context.put("padding", 42);
43+
context.registerFilter(new PrintPathFilter());
4244
}
4345

4446
@Test
@@ -76,6 +78,21 @@ public void itAvoidsNestedImportCycle() throws IOException {
7678
.contains("Import cycle detected", "b-imports-a.jinja");
7779
}
7880

81+
@Test
82+
public void itHandlesNullImportedValues() throws IOException {
83+
Jinjava jinjava = new Jinjava();
84+
interpreter = new JinjavaInterpreter(jinjava, context, jinjava.getGlobalConfig());
85+
86+
interpreter.render(
87+
Resources.toString(
88+
Resources.getResource("tags/importtag/imports-null.jinja"),
89+
StandardCharsets.UTF_8
90+
)
91+
);
92+
assertThat(context.get("foo")).isEqualTo("foo");
93+
assertThat(context.get("bar")).isEqualTo(null);
94+
}
95+
7996
@Test
8097
public void importedContextExposesVars() {
8198
assertThat(fixture("import"))
@@ -352,6 +369,17 @@ public void itSetsErrorLineNumbersCorrectlyForImportedMacros() throws IOExceptio
352369
.isEqualTo("tags/importtag/errors/macro-with-error.jinja");
353370
}
354371

372+
@Test
373+
public void itCorrectlySetsNestedPaths() {
374+
context.put("foo", "foo");
375+
assertThat(
376+
interpreter.render(
377+
"{% import 'double-import-macro.jinja' %}{{ print_path_macro2(foo) }}"
378+
)
379+
)
380+
.isEqualTo("double-import-macro.jinja\n\nimport-macro.jinja\nfoo\n");
381+
}
382+
355383
private String fixture(String name) {
356384
try {
357385
return interpreter.renderFlat(

src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,27 @@ public void itCorrectlySetsPathForSecondPass() {
535535
.isEqualTo("import-macro.jinja\nfoo");
536536
}
537537

538+
@Test
539+
public void itCorrectlySetsNestedPathsForSecondPass() {
540+
setupResourceLocator();
541+
context.put("foo", DeferredValue.instance());
542+
String firstPassResult = interpreter.render(
543+
"{% import 'double-import-macro.jinja' %}{{ print_path_macro2(foo) }}"
544+
);
545+
assertThat(firstPassResult)
546+
.isEqualTo(
547+
"{% set deferred_import_resource_path = 'double-import-macro.jinja' %}{% macro print_path_macro2(var) %}{{ filter:print_path.filter(var, ____int3rpr3t3r____) }}\n" +
548+
"{% set deferred_import_resource_path = 'import-macro.jinja' %}{% macro print_path_macro(var) %}\n" +
549+
"{{ filter:print_path.filter(var, ____int3rpr3t3r____) }}\n" +
550+
"{{ var }}\n" +
551+
"{% endmacro %}{% set deferred_import_resource_path = null %}{{ print_path_macro(var) }}{% endmacro %}{% set deferred_import_resource_path = null %}{{ print_path_macro2(foo) }}"
552+
);
553+
context.put("foo", "foo");
554+
context.put(Context.GLOBAL_MACROS_SCOPE_KEY, null);
555+
assertThat(interpreter.render(firstPassResult).trim())
556+
.isEqualTo("double-import-macro.jinja\n\nimport-macro.jinja\nfoo");
557+
}
558+
538559
@Test
539560
public void itImportsDoublyNamed() {
540561
setupResourceLocator();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% import 'import-macro.jinja' -%}
2+
{% macro print_path_macro2(var) -%}
3+
{{ var|print_path }}
4+
{{ print_path_macro(var) }}
5+
{%- endmacro %}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{% set bar = 'bar' %}
2+
{% import 'tags/importtag/null-value.jinja' %}
3+
{{ foo }}
4+
{{ bar }}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{% set foo = 'foo' %}
2+
{% set bar = null %}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% import 'import-macro.jinja' -%}
2+
{% macro print_path_macro2(var) -%}
3+
{{ var|print_path }}
4+
{{ print_path_macro(var) }}
5+
{%- endmacro %}

0 commit comments

Comments
 (0)