This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/main by this push:
     new 2d10b319f AVRO-4046: Respect writer's schema for default values (#3127)
2d10b319f is described below

commit 2d10b319f4f5e3e8a10a686b68a14010f6de527d
Author: Michel Hartmann <[email protected]>
AuthorDate: Thu Sep 12 09:39:57 2024 +0200

    AVRO-4046: Respect writer's schema for default values (#3127)
    
    Only use default values from reader's schema for fields that are not 
defined in the writer's schema
    
    Co-authored-by: Michel Hartmann <[email protected]>
---
 lang/php/lib/Datum/AvroIODatumReader.php |  1 +
 lang/php/test/IODatumReaderTest.php      | 34 +++++++++++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/lang/php/lib/Datum/AvroIODatumReader.php 
b/lang/php/lib/Datum/AvroIODatumReader.php
index a479c2bd6..c9a5ae395 100644
--- a/lang/php/lib/Datum/AvroIODatumReader.php
+++ b/lang/php/lib/Datum/AvroIODatumReader.php
@@ -358,6 +358,7 @@ class AvroIODatumReader
             }
         }
         // Fill in default values
+        $writers_fields = $writers_schema->fieldsHash();
         foreach ($readers_fields as $field_name => $field) {
             if (isset($writers_fields[$field_name])) {
                 continue;
diff --git a/lang/php/test/IODatumReaderTest.php 
b/lang/php/test/IODatumReaderTest.php
index dc6ead359..acdc8c484 100644
--- a/lang/php/test/IODatumReaderTest.php
+++ b/lang/php/test/IODatumReaderTest.php
@@ -37,7 +37,7 @@ class IODatumReaderTest extends TestCase
 JSON;
         $readers_schema = $writers_schema;
         $this->assertTrue(AvroIODatumReader::schemasMatch(
-            AvroSchema::parse($writers_schema),
+                AvroSchema::parse($writers_schema),
             AvroSchema::parse($readers_schema)));
     }
 
@@ -90,4 +90,36 @@ _JSON;
 
         $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);
+    }
 }

Reply via email to