From e2b4c07a2d15426d683a1df42ab5cd5e24294dcb Mon Sep 17 00:00:00 2001 From: Manhey Chiu Date: Fri, 19 Apr 2024 04:24:07 +0100 Subject: [PATCH 1/3] Add undefined variable warning when config enabled --- .../jinjava/interpret/JinjavaInterpreter.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java index 8e0510aca..5ff87fa7c 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java +++ b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java @@ -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 blocks = ArrayListMultimap.create(); private final LinkedList extendParentRoots = new LinkedList<>(); private final Map revertibleObjects = new HashMap<>(); @@ -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(getContext()) + ) { + addError( + new TemplateError( + ErrorType.WARNING, + ErrorReason.UNKNOWN, + ErrorItem.TOKEN, + "Undefined variable: '" + variable + "'", + null, + lineNumber, + startPosition, + null, + BasicTemplateErrorCategory.UNKNOWN, + ImmutableMap.of("variable", variable) + ) + ); + } } return obj; } From 468009609f2bcaca0b3ea7e058ccf92dff189c49 Mon Sep 17 00:00:00 2001 From: Manhey Chiu Date: Fri, 19 Apr 2024 04:47:31 +0100 Subject: [PATCH 2/3] Add unit test --- .../interpret/JinjavaInterpreterTest.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java b/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java index e8e9641a1..f866db349 100644 --- a/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java +++ b/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java @@ -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")); + } } From a9f53fd785c5df6cd9e8e33d9cc3f3484b0e86b8 Mon Sep 17 00:00:00 2001 From: manheychiu <119883507+manheychiu@users.noreply.github.com> Date: Fri, 19 Apr 2024 20:03:44 +0100 Subject: [PATCH 3/3] Update src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java Co-authored-by: Jack Smith <72623970+jasmith-hs@users.noreply.github.com> --- .../java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java index 5ff87fa7c..a85b756b1 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java +++ b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java @@ -593,7 +593,7 @@ public Object retraceVariable(String variable, int lineNumber, int startPosition getConfig() .getFeatures() .getActivationStrategy(OUTPUT_UNDEFINED_VARIABLES_ERROR) - .isActive(getContext()) + .isActive(context) ) { addError( new TemplateError(