Skip to content

Commit

Permalink
Fix bugs in how macro functions reconstruct the current path
Browse files Browse the repository at this point in the history
  • Loading branch information
jasmith-hs committed Sep 6, 2024
1 parent 789cfe2 commit 74d9a4d
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hubspot.jinjava.lib.fn.eager;

import static com.hubspot.jinjava.loader.RelativePathResolver.CURRENT_PATH_CONTEXT_KEY;

import com.google.common.annotations.Beta;
import com.hubspot.jinjava.el.ext.AstMacroFunction;
import com.hubspot.jinjava.el.ext.DeferredInvocationResolutionException;
Expand All @@ -13,6 +15,7 @@
import com.hubspot.jinjava.lib.fn.MacroFunction;
import com.hubspot.jinjava.lib.tag.MacroTag;
import com.hubspot.jinjava.lib.tag.eager.EagerExecutionResult;
import com.hubspot.jinjava.lib.tag.eager.importing.EagerImportingStrategyFactory;
import com.hubspot.jinjava.objects.serialization.PyishObjectMapper;
import com.hubspot.jinjava.tree.Node;
import com.hubspot.jinjava.util.EagerContextWatcher;
Expand Down Expand Up @@ -80,6 +83,11 @@ public Object doEvaluate(
() -> getEvaluationResultDirectly(argMap, kwargMap, varArgs, interpreter),
interpreter
);
return wrapCurrentPathSetting(
interpreter,
importFile,
result.asTemplateString()
);
}
return result.asTemplateString();
} finally {
Expand Down Expand Up @@ -121,12 +129,45 @@ public Object doEvaluate(
.put(
tempVarName,
new MacroFunctionTempVariable(
prefixToPreserveState + eagerExecutionResult.asTemplateString()
wrapCurrentPathSetting(
interpreter,
Optional
.ofNullable(localContextScope.get(Context.IMPORT_RESOURCE_PATH_KEY))
.map(Object::toString),
prefixToPreserveState + eagerExecutionResult.asTemplateString()
)
)
);
throw new DeferredInvocationResolutionException(tempVarName);
}
return eagerExecutionResult.getResult().toString(true);
return wrapCurrentPathSetting(
interpreter,
Optional
.ofNullable(localContextScope.get(Context.IMPORT_RESOURCE_PATH_KEY))
.map(Object::toString),
eagerExecutionResult.getResult().toString(true)
);
}

private static String wrapCurrentPathSetting(
JinjavaInterpreter interpreter,
Optional<String> importFile,
String resultToWrap
) {
if (!importFile.isPresent()) {
return resultToWrap;
}
final String initialPathSetter =
EagerImportingStrategyFactory.getSetTagForCurrentPath(interpreter);
final String newPathSetter = EagerReconstructionUtils.buildBlockOrInlineSetTag(
CURRENT_PATH_CONTEXT_KEY,
importFile.get(),
interpreter
);
if (initialPathSetter.equals(newPathSetter)) {
return resultToWrap;
}
return newPathSetter + resultToWrap + initialPathSetter;
}

private String getEvaluationResultDirectly(
Expand Down Expand Up @@ -312,6 +353,7 @@ private boolean differentMacroWithSameNameExists(JinjavaInterpreter interpreter)
return false;
}
MacroFunction mostRecent = context.getGlobalMacro(getName());
//noinspection ErrorProne
if (this != mostRecent) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
child.getContext().put(Context.IMPORT_RESOURCE_PATH_KEY, templateFile);
JinjavaInterpreter.pushCurrent(child);
try {
child.render(node);
child.render(node, false);
} finally {
JinjavaInterpreter.popCurrent();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/hubspot/jinjava/lib/tag/ImportTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
JinjavaInterpreter.pushCurrent(child);

try {
child.render(node);
child.render(node, false);
} finally {
JinjavaInterpreter.popCurrent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public String getEagerTagImage(TagToken tagToken, JinjavaInterpreter interpreter
JinjavaInterpreter.pushCurrent(child);
String output;
try {
output = child.render(node);
output = child.render(node, false);
} finally {
JinjavaInterpreter.popCurrent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public String getEagerTagImage(TagToken tagToken, JinjavaInterpreter interpreter
String output;
try {
eagerImportingStrategy.setup(child);
output = child.render(node);
output = child.render(node, false);
} finally {
JinjavaInterpreter.popCurrent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public static String getSetTagForCurrentPath(JinjavaInterpreter interpreter) {
.getContext()
.getCurrentPathStack()
.peek()
.map(c -> {
interpreter
.getContext()
.replace(RelativePathResolver.CURRENT_PATH_CONTEXT_KEY, c);
return c;
})
.orElseGet(() ->
(String) interpreter
.getContext()
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/hubspot/jinjava/EagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1597,4 +1597,11 @@ public void prepareContext(Context context) {
"keeps-meta-context-variables-through-import/test"
);
}

@Test
public void itWrapsCurrentPathAroundMacro() {
expectedTemplateInterpreter.assertExpectedOutputNonIdempotent(
"wraps-current-path-around-macro/test"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,12 @@ public String getFixtureTemplate(String name) {

private String expected(String name) {
try {
return Resources.toString(
Resources.getResource(String.format("%s/%s.expected.jinja", path, name)),
StandardCharsets.UTF_8
);
return Resources
.toString(
Resources.getResource(String.format("%s/%s.expected.jinja", path, name)),
StandardCharsets.UTF_8
)
.replaceAll("\\\\\n\\s*", "");
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% macro foo_importer() -%}
{%- import "../../../eager/wraps-current-path-around-macro/dir2/imported.jinja" -%}
{%- print foo -%}
{%- endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% set foo = deferred %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% set __macro_foo_importer_1564912668_temp_variable_0__ %}\
{% set current_path = '../../eager/wraps-current-path-around-macro/dir1/macro.jinja' %}\
{% do %}\
{% set current_path = '../../eager/wraps-current-path-around-macro/dir2/imported.jinja' %}\
{% set foo = deferred %}\
{% set current_path = '../../eager/wraps-current-path-around-macro/dir1/macro.jinja' %}\
{% enddo %}\
{% print foo %}\
{% set current_path = '' %}\
{% endset %}\
{% print __macro_foo_importer_1564912668_temp_variable_0__ %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% from "../../eager/wraps-current-path-around-macro/dir1/macro.jinja" import foo_importer -%}
{% print foo_importer() %}

0 comments on commit 74d9a4d

Please sign in to comment.