Skip to content

Commit 4820ad3

Browse files
authored
Merge pull request #1150 from abarrafo/ab-debug-undefined
Debug Undefined Feature to echo back Uknown tokens.
2 parents 87e0336 + ba25549 commit 4820ad3

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/main/java/com/hubspot/jinjava/lib/expression/DefaultExpressionStrategy.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.hubspot.jinjava.lib.expression;
22

3+
import com.hubspot.jinjava.features.FeatureActivationStrategy;
34
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
45
import com.hubspot.jinjava.lib.filter.EscapeFilter;
56
import com.hubspot.jinjava.objects.SafeString;
@@ -11,6 +12,7 @@
1112
public class DefaultExpressionStrategy implements ExpressionStrategy {
1213

1314
private static final long serialVersionUID = 436239440273704843L;
15+
public static final String ECHO_UNDEFINED = "echoUndefined";
1416

1517
public RenderedOutputNode interpretOutput(
1618
ExpressionToken master,
@@ -20,6 +22,16 @@ public RenderedOutputNode interpretOutput(
2022
master.getExpr(),
2123
master.getLineNumber()
2224
);
25+
26+
final FeatureActivationStrategy feat = interpreter
27+
.getConfig()
28+
.getFeatures()
29+
.getActivationStrategy(ECHO_UNDEFINED);
30+
31+
if (var == null && feat.isActive(interpreter.getContext())) {
32+
return new RenderedOutputNode(master.getImage());
33+
}
34+
2335
String result = interpreter.getAsString(var);
2436

2537
if (interpreter.getConfig().isNestedInterpretationEnabled()) {

src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.hubspot.jinjava.tree;
22

3+
import static com.hubspot.jinjava.lib.expression.DefaultExpressionStrategy.ECHO_UNDEFINED;
34
import static org.assertj.core.api.Assertions.assertThat;
45
import static org.assertj.core.api.Assertions.assertThatThrownBy;
56

67
import com.google.common.io.Resources;
78
import com.hubspot.jinjava.BaseInterpretingTest;
89
import com.hubspot.jinjava.Jinjava;
910
import com.hubspot.jinjava.JinjavaConfig;
11+
import com.hubspot.jinjava.features.FeatureConfig;
12+
import com.hubspot.jinjava.features.FeatureStrategies;
1013
import com.hubspot.jinjava.interpret.Context;
1114
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
1215
import com.hubspot.jinjava.interpret.UnknownTokenException;
@@ -129,6 +132,29 @@ public void itRendersStringRange() throws Exception {
129132
assertThat(node.render(interpreter).toString()).isEqualTo("345");
130133
}
131134

135+
@Test
136+
public void itRenderEchoUndefined() {
137+
final JinjavaConfig config = JinjavaConfig
138+
.newBuilder()
139+
.withFeatureConfig(
140+
FeatureConfig.newBuilder().add(ECHO_UNDEFINED, FeatureStrategies.ACTIVE).build()
141+
)
142+
.build();
143+
final JinjavaInterpreter jinjavaInterpreter = new Jinjava(config).newInterpreter();
144+
jinjavaInterpreter.getContext().put("subject", "this");
145+
146+
String template =
147+
"{{ subject | capitalize() }} expression {{ testing.template('hello_world') }} " +
148+
"has a {{ unknown | lower() }} " +
149+
"token but {{ unknown | default(\"replaced\") }} and empty {{ '' }}";
150+
Node node = new TreeParser(jinjavaInterpreter, template).buildTree();
151+
assertThat(jinjavaInterpreter.render(node))
152+
.isEqualTo(
153+
"This expression {{ testing.template('hello_world') }} " +
154+
"has a {{ unknown | lower() }} token but replaced and empty "
155+
);
156+
}
157+
132158
@Test
133159
public void itFailsOnUnknownTokensVariables() throws Exception {
134160
final JinjavaConfig config = JinjavaConfig

0 commit comments

Comments
 (0)