Skip to content
Draft
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
2 changes: 1 addition & 1 deletion src/main/java/com/hubspot/jinjava/JinjavaConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ default ExecutionMode getExecutionMode() {

@Value.Default
default LegacyOverrides getLegacyOverrides() {
return LegacyOverrides.NONE;
return LegacyOverrides.THREE_POINT_0;
}

@Value.Default
Expand Down
44 changes: 29 additions & 15 deletions src/test/java/com/hubspot/jinjava/el/ExtendedSyntaxBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.common.io.Resources;
import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.JinjavaConfig;
import com.hubspot.jinjava.LegacyOverrides;
import com.hubspot.jinjava.interpret.Context;
import com.hubspot.jinjava.interpret.IndexOutOfRangeException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
Expand Down Expand Up @@ -188,22 +189,35 @@ public void literalTuple() {

@Test
public void mapLiteral() {
context.put("foo", "bar");
assertThat((Map<String, Object>) val("{}")).isEmpty();
Map<String, Object> map = (Map<String, Object>) val(
"{foo: foo, \"foo2\": foo, foo3: 123, foo4: 'string', foo5: {}, foo6: [1, 2]}"
);
assertThat(map)
.contains(
entry("foo", "bar"),
entry("foo2", "bar"),
entry("foo3", 123L),
entry("foo4", "string"),
entry("foo6", Arrays.asList(1L, 2L))
interpreter =
new Jinjava(
JinjavaConfig
.builder()
.withMaxOutputSize(MAX_STRING_LENGTH)
.withLegacyOverrides(LegacyOverrides.THREE_POINT_0.withEvaluateMapKeys(false))
.build()
)
.newInterpreter();
try (var a = JinjavaInterpreter.closeablePushCurrent(interpreter).get()) {
interpreter.getContext().put("foo", "bar");
assertThat((Map<String, Object>) val("{}")).isEmpty();
Map<String, Object> map = (Map<String, Object>) val(
"{foo: foo, \"foo2\": foo, foo3: 123, foo4: 'string', foo5: {}, foo6: [1, 2]}"
);

assertThat((Map<String, Object>) val("{\"address\":\"123 Main - Boston, MA 02111\"}"))
.contains(entry("address", "123 Main - Boston, MA 02111"));
assertThat(map)
.contains(
entry("foo", "bar"),
entry("foo2", "bar"),
entry("foo3", 123L),
entry("foo4", "string"),
entry("foo6", Arrays.asList(1L, 2L))
);

assertThat(
(Map<String, Object>) val("{\"address\":\"123 Main - Boston, MA 02111\"}")
)
.contains(entry("address", "123 Main - Boston, MA 02111"));
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.common.collect.Lists;
import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.JinjavaConfig;
import com.hubspot.jinjava.LegacyOverrides;
import com.hubspot.jinjava.features.FeatureConfig;
import com.hubspot.jinjava.features.FeatureStrategies;
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
Expand Down Expand Up @@ -363,6 +364,17 @@ public void itInterpretsWhitespaceControl() {

@Test
public void itInterpretsEmptyExpressions() {
jinjava =
new Jinjava(
JinjavaConfig
.builder()
.withTimeZone(ZoneId.of("America/New_York"))
.withLegacyOverrides(
LegacyOverrides.THREE_POINT_0.withParseWhitespaceControlStrictly(false)
)
.build()
);
interpreter = jinjava.newInterpreter();
assertThat(interpreter.render("{{}}")).isEqualTo("");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public void ppObject() {
"{%% raw %%}(MyClass: {\n" +
" &quot;foo&quot; : &quot;%s&quot;,\n" +
" &quot;bar&quot; : %d,\n" +
" &quot;nestedClass&quot; : {\n" +
" &quot;fooField&quot; : &quot;%s&quot;,\n" +
" &quot;barField&quot; : %d\n" +
" &quot;nested_class&quot; : {\n" +
" &quot;foo_field&quot; : &quot;%s&quot;,\n" +
" &quot;bar_field&quot; : %d\n" +
" }\n" +
"}){%% endraw %%}",
myClass.getFoo(),
Expand Down
47 changes: 23 additions & 24 deletions src/test/java/com/hubspot/jinjava/lib/tag/ValidationModeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.common.collect.ImmutableList;
import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.JinjavaConfig;
import com.hubspot.jinjava.LegacyOverrides;
import com.hubspot.jinjava.interpret.Context;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.lib.filter.Filter;
Expand All @@ -17,7 +18,6 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -81,15 +81,12 @@ public void setup() {
new JinjavaInterpreter(
jinjava,
context,
JinjavaConfig.newBuilder().withValidationMode(true).build()
JinjavaConfig
.newBuilder()
.withLegacyOverrides(LegacyOverrides.NONE)
.withValidationMode(true)
.build()
);

JinjavaInterpreter.pushCurrent(interpreter);
}

@After
public void tearDown() {
JinjavaInterpreter.popCurrent();
}

@Test
Expand Down Expand Up @@ -278,17 +275,19 @@ public void itDoesNotExecuteMacrosInValidatedBlocks() {
interpreter.getContext()
);
interpreter.getContext().addGlobalMacro(macro);

String template =
"{{ hello() }}" + "{% if false %} " + " {{ hello() }}" + "{% endif %}";

assertThat(interpreter.getErrors()).isEmpty();
assertThat(interpreter.render(template).trim()).isEqualTo("hello");
assertThat(macro.getInvocationCount()).isEqualTo(1);

assertThat(validatingInterpreter.render(template).trim()).isEqualTo("hello");
assertThat(macro.getInvocationCount()).isEqualTo(3);
assertThat(validatingInterpreter.getErrors()).isEmpty();
try (var a = JinjavaInterpreter.closeablePushCurrent(interpreter).get()) {
assertThat(interpreter.getErrors()).isEmpty();
assertThat(interpreter.render(template).trim()).isEqualTo("hello");
assertThat(macro.getInvocationCount()).isEqualTo(1);
}
try (var a = JinjavaInterpreter.closeablePushCurrent(validatingInterpreter).get()) {
assertThat(validatingInterpreter.render(template).trim()).isEqualTo("hello");
assertThat(macro.getInvocationCount()).isEqualTo(3);
assertThat(validatingInterpreter.getErrors()).isEmpty();
}
}

@Test
Expand Down Expand Up @@ -333,13 +332,13 @@ public void itDoesNotExecuteFiltersInValidatedBlocks() {
assertThat(result).isEqualTo("10");
assertThat(validationFilter.getExecutionCount()).isEqualTo(1);

JinjavaInterpreter.pushCurrent(validatingInterpreter);
result = validatingInterpreter.render(template).trim();
try (var a = JinjavaInterpreter.closeablePushCurrent(validatingInterpreter).get()) {
result = validatingInterpreter.render(template).trim();

assertThat(validatingInterpreter.getErrors().size()).isEqualTo(1);
assertThat(validatingInterpreter.getErrors().get(0).getMessage()).contains("hey(");
assertThat(result).isEqualTo("10");
assertThat(validationFilter.getExecutionCount()).isEqualTo(2);
JinjavaInterpreter.popCurrent();
assertThat(validatingInterpreter.getErrors().size()).isEqualTo(1);
assertThat(validatingInterpreter.getErrors().get(0).getMessage()).contains("hey(");
assertThat(result).isEqualTo("10");
assertThat(validationFilter.getExecutionCount()).isEqualTo(2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.hubspot.jinjava.BaseInterpretingTest;
import com.hubspot.jinjava.JinjavaConfig;
Expand Down Expand Up @@ -136,7 +138,7 @@ public void itPutsOnContextInChildContext() {
public void itModifiesContextInChildContext() {
context.put("foo", new ArrayList<>());
assertThat(interpreter.render("{{ modify_context('foo', 'bar') }}{{ foo }}"))
.isEqualTo("[bar]");
.isEqualTo("['bar']");
}

@Test
Expand All @@ -145,7 +147,7 @@ public void itDoesntModifyContextWhenResultIsDeferred() {
assertThat(
interpreter.render("{{ modify_context('foo', 'bar') ~ deferred }}{{ foo }}")
)
.isEqualTo("{{ null ~ deferred }}[bar]");
.isEqualTo("{{ null ~ deferred }}['bar']");
}

public static void addToContext(String key, Object value) {
Expand Down