mirror of https://github.com/apache/nifi.git
NIFI-5268: Fix JostTransformJSON spec validation with EL
Signed-off-by: Pierre Villard <pierre.villard.fr@gmail.com> This closes #2762.
This commit is contained in:
parent
504152eaa1
commit
71cf3fd46d
|
@ -209,13 +209,15 @@ public class JoltTransformJSON extends AbstractProcessor {
|
|||
}
|
||||
|
||||
final String specValue = validationContext.getProperty(JOLT_SPEC).getValue();
|
||||
final String invalidExpressionMsg = validationContext.newExpressionLanguageCompiler().validateExpression(specValue,true);
|
||||
|
||||
if (validationContext.isExpressionLanguagePresent(specValue) && invalidExpressionMsg != null) {
|
||||
final String customMessage = "The expression language used withing this specification is invalid";
|
||||
results.add(new ValidationResult.Builder().valid(false)
|
||||
.explanation(customMessage)
|
||||
.build());
|
||||
if (validationContext.isExpressionLanguagePresent(specValue)) {
|
||||
final String invalidExpressionMsg = validationContext.newExpressionLanguageCompiler().validateExpression(specValue,true);
|
||||
if (!StringUtils.isEmpty(invalidExpressionMsg)) {
|
||||
results.add(new ValidationResult.Builder().valid(false)
|
||||
.subject(JOLT_SPEC.getDisplayName())
|
||||
.explanation("Invalid Expression Language: " + invalidExpressionMsg)
|
||||
.build());
|
||||
}
|
||||
} else {
|
||||
//for validation we want to be able to ensure the spec is syntactically correct and not try to resolve variables since they may not exist yet
|
||||
Object specJson = SORTR.getValue().equals(transform) ? null : JsonUtils.jsonToObject(specValue.replaceAll("\\$\\{","\\\\\\\\\\$\\{"), DEFAULT_CHARSET);
|
||||
|
|
|
@ -23,6 +23,8 @@ import java.io.IOException;
|
|||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.nifi.flowfile.attributes.CoreAttributes;
|
||||
|
@ -417,5 +419,32 @@ public class TestJoltTransformJSON {
|
|||
assertTrue(DIFFY.diff(compareJson, transformedJson).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoltSpecEL() throws IOException {
|
||||
final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
|
||||
final String spec = "${joltSpec}";
|
||||
runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
|
||||
runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM,JoltTransformJSON.DEFAULTR);
|
||||
final Map<String, String> attributes = Collections.singletonMap("joltSpec",
|
||||
"{\"RatingRange\":5,\"rating\":{\"*\":{\"MaxLabel\":\"High\",\"MinLabel\":\"Low\",\"DisplayType\":\"NORMAL\"}}}");
|
||||
runner.enqueue(JSON_INPUT, attributes);
|
||||
runner.run();
|
||||
runner.assertAllFlowFilesTransferred(JoltTransformJSON.REL_SUCCESS);
|
||||
final MockFlowFile transformed = runner.getFlowFilesForRelationship(JoltTransformJSON.REL_SUCCESS).get(0);
|
||||
transformed.assertAttributeExists(CoreAttributes.MIME_TYPE.key());
|
||||
transformed.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(),"application/json");
|
||||
Object transformedJson = JsonUtils.jsonToObject(new ByteArrayInputStream(transformed.toByteArray()));
|
||||
Object compareJson = JsonUtils.jsonToObject(Files.newInputStream(Paths.get("src/test/resources/TestJoltTransformJson/defaultrOutput.json")));
|
||||
assertTrue(DIFFY.diff(compareJson, transformedJson).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoltSpecInvalidEL() throws IOException {
|
||||
final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
|
||||
final String spec = "${joltSpec:nonExistingFunction()}";
|
||||
runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
|
||||
runner.enqueue(JSON_INPUT);
|
||||
runner.assertNotValid();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue