You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by rs...@apache.org on 2026/08/06 15:24:02 UTC

(avro) branch main updated: AVRO-4322: Use reader/writers constently with model (#3924)

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

RyanSkraba 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 42e4c3d4b0 AVRO-4322: Use reader/writers constently with model (#3924)
42e4c3d4b0 is described below

commit 42e4c3d4b03ced580c0d8186b3df133652ab445b
Author: Ryan Skraba <ry...@skraba.com>
AuthorDate: Thu Aug 6 17:23:55 2026 +0200

    AVRO-4322: Use reader/writers constently with model (#3924)
---
 .../org/apache/avro/io/FastReaderBuilderJavaClassTest.java   | 12 +++++++-----
 .../org/apache/avro/specific/TestSpecificDatumReader.java    |  6 ++++++
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/lang/java/avro/src/test/java/org/apache/avro/io/FastReaderBuilderJavaClassTest.java b/lang/java/avro/src/test/java/org/apache/avro/io/FastReaderBuilderJavaClassTest.java
index 0cb745d931..f82b70a7a2 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/io/FastReaderBuilderJavaClassTest.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/io/FastReaderBuilderJavaClassTest.java
@@ -29,8 +29,6 @@ import java.util.Map;
 import org.apache.avro.Schema;
 import org.apache.avro.SchemaBuilder;
 import org.apache.avro.generic.GenericData;
-import org.apache.avro.generic.GenericDatumReader;
-import org.apache.avro.generic.GenericDatumWriter;
 import org.apache.avro.generic.GenericRecord;
 import org.apache.avro.generic.GenericRecordBuilder;
 import org.apache.avro.specific.SpecificData;
@@ -70,20 +68,24 @@ public class FastReaderBuilderJavaClassTest {
           .set("prices", Map.of("-0.0002", "cheap", "12345.678", "expensive")).build();
 
   /**
-   * Reusable round-trip logic for a record, using the given model.
+   * Reusable round-trip logic for a record, using the given model. The reader is
+   * built via {@link GenericData#createDatumReader(Schema, Schema)} so that
+   * SpecificData produces a real {@code SpecificDatumReader}, rather than a
+   * {@code GenericDatumReader} wrapping a SpecificData instance.
    */
+  @SuppressWarnings("unchecked")
   public static GenericRecord roundTrip(GenericRecord record, GenericData model) throws IOException {
     byte[] serialized;
 
     try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
-      GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<>(record.getSchema());
+      DatumWriter<GenericRecord> writer = model.createDatumWriter(record.getSchema());
       BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(baos, null);
       writer.write(record, encoder);
       encoder.flush();
       serialized = baos.toByteArray();
     }
 
-    GenericDatumReader<GenericRecord> reader = new GenericDatumReader<>(record.getSchema(), record.getSchema(), model);
+    DatumReader<GenericRecord> reader = model.createDatumReader(record.getSchema(), record.getSchema());
     BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(serialized, null);
     return reader.read(null, decoder);
   }
diff --git a/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificDatumReader.java b/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificDatumReader.java
index 3c10b74cde..cd4589e4f3 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificDatumReader.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificDatumReader.java
@@ -68,6 +68,12 @@ public class TestSpecificDatumReader {
 
   public static class MyReader extends SpecificDatumReader<MyData> {
 
+    MyReader() {
+      // Use a new data model instead of the singleton in order to avoid modifying the
+      // setFastReaderEnabled state for other tests.
+      super(null, null, new SpecificData());
+    }
+
     @Override
     protected Class findStringClass(Schema schema) {
       return MyData.class;