Skip to content

Commit

Permalink
Add a feature to output template error when the variable is undefined (
Browse files Browse the repository at this point in the history
…#1174)

This PR adds a feature `OUTPUT_UNDEFINED_VARIABLES_ERROR`. When enabled,
a template error (severity=WARNING) will be added when trying to resolve
an undefined variable.

---------

Co-authored-by: Manhey Chiu <[email protected]>
Co-authored-by: Jack Smith <[email protected]>
  • Loading branch information
3 people authored Apr 19, 2024
1 parent 61452e4 commit b910db5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public class JinjavaInterpreter implements PyishSerializable {

public static final String IGNORED_OUTPUT_FROM_EXTENDS_NOTE =
"ignored_output_from_extends";

public static final String OUTPUT_UNDEFINED_VARIABLES_ERROR =
"OUTPUT_UNDEFINED_VARIABLES_ERROR";
private final Multimap<String, BlockInfo> blocks = ArrayListMultimap.create();
private final LinkedList<Node> extendParentRoots = new LinkedList<>();
private final Map<String, RevertibleObject> revertibleObjects = new HashMap<>();
Expand Down Expand Up @@ -585,6 +588,28 @@ public Object retraceVariable(String variable, int lineNumber, int startPosition
}
}
obj = var.resolve(obj);
} else {
if (
getConfig()
.getFeatures()
.getActivationStrategy(OUTPUT_UNDEFINED_VARIABLES_ERROR)
.isActive(context)
) {
addError(
new TemplateError(
ErrorType.WARNING,
ErrorReason.UNKNOWN,
ErrorItem.TOKEN,
"Undefined variable: '" + variable + "'",
null,
lineNumber,
startPosition,
null,
BasicTemplateErrorCategory.UNKNOWN,
ImmutableMap.of("variable", variable)
)
);
}
}
return obj;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,4 +557,47 @@ public void itPreventsAccidentalExpressions() {
JinjavaInterpreter.popCurrent();
}
}

@Test
public void itOutputsUndefinedVariableError() {
String template = "{% set foo=123 %}{{ foo }}{{ bar }}";

JinjavaInterpreter normalInterpreter = new JinjavaInterpreter(
jinjava,
jinjava.getGlobalContext(),
JinjavaConfig.newBuilder().withExecutionMode(EagerExecutionMode.instance()).build()
);
JinjavaInterpreter outputtingErrorInterpreters = new JinjavaInterpreter(
jinjava,
jinjava.getGlobalContext(),
JinjavaConfig
.newBuilder()
.withFeatureConfig(
FeatureConfig
.newBuilder()
.add(
JinjavaInterpreter.OUTPUT_UNDEFINED_VARIABLES_ERROR,
FeatureStrategies.ACTIVE
)
.build()
)
.withExecutionMode(EagerExecutionMode.instance())
.build()
);

String normalRenderResult = normalInterpreter.render(template);
String outputtingErrorRenderResult = outputtingErrorInterpreters.render(template);
assertThat(normalRenderResult).isEqualTo("123");
assertThat(outputtingErrorRenderResult).isEqualTo("123");
assertThat(normalInterpreter.getErrors()).isEmpty();
assertThat(outputtingErrorInterpreters.getErrors().size()).isEqualTo(1);
assertThat(outputtingErrorInterpreters.getErrors().get(0).getMessage())
.contains("Undefined variable: 'bar'");
assertThat(outputtingErrorInterpreters.getErrors().get(0).getReason())
.isEqualTo(ErrorReason.UNKNOWN);
assertThat(outputtingErrorInterpreters.getErrors().get(0).getSeverity())
.isEqualTo(ErrorType.WARNING);
assertThat(outputtingErrorInterpreters.getErrors().get(0).getCategoryErrors())
.isEqualTo(ImmutableMap.of("variable", "bar"));
}
}

0 comments on commit b910db5

Please sign in to comment.