Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVRO-4046: [PHP] Handling of default values #3127

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}