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

exceptionfactory pushed a commit to branch support/nifi-1.x
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/support/nifi-1.x by this push:
     new 8d594eaa89 NIFI-12704 Avoid NPE in escapeJson() for Root Path
8d594eaa89 is described below

commit 8d594eaa891eabb61dba73045daad06fdc12c728
Author: EndzeitBegins <16666115+endzeitbeg...@users.noreply.github.com>
AuthorDate: Sun Feb 25 09:19:58 2024 +0100

    NIFI-12704 Avoid NPE in escapeJson() for Root Path
    
    This closes #8450
    
    Signed-off-by: David Handermann <exceptionfact...@apache.org>
    (cherry picked from commit a52d6a8214dc2b323ae238a87eb5fc7571de72cc)
---
 .../nifi/record/path/paths/RecordPathCompiler.java    |  4 ++++
 .../org/apache/nifi/record/path/TestRecordPath.java   | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+)

diff --git 
a/nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/paths/RecordPathCompiler.java
 
b/nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/paths/RecordPathCompiler.java
index c6f23b145b..5e484b3016 100644
--- 
a/nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/paths/RecordPathCompiler.java
+++ 
b/nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/paths/RecordPathCompiler.java
@@ -128,6 +128,10 @@ public class RecordPathCompiler {
                 return new RootPath();
             }
             case CHILD_REFERENCE: {
+                if (tree.getChildCount() == 0) {
+                    return new RootPath();
+                }
+
                 final Tree childTree = tree.getChild(0);
                 final int childTreeType = childTree.getType();
                 if (childTreeType == FIELD_NAME) {
diff --git 
a/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/TestRecordPath.java
 
b/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/TestRecordPath.java
index 8bd9d0c62f..3aa8ea1bef 100644
--- 
a/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/TestRecordPath.java
+++ 
b/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/TestRecordPath.java
@@ -56,6 +56,7 @@ import java.util.stream.IntStream;
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
+    import static org.junit.jupiter.api.Assertions.assertInstanceOf;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -2031,6 +2032,17 @@ public class TestRecordPath {
         assertEquals(48, fieldValues.get(0).getValue());
     }
 
+    @Test
+    public void testRecordRootReferenceInFunction() {
+        final Record record = createSimpleRecord();
+
+        final FieldValue singleArgumentFieldValue = 
evaluateSingleFieldValue("escapeJson(/)", record);
+        assertEquals("{\"id\":48,\"name\":\"John Doe\",\"missing\":null}", 
singleArgumentFieldValue.getValue());
+        final FieldValue multipleArgumentsFieldValue = 
evaluateSingleFieldValue("mapOf(\"copy\",/)", record);
+        assertInstanceOf(MapRecord.class, 
multipleArgumentsFieldValue.getValue());
+        assertEquals(record.toString(), ((MapRecord) 
multipleArgumentsFieldValue.getValue()).getValue("copy"));
+    }
+
     private List<RecordField> getDefaultFields() {
         final List<RecordField> fields = new ArrayList<>();
         fields.add(new RecordField("id", RecordFieldType.INT.getDataType()));
@@ -2068,4 +2080,11 @@ public class TestRecordPath {
         return new MapRecord(schema, values);
     }
 
+    private static FieldValue evaluateSingleFieldValue(RecordPath recordPath, 
Record record) {
+        return 
recordPath.evaluate(record).getSelectedFields().findFirst().get();
+    }
+
+    private static FieldValue evaluateSingleFieldValue(String path, Record 
record) {
+        return evaluateSingleFieldValue(RecordPath.compile(path), record);
+    }
 }

Reply via email to