diff --git a/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/pom.xml b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/pom.xml index c8fd38d3ac..cec2d9ddb0 100644 --- a/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/pom.xml +++ b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/pom.xml @@ -42,4 +42,18 @@ nifi-record + + + + org.apache.rat + apache-rat-plugin + + + src/test/resources/org/apache/nifi/avro/data.avro + src/test/resources/org/apache/nifi/avro/schema.json + + + + + diff --git a/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/avro/AvroTypeUtil.java b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/avro/AvroTypeUtil.java index cc1fd38d19..adbb6e36a2 100644 --- a/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/avro/AvroTypeUtil.java +++ b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/avro/AvroTypeUtil.java @@ -626,6 +626,7 @@ public class AvroTypeUtil { final Map values = new HashMap<>(recordSchema.getFieldCount()); for (final RecordField recordField : recordSchema.getFields()) { + Object value = avroRecord.get(recordField.getFieldName()); if (value == null) { for (final String alias : recordField.getAliases()) { @@ -637,6 +638,7 @@ public class AvroTypeUtil { } final String fieldName = recordField.getFieldName(); + try { final Field avroField = avroRecord.getSchema().getField(fieldName); if (avroField == null) { values.put(fieldName, null); @@ -650,6 +652,10 @@ public class AvroTypeUtil { final Object coercedValue = DataTypeUtils.convertType(rawValue, desiredType, fieldName); values.put(fieldName, coercedValue); + } catch (Exception ex) { + logger.debug("fail to convert field " + fieldName, ex ); + throw ex; + } } return values; @@ -716,6 +722,10 @@ public class AvroTypeUtil { return true; } break; + case MAP: + if (value instanceof Map) { + return true; + } } return DataTypeUtils.isCompatibleDataType(value, dataType); diff --git a/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/java/org/apache/nifi/avro/TestAvroTypeUtil.java b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/java/org/apache/nifi/avro/TestAvroTypeUtil.java index b0178298a5..cf096aab28 100644 --- a/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/java/org/apache/nifi/avro/TestAvroTypeUtil.java +++ b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/java/org/apache/nifi/avro/TestAvroTypeUtil.java @@ -20,6 +20,7 @@ package org.apache.nifi.avro; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -28,6 +29,9 @@ import java.util.Optional; import org.apache.avro.Schema; import org.apache.avro.Schema.Field; import org.apache.avro.Schema.Type; +import org.apache.avro.file.DataFileStream; +import org.apache.avro.generic.GenericDatumReader; +import org.apache.avro.generic.GenericRecord; import org.apache.nifi.schema.access.SchemaNotFoundException; import org.apache.nifi.serialization.SimpleRecordSchema; import org.apache.nifi.serialization.record.DataType; @@ -239,4 +243,20 @@ public class TestAvroTypeUtil { Assert.assertEquals(recordASchema, ((RecordDataType)recordBParentField.get().getDataType()).getChildSchema()); } + @Test + public void testMapWithNullSchema() throws IOException { + + Schema recursiveSchema = new Schema.Parser().parse(getClass().getResourceAsStream("schema.json")); + + // Make sure the following doesn't throw an exception + RecordSchema recordASchema = AvroTypeUtil.createSchema(recursiveSchema.getTypes().get(0)); + + // check the fix with the proper file + try(DataFileStream r = new DataFileStream<>(getClass().getResourceAsStream("data.avro"), + new GenericDatumReader<>())) { + GenericRecord n= r.next(); + AvroTypeUtil.convertAvroRecordToMap(n, recordASchema); + } + } + } diff --git a/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/resources/org/apache/nifi/avro/data.avro b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/resources/org/apache/nifi/avro/data.avro new file mode 100644 index 0000000000..139f244bfb Binary files /dev/null and b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/resources/org/apache/nifi/avro/data.avro differ diff --git a/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/resources/org/apache/nifi/avro/schema.json b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/resources/org/apache/nifi/avro/schema.json new file mode 100644 index 0000000000..f6fd74b28a --- /dev/null +++ b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/resources/org/apache/nifi/avro/schema.json @@ -0,0 +1,20 @@ +[ { + "namespace" : "net.a", + "type" : "record", + "name" : "O", + "fields" : [ { + "name" : "hash", + "type" : [ "null", { + "type" : "map", + "values" : "string" + } ] + } ] +}, { + "namespace" : "net.a", + "type" : "record", + "name" : "A", + "fields" : [ { + "name" : "o", + "type" : [ "null", "O" ] + }] +} ] \ No newline at end of file