Populating default values on GenericRecord from Avro schema if not present in RecordSchema
This closes
@@ -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> recordField = recordSchema.getField(field.name());
+ if (!recordField.isPresent() && rec.get(field.name()) == null && field.defaultVal() != null) {
+ rec.put(field.name(), field.defaultVal());
+ }
+ }
+
return rec;
}
