This is an automated email from the ASF dual-hosted git repository.

exceptionfactory pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/main by this push:
     new c11092b2b4 NIFI-10998 Fixed SplitJson to always compile new JsonPath 
when property changes
c11092b2b4 is described below

commit c11092b2b42177aa563edb89f347af5bb420daf4
Author: Matthew Burgess <[email protected]>
AuthorDate: Thu Dec 22 14:03:00 2022 -0500

    NIFI-10998 Fixed SplitJson to always compile new JsonPath when property 
changes
    
    This closes #6803
    
    Signed-off-by: David Handermann <[email protected]>
---
 .../apache/nifi/processors/standard/SplitJson.java |  6 ++--
 .../nifi/processors/standard/TestSplitJson.java    | 34 ++++++++++++++++++++++
 2 files changed, 36 insertions(+), 4 deletions(-)

diff --git 
a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitJson.java
 
b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitJson.java
index ea118f9c7e..21166f31bb 100644
--- 
a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitJson.java
+++ 
b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitJson.java
@@ -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);
             }
         }
     }
diff --git 
a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitJson.java
 
b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitJson.java
index 4ad847cbef..37480fb613 100644
--- 
a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitJson.java
+++ 
b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitJson.java
@@ -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());

Reply via email to