Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,13 @@ public String reconstructImage(String fullName) {
String suffix = "";
JinjavaInterpreter interpreter = JinjavaInterpreter.getCurrent();

Optional<String> 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<String> importFile = importPath instanceof String
? Optional.of((String) importPath)
: Optional.empty();
Object currentDeferredImportResource = null;
if (importFile.isPresent()) {
currentDeferredImportResource =
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/hubspot/jinjava/lib/tag/ImportTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,29 @@ public void itPreservesOriginalValueOfDeferredImportedBindings() {
.isNull();
}

@Test
public void itDoesNotDeferMetaContextVariablesDuringDeferredImport() {
JinjavaInterpreter child = getChildInterpreter(interpreter, "");
Map<String, Object> 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() {
Expand Down
Loading