Skip to content

Commit

Permalink
[jinja] Empty string result when binding is missing (openhab#10581)
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kharuzhy <antroids@gmail.com>
  • Loading branch information
antroids authored and computergeek1507 committed Jul 13, 2021
1 parent b0c93e8 commit 7080440
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.JinjavaConfig;
import com.hubspot.jinjava.interpret.FatalTemplateErrorsException;

/**
* <p>
Expand All @@ -45,7 +47,8 @@ public class JinjaTransformationService implements TransformationService {

private final Logger logger = LoggerFactory.getLogger(JinjaTransformationService.class);

private Jinjava jinjava = new Jinjava();
private final JinjavaConfig config = JinjavaConfig.newBuilder().withFailOnUnknownTokens(true).build();
private final Jinjava jinjava = new Jinjava(config);

/**
* Transforms the input <code>value</code> by Jinja template.
Expand All @@ -56,9 +59,11 @@ public class JinjaTransformationService implements TransformationService {
*/
@Override
public @Nullable String transform(String template, String value) throws TransformationException {
String transformationResult;
Map<String, @Nullable Object> bindings = new HashMap<>();

logger.debug("about to transform '{}' by the function '{}'", value, template);

Map<String, @Nullable Object> bindings = new HashMap<>();
bindings.put("value", value);

try {
Expand All @@ -68,7 +73,11 @@ public class JinjaTransformationService implements TransformationService {
// ok, then value_json is null...
}

String transformationResult = jinjava.render(template, bindings);
try {
transformationResult = jinjava.render(template, bindings);
} catch (FatalTemplateErrorsException e) {
throw new TransformationException("An error occurred while transformation. " + e.getMessage(), e);
}

logger.debug("transformation resulted in '{}'", transformationResult);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,60 @@ public void testQuotedStringOnly() throws TransformationException {
// Asserts
assertEquals("Hello world!", transformedResponse);
}

@Test
public void testJsonParsingError() throws TransformationException {
// when JSON binding parsing failed
String transformedResponse = processor.transform("Hello {{ value }}!", "{\"string\"{: \"world\"}");

// then template should be rendered
assertEquals("Hello {\"string\"{: \"world\"}!", transformedResponse);
}

@Test
public void testTemplateError() {
assertThrows(TransformationException.class,
() -> processor.transform("Hello {{{ value_json.string }}!", "{\"string\": \"world\"}"));
}

@Test
public void testMissingVariableError() {
assertThrows(TransformationException.class,
() -> processor.transform("Hello {{ missing }}!", "{\"string\": \"world\"}"));
}

@Test
public void testMissingMapKeyError() {
assertThrows(TransformationException.class,
() -> processor.transform("Hello {{ value_json.missing }}!", "{\"string\": \"world\"}"));
}

@Test
public void testMissingVariableIsDefined() throws TransformationException {
// when checking missing variable
String transformedResponse = processor.transform("{{ missing is defined }}", "{\"string\": \"world\"}");

// then missing variable is not defined
assertEquals("false", transformedResponse);
}

@Test
public void testMissingMapKeyIsDefined() throws TransformationException {
// when checking missing map key
String transformedResponse = processor.transform("{{ value_json.missing is defined }}",
"{\"string\": \"world\"}");

// then missing map key is not defined
assertEquals("false", transformedResponse);
}

@Test
public void testIsDefined() throws TransformationException {
// when checking map key
String transformedResponse = processor.transform("{{ value_json.string is defined }}",
"{\"string\": \"world\"}");

// then map key is defined
assertEquals("true", transformedResponse);
}
}

0 comments on commit 7080440

Please sign in to comment.