Skip to content

Commit

Permalink
AVRO-4046: Respect writer's schema for default values
Browse files Browse the repository at this point in the history
Only use default values from reader's schema for fields that are not defined in the writer's schema
  • Loading branch information
Michel Hartmann committed Aug 30, 2024
1 parent 8fad7dd commit 121673a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions lang/php/lib/Datum/AvroIODatumReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ public function readRecord($writers_schema, $readers_schema, $decoder)
}
}
// Fill in default values
$writers_fields = $writers_schema->fieldsHash();
foreach ($readers_fields as $field_name => $field) {
if (isset($writers_fields[$field_name])) {
continue;
Expand Down
34 changes: 33 additions & 1 deletion lang/php/test/IODatumReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testSchemaMatching()
JSON;
$readers_schema = $writers_schema;
$this->assertTrue(AvroIODatumReader::schemasMatch(
AvroSchema::parse($writers_schema),
AvroSchema::parse($writers_schema),
AvroSchema::parse($readers_schema)));
}

Expand Down Expand Up @@ -90,4 +90,36 @@ public function testRecordNullField()

$this->assertSame('0200', bin2hex($bin));
}

public function testRecordFieldWithDefault()
{
$schema = AvroSchema::parse(<<<_JSON
{
"name": "RecordWithDefaultValue",
"type": "record",
"fields": [
{
"name": "field1",
"type": "string",
"default": "default"
}
]
}
_JSON
);

$io = new AvroStringIO();
$writer = new AvroIODatumWriter();
$writer->writeData($schema, ['field1' => "foobar"], new AvroIOBinaryEncoder($io));

$bin = $io->string();
$reader = new AvroIODatumReader();
$record = $reader->readRecord(
$schema,
$schema,
new AvroIOBinaryDecoder(new AvroStringIO($bin))
);

$this->assertEquals(['field1' => "foobar"], $record);
}
}

0 comments on commit 121673a

Please sign in to comment.