From b0c942877682d7aaebe4bec003fae3bff9be263e Mon Sep 17 00:00:00 2001 From: Bryan Bende Date: Wed, 7 Jun 2017 13:00:13 -0400 Subject: [PATCH] NIFI-4030 Populating default values on GenericRecord from Avro schema if not present in RecordSchema This closes #1896. --- .../src/main/java/org/apache/nifi/avro/AvroTypeUtil.java | 9 +++++++++ 1 file changed, 9 insertions(+) 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 1417e67f15..76438c5212 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 @@ -273,6 +273,15 @@ public class AvroTypeUtil { rec.put(fieldName, converted); } + // see if the Avro schema has any fields that aren't in the RecordSchema, and if those fields have a default + // value then we want to populate it in the GenericRecord being produced + for (final Field field : avroSchema.getFields()) { + final Optional recordField = recordSchema.getField(field.name()); + if (!recordField.isPresent() && rec.get(field.name()) == null && field.defaultVal() != null) { + rec.put(field.name(), field.defaultVal()); + } + } + return rec; }