mirror of https://github.com/apache/nifi.git
NIFI-7637: Ensure all array elements are compatible in DataTypeUtils.isArrayTypeCompatible() (#4612)
This commit is contained in:
parent
77b4abc21e
commit
4fee076561
|
@ -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")
|
||||
|
|
|
@ -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<Object, BigInteger> toBigInteger = v -> (BigInteger) DataTypeUtils.convertType(v, RecordFieldType.BIGINT.getDataType(), "field");
|
||||
|
|
Loading…
Reference in New Issue