diff --git a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java index 70c5548a30..f0f80c1e55 100644 --- a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java +++ b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java @@ -720,10 +720,21 @@ public class DataTypeUtils { } public static boolean isArrayTypeCompatible(final Object value, final DataType elementDataType) { - return value != null - // Either an object array or a String to be converted to byte[] - && (value instanceof Object[] - || (value instanceof String && RecordFieldType.BYTE.getDataType().equals(elementDataType))); + if (value == null) { + return false; + } + // Either an object array (check the element type) or a String to be converted to byte[] + if (value instanceof Object[]) { + for (Object o : ((Object[]) value)) { + // Check each element to ensure its type is the same or can be coerced (if need be) + if (!isCompatibleDataType(o, elementDataType)) { + return false; + } + } + return true; + } else { + return value instanceof String && RecordFieldType.BYTE.getDataType().equals(elementDataType); + } } @SuppressWarnings("unchecked") diff --git a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java index d82be97151..017763ca17 100644 --- a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java +++ b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java @@ -424,6 +424,14 @@ public class TestDataTypeUtils { assertFalse(DataTypeUtils.isCompatibleDataType(new Long[]{1L, 2L}, dataType)); } + @Test + public void testIsCompatibleDataTypeArrayDifferentElementTypes() { + Object[] array = new Object[]{"2", 1}; + assertTrue(DataTypeUtils.isCompatibleDataType(array, RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.INT.getDataType()))); + array = new Object[]{Collections.singletonMap("hello", "world"), 1}; + assertFalse(DataTypeUtils.isCompatibleDataType(array, RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.INT.getDataType()))); + } + @Test public void testConvertDataTypeBigint() { final Function toBigInteger = v -> (BigInteger) DataTypeUtils.convertType(v, RecordFieldType.BIGINT.getDataType(), "field");