Skip to content

fix: Ensure SpecificSingleObjectWriter writes header on every call - #438

Merged
martin-g merged 7 commits into
apache:mainfrom
flxo:single-object-header
Jan 26, 2026
Merged

martin-g merged 7 commits into
apache:mainfrom
flxo:single-object-header

Conversation

@flxo

@flxo flxo commented Jan 26, 2026

Copy link
Copy Markdown
Contributor

Problem

SpecificSingleObjectWriter had a bug where it only wrote the single-object encoding header on the first call to write_ref(). Subsequent calls would write only the data without the header, making those messages unreadable.

This violated the Avro single-object encoding specification, which requires
that each message be independently decodable with its own header (2-byte marker + 8-byte schema fingerprint). This is critical for use cases like message queues where individual messages are stored or transmitted separately.

Solution

This PR removes the header_written flag from SpecificSingleObjectWriter and ensures that every call to write_ref() and write() writes a complete single-object encoded message (header + data). This matches the behavior of GenericSingleObjectWriter and conforms to the Avro specification.

Changes

  • Removed the header_written field from SpecificSingleObjectWriter
  • Modified write_ref() to always write the header before writing data
  • Updated documentation to clarify that each call produces an independently decodable message
  • Added test verification that multiple writes to the same buffer each produce valid, independently decodable messages

Test Plan

The updated test in avro/src/writer.rs:1573 verifies that:

  • Multiple calls to write_ref() produce identical output to calling write_value() multiple times
  • Each write produces a complete, independently decodable message
  • Output matches GenericSingleObjectWriter behavior

Fixes #431

Previously, SpecificSingleObjectWriter had a header_written flag that
caused it to only write the single-object encoding header on the first
call to write_ref(). Subsequent calls would write only the data, making
those messages unreadable since they lacked the required header.

According to the Avro single-object encoding specification, each message
must be independently decodable with its own header (2-byte marker +
8-byte schema fingerprint). This is essential for use cases like message
queues where individual messages are stored/transmitted separately.

This fix removes the header_written flag and makes write_ref() always
write the header, matching GenericSingleObjectWriter's behavior. A new
test verifies that multiple writes to the same buffer each produce
independently decodable messages.
@flxo
flxo force-pushed the single-object-header branch from 529674d to 5fca352 Compare January 26, 2026 09:09
@martin-g

Copy link
Copy Markdown
Member

Could you please extend the example to write more than one objects and respectfully to read them.
At the moment it writes+reads just one and this was the reason to not catch the problem earlier.

Comment thread avro/src/writer.rs Resolved
use `Write::write_all` instead of `write` to ensure _all_ bytes are written.

Co-authored-by: Martin Grigorov <[email protected]>
@flxo
flxo force-pushed the single-object-header branch from 66c24ba to d7da8c3 Compare January 26, 2026 10:02
Adds a loop that writes the same object twice to ensure the header is
correctly written on every call, not just the first one.
@flxo

flxo commented Jan 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Could you please extend the example to write more than one objects and respectfully to read them. At the moment it writes+reads just one and this was the reason to not catch the problem earlier.

Extended the example/(test) with a loop. Let me know if you wish changes here.

@flxo
flxo requested a review from martin-g January 26, 2026 10:45
Comment thread avro/examples/specific_single_object.rs Resolved
Comment thread avro/examples/specific_single_object.rs Resolved
@martin-g
martin-g merged commit 6218cd7 into apache:main Jan 26, 2026
@martin-g

Copy link
Copy Markdown
Member

Thank you, @flxo !

@martin-g martin-g added this to the 0.22.0 milestone Jan 26, 2026
@flxo

flxo commented Jan 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @martin-g and @Kriskras99 for the fast review and merge!

@flxo
flxo deleted the single-object-header branch January 26, 2026 13:25
@Kriskras99 Kriskras99 mentioned this pull request Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Question: Clarify intended usage of SpecificSingleObjectWriter and header_written flag

3 participants