[jinja] Empty string result when binding is missing (#10581)
Signed-off-by: Anton Kharuzhy <antroids@gmail.com>
This commit is contained in:
parent
5d63fb1689
commit
2110e13d1b
@ -31,6 +31,8 @@ import org.slf4j.LoggerFactory;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.hubspot.jinjava.Jinjava;
|
import com.hubspot.jinjava.Jinjava;
|
||||||
|
import com.hubspot.jinjava.JinjavaConfig;
|
||||||
|
import com.hubspot.jinjava.interpret.FatalTemplateErrorsException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@ -45,7 +47,8 @@ public class JinjaTransformationService implements TransformationService {
|
|||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(JinjaTransformationService.class);
|
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.
|
* Transforms the input <code>value</code> by Jinja template.
|
||||||
@ -56,9 +59,11 @@ public class JinjaTransformationService implements TransformationService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public @Nullable String transform(String template, String value) throws TransformationException {
|
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);
|
logger.debug("about to transform '{}' by the function '{}'", value, template);
|
||||||
|
|
||||||
Map<String, @Nullable Object> bindings = new HashMap<>();
|
|
||||||
bindings.put("value", value);
|
bindings.put("value", value);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -68,7 +73,11 @@ public class JinjaTransformationService implements TransformationService {
|
|||||||
// ok, then value_json is null...
|
// 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);
|
logger.debug("transformation resulted in '{}'", transformationResult);
|
||||||
|
|
||||||
|
|||||||
@ -59,4 +59,60 @@ public class JinjaTransformationServiceTest {
|
|||||||
// Asserts
|
// Asserts
|
||||||
assertEquals("Hello world!", transformedResponse);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user