Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a feature to output template error when the variable is undefined #1174

Merged
merged 3 commits into from
Apr 19, 2024
Merged
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 @@ -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"));
}
}
Loading