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 18a649794..d761696d0 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 @@ -237,9 +237,13 @@ public String reconstructImage(String fullName) { String suffix = ""; JinjavaInterpreter interpreter = JinjavaInterpreter.getCurrent(); - Optional importFile = Optional.ofNullable( - (String) localContextScope.get(Context.IMPORT_RESOURCE_PATH_KEY) - ); + Object importPath = localContextScope.get(Context.IMPORT_RESOURCE_PATH_KEY); + if (importPath instanceof DeferredValue) { + importPath = ((DeferredValue) importPath).getOriginalValue(); + } + Optional importFile = importPath instanceof String + ? Optional.of((String) importPath) + : Optional.empty(); Object currentDeferredImportResource = null; if (importFile.isPresent()) { currentDeferredImportResource = diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/ImportTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/ImportTag.java index 4ce3c9d27..49832f2c5 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/ImportTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/ImportTag.java @@ -13,6 +13,7 @@ import com.hubspot.jinjava.interpret.DeferredValueException; import com.hubspot.jinjava.interpret.InterpretException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.MetaContextVariables; import com.hubspot.jinjava.interpret.TagCycleException; import com.hubspot.jinjava.interpret.TemplateError; import com.hubspot.jinjava.interpret.TemplateError.ErrorItem; @@ -212,6 +213,9 @@ public static void handleDeferredNodesDuringImport( } childBindings.remove(Context.GLOBAL_MACROS_SCOPE_KEY); childBindings.forEach((key, value) -> { + if (MetaContextVariables.isMetaContextVariable(key, interpreter.getContext())) { + return; + } Object originalValue = value instanceof DeferredValue ? ((DeferredValue) value).getOriginalValue() : null; diff --git a/src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java b/src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java index 6e082e9e8..42fdab34f 100644 --- a/src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java @@ -247,6 +247,29 @@ public void itPreservesOriginalValueOfDeferredImportedBindings() { .isNull(); } + @Test + public void itDoesNotDeferMetaContextVariablesDuringDeferredImport() { + JinjavaInterpreter child = getChildInterpreter(interpreter, ""); + Map childBindings = new HashMap<>(); + childBindings.put(Context.IMPORT_RESOURCE_PATH_KEY, TEMPLATE_FILE); + childBindings.put("request", DeferredValue.instance("the real request")); + + ImportTag.handleDeferredNodesDuringImport( + interpreter.parse(""), + "", + childBindings, + child, + interpreter + ); + + assertThat(interpreter.getContext().get(Context.IMPORT_RESOURCE_PATH_KEY)).isNull(); + assertThat(interpreter.getContext().get("request")).isInstanceOf(DeferredValue.class); + assertThat( + ((DeferredValue) interpreter.getContext().get("request")).getOriginalValue() + ) + .isEqualTo("the real request"); + } + @Test @SuppressWarnings("unchecked") public void itHandlesMultiLayerSomeAliased() {