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 ed4e1a6d77f NIFI-15809 Record path toBytes returns an ARRAY[BYTE] type
(#11121)
ed4e1a6d77f is described below
commit ed4e1a6d77fa6ca9898b7b6731bf3a80a4924d9a
Author: Alaksiej Ščarbaty <[email protected]>
AuthorDate: Thu Apr 9 16:49:51 2026 +0200
NIFI-15809 Record path toBytes returns an ARRAY[BYTE] type (#11121)
The toBytes function previously returned a FieldValue that inherited
the source field's DataType instead of ARRAY[BYTE]. This caused type
mismatches when the source field was a string.
---
.../org/apache/nifi/record/path/functions/ToBytes.java | 16 ++++++++++++++--
.../java/org/apache/nifi/record/path/TestRecordPath.java | 15 +++++++++++++--
2 files changed, 27 insertions(+), 4 deletions(-)
diff --git
a/nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/functions/ToBytes.java
b/nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/functions/ToBytes.java
index 47275cfb1d4..737f4ed8f20 100644
---
a/nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/functions/ToBytes.java
+++
b/nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/functions/ToBytes.java
@@ -21,6 +21,7 @@ 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;
@@ -50,14 +51,25 @@ public class ToBytes extends RecordPathSegment {
final Charset charset = getCharset(this.charsetSegment,
context);
+ final RecordField originalField = fv.getField();
+ final String fieldName = originalField != null ?
originalField.getFieldName() : "toBytes";
final byte[] bytesValue;
- Byte[] src = (Byte[]) DataTypeUtils.toArray(fv.getValue(),
fv.getField().getFieldName(), RecordFieldType.BYTE.getDataType(), charset);
+ Byte[] src = (Byte[]) DataTypeUtils.toArray(fv.getValue(),
fieldName, RecordFieldType.BYTE.getDataType(), charset);
bytesValue = new byte[src.length];
for (int i = 0; i < src.length; i++) {
bytesValue[i] = src[i];
}
- return new StandardFieldValue(bytesValue, fv.getField(),
fv.getParent().orElse(null));
+ final RecordField bytesField;
+ if (originalField != null) {
+ bytesField = new
RecordField(originalField.getFieldName(),
+
RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.BYTE.getDataType()),
+ null, originalField.getAliases(), false);
+ } else {
+ bytesField = new RecordField("toBytes",
+
RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.BYTE.getDataType()));
+ }
+ return new StandardFieldValue(bytesValue, bytesField,
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 03d7c666796..9f107e00883 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
@@ -2022,7 +2022,9 @@ public class TestRecordPath {
final String originalValue = "Hello World!";
record.setValue("name", originalValue);
-
assertArrayEquals(originalValue.getBytes(StandardCharsets.UTF_16LE), (byte[])
evaluateSingleFieldValue("toBytes(/name, 'UTF-16LE')", record).getValue());
+ final FieldValue result =
evaluateSingleFieldValue("toBytes(/name, 'UTF-16LE')", record);
+
assertArrayEquals(originalValue.getBytes(StandardCharsets.UTF_16LE), (byte[])
result.getValue());
+
assertEquals(RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.BYTE.getDataType()),
result.getField().getDataType());
}
@Test
@@ -2031,13 +2033,22 @@ public class TestRecordPath {
record.setValue("name", originalValue);
record.setValue("firstName", "UTF-8");
-
assertArrayEquals(originalValue.getBytes(StandardCharsets.UTF_8), (byte[])
evaluateSingleFieldValue("toBytes(/name, /firstName)", record).getValue());
+ final FieldValue result =
evaluateSingleFieldValue("toBytes(/name, /firstName)", record);
+
assertArrayEquals(originalValue.getBytes(StandardCharsets.UTF_8), (byte[])
result.getValue());
+
assertEquals(RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.BYTE.getDataType()),
result.getField().getDataType());
}
@Test
public void throwsExceptionWhenPassedAnNonExistingCharset() {
assertThrows(IllegalCharsetNameException.class, () ->
evaluateSingleFieldValue("toBytes(/name, 'NOT A REAL CHARSET')", record));
}
+
+ @Test
+ public void handlesLiteralValue() {
+ final FieldValue result =
evaluateSingleFieldValue("toBytes('Hello', 'UTF-8')", record);
+ assertArrayEquals("Hello".getBytes(StandardCharsets.UTF_8),
(byte[]) result.getValue());
+
assertEquals(RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.BYTE.getDataType()),
result.getField().getDataType());
+ }
}
@Nested