Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public String getContent() {

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());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, it looks like this was a hidden bug -- if we setFastReaderEnabled(true) or setFastReaderEnabled(false) on the singleton instance of SpecificData, it overrides the system property and in other tests we end up not testing what we thought we were testing...

I checked through the code and this looks like the only spot where it gets changed in the singleton.

@iemejia iemejia Aug 6, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, that's a bit weird so we were not really testing everything correctly ufff. Oh wait that's Test specific, I see now.

}

@Override
protected Class findStringClass(Schema schema) {
return MyData.class;
Expand Down
Loading