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

pvillard 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 29a3144dcd7 NIFI-15807 Record path toString returns a string type 
(#11119)
29a3144dcd7 is described below

commit 29a3144dcd7ee7271a0859a3a9be79b212d65d96
Author: Alaksiej Ščarbaty <[email protected]>
AuthorDate: Thu Apr 9 16:43:47 2026 +0200

    NIFI-15807 Record path toString returns a string type (#11119)
    
    The toString function previously returned a FieldValue that inherited
    the source field's DataType instead of STRING. This caused type
    mismatches when the source field was not already a string (e.g.
    ARRAY[BYTE]).
---
 .../org/apache/nifi/record/path/functions/ToString.java   | 12 +++++++++++-
 .../java/org/apache/nifi/record/path/TestRecordPath.java  | 15 +++++++++++++--
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git 
a/nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/functions/ToString.java
 
b/nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/functions/ToString.java
index 7204ffc9e70..bd2ec39ec43 100644
--- 
a/nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/functions/ToString.java
+++ 
b/nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/functions/ToString.java
@@ -21,6 +21,8 @@ import 
org.apache.nifi.record.path.RecordPathEvaluationContext;
 import org.apache.nifi.record.path.StandardFieldValue;
 import org.apache.nifi.record.path.paths.RecordPathSegment;
 import org.apache.nifi.record.path.util.RecordPathUtils;
+import org.apache.nifi.serialization.record.RecordField;
+import org.apache.nifi.serialization.record.RecordFieldType;
 import org.apache.nifi.serialization.record.util.DataTypeUtils;
 
 import java.nio.charset.Charset;
@@ -63,7 +65,15 @@ public class ToString extends RecordPathSegment {
                     } else {
                         stringValue = DataTypeUtils.toString(fv.getValue(), 
(String) null, charset);
                     }
-                    return new StandardFieldValue(stringValue, fv.getField(), 
fv.getParent().orElse(null));
+                    final RecordField originalField = fv.getField();
+                    final RecordField stringField;
+                    if (originalField != null) {
+                        stringField = new 
RecordField(originalField.getFieldName(), RecordFieldType.STRING.getDataType(),
+                            null, originalField.getAliases(), false);
+                    } else {
+                        stringField = new RecordField("toString", 
RecordFieldType.STRING.getDataType());
+                    }
+                    return new StandardFieldValue(stringValue, stringField, 
fv.getParent().orElse(null));
                 });
     }
 
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 00bda40fd4d..c9bae3a2798 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
@@ -2157,7 +2157,9 @@ public class TestRecordPath {
             public void decodesBytesAsStringUsingTheDefinedCharset() {
                 record.setValue("bytes", "Hello 
World!".getBytes(StandardCharsets.UTF_16));
 
-                assertEquals("Hello World!", 
evaluateSingleFieldValue("toString(/bytes, 'UTF-16')", record).getValue());
+                final FieldValue result = 
evaluateSingleFieldValue("toString(/bytes, 'UTF-16')", record);
+                assertEquals("Hello World!", result.getValue());
+                assertEquals(RecordFieldType.STRING.getDataType(), 
result.getField().getDataType());
             }
 
             @Test
@@ -2165,13 +2167,22 @@ public class TestRecordPath {
                 record.setValue("bytes", "Hello 
World!".getBytes(StandardCharsets.UTF_8));
                 record.setValue("name", "UTF-8");
 
-                assertEquals("Hello World!", 
evaluateSingleFieldValue("toString(/bytes, /name)", record).getValue());
+                final FieldValue result = 
evaluateSingleFieldValue("toString(/bytes, /name)", record);
+                assertEquals("Hello World!", result.getValue());
+                assertEquals(RecordFieldType.STRING.getDataType(), 
result.getField().getDataType());
             }
 
             @Test
             public void throwsExceptionWhenPassedAnNonExistingCharset() {
                 assertThrows(IllegalCharsetNameException.class, () -> 
evaluateSingleFieldValue("toString(/bytes, 'NOT A REAL CHARSET')", record));
             }
+
+            @Test
+            public void handlesLiteralValue() {
+                final FieldValue result = 
evaluateSingleFieldValue("toString('literalValue', 'UTF-8')", record);
+                assertEquals("literalValue", result.getValue());
+                assertEquals(RecordFieldType.STRING.getDataType(), 
result.getField().getDataType());
+            }
         }
 
         @Nested

Reply via email to