NIFI-6682 SplitJson does not work if the input is null

This closes #3772

Signed-off-by: Mike Thomsen <mthomsen@apache.org>
This commit is contained in:
Otto Fowler 2019-09-26 17:54:06 -04:00 committed by Mike Thomsen
parent 4d25b6341c
commit cfcf2e698d
No known key found for this signature in database
GPG Key ID: 88511C3D4CAD246F
2 changed files with 30 additions and 0 deletions

View File

@ -18,6 +18,7 @@ package org.apache.nifi.processors.standard;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.spi.json.JsonProvider;
@ -79,6 +80,16 @@ public abstract class AbstractJsonPathProcessor extends AbstractProcessor {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(in)) {
DocumentContext ctx = JsonPath.using(STRICT_PROVIDER_CONFIGURATION).parse(bufferedInputStream);
contextHolder.set(ctx);
} catch (IllegalArgumentException iae) {
// The JsonPath.parse() above first parses the json, then creates a context object from the parsed
// json. It is possible for the json parsing to complete without error, but produce a null object.
// In this case the context creation will fail and throw an IllegalArgumentException. This is in
// my opinion a bug in the JsonPath library, as it doesn't really throw the correct exception
// contextually.
// The general handling in derived classes handles InvalidJsonException.
// The best thing to do here, is to re-throw with the proper exception, such that the calling logic
// can route.
throw new InvalidJsonException(iae);
}
}
});

View File

@ -235,4 +235,23 @@ public class TestSplitJson {
testRunner.getFlowFilesForRelationship(SplitJson.REL_SPLIT).get(i).assertContentEquals("null");
}
}
@Test
public void testSplit_pathToInputStringNullValue() throws Exception {
final TestRunner testRunner = TestRunners.newTestRunner(new SplitJson());
testRunner.setProperty(SplitJson.ARRAY_JSON_PATH_EXPRESSION, "$.*");
ProcessSession session = testRunner.getProcessSessionFactory().createSession();
FlowFile ff = session.create();
ff = session.write(ff, new OutputStreamCallback() {
@Override
public void process(OutputStream out) throws IOException {
try (OutputStream outputStream = new BufferedOutputStream(out)) {
outputStream.write("null".getBytes(StandardCharsets.UTF_8));
}
}
});
testRunner.enqueue(ff);
testRunner.run();
testRunner.assertTransferCount(SplitJson.REL_FAILURE, 1);
}
}