From 5f7bd81af97523b6b25f206cd0810c148d8dcd4a Mon Sep 17 00:00:00 2001 From: Patrice Freydiere Date: Wed, 11 Oct 2017 22:17:15 +0200 Subject: [PATCH] NIFI-4441: patch avro maps in union types. This closes #2207. Signed-off-by: Mark Payne --- .../nifi-avro-record-utils/pom.xml | 14 ++++++++++++ .../org/apache/nifi/avro/AvroTypeUtil.java | 10 +++++++++ .../apache/nifi/avro/TestAvroTypeUtil.java | 20 ++++++++++++++++++ .../resources/org/apache/nifi/avro/data.avro | Bin 0 -> 269 bytes .../org/apache/nifi/avro/schema.json | 20 ++++++++++++++++++ 5 files changed, 64 insertions(+) create mode 100644 nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/resources/org/apache/nifi/avro/data.avro create mode 100644 nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/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/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 0000000000000000000000000000000000000000..139f244bfba877d4dfac1708e00e1f8c938f550a GIT binary patch literal 269 zcmeZI%3@>^ODrqO*DFrWNX<=L#vEO(R8m=xs$`{9l$xAhl%k}gl$V$b;`>9H#RZAU zAbui{pO%@LlTxf?1=Iyqm62GS0hEB45v`P0nvdGpmY7qT3e=@kTvC*o zm#$PB3xo(o=oUK?WpO@+tw0xp431@B<4>HcbJ1-9n=N