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 <pierre.villard.fr@gmail.com>

This closes #5677.
This commit is contained in:
Mark Payne 2022-01-19 10:21:15 -05:00 committed by Pierre Villard
parent 139a269c41
commit 1d353435a5
No known key found for this signature in database
GPG Key ID: F92A93B30C07C6D5
2 changed files with 23 additions and 2 deletions

View File

@ -626,8 +626,7 @@ public class AvroTypeUtil {
continue;
}
final Optional<RecordField> 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());
}
}

View File

@ -122,6 +122,27 @@ public class TestAvroTypeUtil {
assertEquals("blue", avroRecord.get("color"));
}
@Test
public void testAvroDefaultValueWithFieldInSchemaButNotRecord() throws IOException {
final List<RecordField> 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<RecordField> 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);