NIFI-10998 Fixed SplitJson to always compile new JsonPath when property changes

This closes #6803

Signed-off-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
Matthew Burgess 2022-12-22 14:03:00 -05:00 committed by exceptionfactory
parent 06c8225bbd
commit c11092b2b4
No known key found for this signature in database
GPG Key ID: 29B6A52D2AAE8DBA
2 changed files with 36 additions and 4 deletions

View File

@ -143,10 +143,8 @@ public class SplitJson extends AbstractJsonPathProcessor {
public void onPropertyModified(PropertyDescriptor descriptor, String oldValue, String newValue) {
if (descriptor.equals(ARRAY_JSON_PATH_EXPRESSION)) {
if (!StringUtils.equals(oldValue, newValue)) {
if (oldValue != null) {
// clear the cached item
JSON_PATH_REF.set(null);
}
// This value will be computed and set in customValidate()
JSON_PATH_REF.set(null);
}
}
}

View File

@ -32,6 +32,7 @@ import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
import static org.apache.nifi.flowfile.attributes.FragmentAttributes.FRAGMENT_COUNT;
@ -112,6 +113,39 @@ public class TestSplitJson {
originalOut.assertContentEquals(JSON_SNIPPET);
}
@Test
public void testSplit_change_jsonpath() throws Exception {
final TestRunner testRunner = TestRunners.newTestRunner(new SplitJson());
testRunner.setProperty(SplitJson.ARRAY_JSON_PATH_EXPRESSION, "$[0].range");
testRunner.enqueue(JSON_SNIPPET);
testRunner.run();
int numSplitsExpected = 10;
testRunner.assertTransferCount(SplitJson.REL_ORIGINAL, 1);
testRunner.getFlowFilesForRelationship(SplitJson.REL_ORIGINAL).get(0).assertAttributeEquals(FRAGMENT_COUNT.key(), String.valueOf(numSplitsExpected));
testRunner.assertTransferCount(SplitJson.REL_SPLIT, numSplitsExpected);
final MockFlowFile originalOut = testRunner.getFlowFilesForRelationship(SplitJson.REL_ORIGINAL).get(0);
originalOut.assertContentEquals(JSON_SNIPPET);
// Change JsonPath Expression, verify it is being applied correctly
testRunner.clearTransferState();
testRunner.setProperty(SplitJson.ARRAY_JSON_PATH_EXPRESSION, "$[*].name");
testRunner.enqueue(JSON_SNIPPET, Collections.singletonMap(CoreAttributes.FILENAME.key(), "test.json"));
testRunner.run();
testRunner.assertTransferCount(SplitJson.REL_ORIGINAL, 1);
final MockFlowFile originalFlowFile = testRunner.getFlowFilesForRelationship(SplitJson.REL_ORIGINAL).get(0);
originalFlowFile.assertAttributeExists(FRAGMENT_ID.key());
originalFlowFile.assertAttributeEquals(FRAGMENT_COUNT.key(), "7");
originalFlowFile.assertContentEquals(JSON_SNIPPET);
testRunner.assertTransferCount(SplitJson.REL_SPLIT, 7);
MockFlowFile flowFile = testRunner.getFlowFilesForRelationship(SplitJson.REL_SPLIT).get(0);
flowFile.assertContentEquals("{\"first\":\"Shaffer\",\"last\":\"Pearson\"}");
}
@Test
public void testSplit_arrayResult_nonScalarValues() throws Exception {
final TestRunner testRunner = TestRunners.newTestRunner(new SplitJson());