From 1d353435a54d0fde8a12eed456e5114e54274e83 Mon Sep 17 00:00:00 2001 From: Mark Payne Date: Wed, 19 Jan 2022 10:21:15 -0500 Subject: [PATCH] NIFI-9594: When converting Record to Avro GenericRecord, ensure that any default values that are defined in the GenericRecord's schema get applied, regardless of whether or not the field exists in the associated RecordSchema. Signed-off-by: Pierre Villard This closes #5677. --- .../org/apache/nifi/avro/AvroTypeUtil.java | 3 +-- .../apache/nifi/avro/TestAvroTypeUtil.java | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) 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 01f4fa7552..d8d6e10c6e 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,8 +626,7 @@ public class AvroTypeUtil { continue; } - final Optional recordField = recordSchema.getField(field.name()); - if (!recordField.isPresent() && rec.get(field.name()) == null) { + if (rec.get(field.name()) == null) { rec.put(field.name(), field.defaultVal()); } } 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 05d31a0237..dac8024f12 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 @@ -122,6 +122,27 @@ public class TestAvroTypeUtil { assertEquals("blue", avroRecord.get("color")); } + @Test + public void testAvroDefaultValueWithFieldInSchemaButNotRecord() throws IOException { + final List fields = new ArrayList<>(); + fields.add(new RecordField("name", RecordFieldType.STRING.getDataType())); + fields.add(new RecordField("color", RecordFieldType.STRING.getDataType())); + final RecordSchema personSchema = new SimpleRecordSchema(fields); + + final org.apache.nifi.serialization.record.Record record = new MapRecord(personSchema, Collections.singletonMap("name", "John Doe")); + final Schema avroSchema = SchemaBuilder.record("person").namespace("nifi") + .fields() + .requiredString("name") + .name("color").type().stringType().stringDefault("blue") + .endRecord(); + + final GenericRecord avroRecord = AvroTypeUtil.createAvroRecord(record, avroSchema); + assertEquals("John Doe", avroRecord.get("name")); + assertEquals("blue", avroRecord.get("color")); + + } + + @Test public void testCreateAvroSchemaPrimitiveTypes() { final List fields = new ArrayList<>(); @@ -625,6 +646,7 @@ public class TestAvroTypeUtil { assertTrue(field4.aliases().contains(" __ Another ONE!!")); } + @Test public void testListToArrayConversion() { final Charset charset = Charset.forName("UTF-8"); Object o = AvroTypeUtil.convertToAvroObject(Collections.singletonList("Hello"), Schema.createArray(Schema.create(Type.STRING)), charset);