Skip to content

Commit

Permalink
chore(jinja): Upgrade jinjava (#2717)
Browse files Browse the repository at this point in the history
The ModuleTag class breaks with jinjava >= 2.2.8 because of a fix in
failsOnUnknownTokens.  We need to catch this error in cases where we
expect that a token may be unknown. The test also uses True as a literal
which should be true (ie, lowercase) which worked before but breaks with
the upgrade.
  • Loading branch information
ezimanyi authored Mar 8, 2019
1 parent 967cb47 commit 35d57a5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
3 changes: 1 addition & 2 deletions orca-pipelinetemplate/orca-pipelinetemplate.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ dependencies {
compile project(":orca-front50")
compile project(":orca-clouddriver")

compile('com.hubspot.jinjava:jinjava:2.2.3')

compile "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${spinnaker.version('jackson')}"
compile "com.fasterxml.jackson.module:jackson-module-kotlin:${spinnaker.version("jackson")}"
compile('com.jayway.jsonpath:json-path:2.2.0')
Expand All @@ -17,6 +15,7 @@ dependencies {
compile spinnaker.dependency("bootAutoConfigure")
compile spinnaker.dependency("springContext")
compile spinnaker.dependency("jacksonDatabind")
compile spinnaker.dependency("jinjava")
compile spinnaker.dependency("spectatorApi")
compile spinnaker.dependency("frigga")
compile spinnaker.dependency('korkExceptions')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Splitter;
import com.hubspot.jinjava.interpret.Context;
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateStateException;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.interpret.*;
import com.hubspot.jinjava.lib.tag.Tag;
import com.hubspot.jinjava.tree.TagNode;
import com.hubspot.jinjava.util.HelperStringTokenizer;
Expand All @@ -37,11 +33,7 @@
import com.netflix.spinnaker.orca.pipelinetemplate.validator.Errors.Error;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -103,12 +95,12 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
List<String> missing = new ArrayList<>();
for (NamedHashMap var : module.getVariables()) {
// First try to assign the variable from the context directly
Object val = interpreter.resolveELExpression(var.getName(), tagNode.getLineNumber());
Object val = tryResolveExpression(interpreter, var.getName(), tagNode.getLineNumber());
if (val == null) {
// Try to assign from a parameter (using the param value as a context key first, then as a literal)
if (paramPairs.containsKey(var.getName())) {
val = Optional.ofNullable(
interpreter.resolveELExpression(paramPairs.get(var.getName()), tagNode.getLineNumber())
tryResolveExpression(interpreter, paramPairs.get(var.getName()), tagNode.getLineNumber())
).orElse(paramPairs.get(var.getName()));
}

Expand Down Expand Up @@ -216,4 +208,11 @@ private static String removeTrailingCommas(String token) {
}
return token;
}

private Object tryResolveExpression(JinjavaInterpreter interpreter, String expression, int lineNumber) {
try {
return interpreter.resolveELExpression(expression, lineNumber);
} catch (UnknownTokenException ignored) { }
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ModuleTagSpec extends Specification {
context.variables.put("m", [myKey: 'myValue'])

when:
def result = renderer.render("{% module myModule myOtherVar=world, subject=testerName, job=trigger.job, concat=m['my' + 'Key'], filtered=trigger.nonExist|default('hello', True) %}", context)
def result = renderer.render("{% module myModule myOtherVar=world, subject=testerName, job=trigger.job, concat=m['my' + 'Key'], filtered=trigger.nonExist|default('hello', true) %}", context)

then:
result == 'hello world, Mr. Tester Testington. You triggered myJob myValue hello'
Expand Down

0 comments on commit 35d57a5

Please sign in to comment.