Skip to content

fix: Resolve Schema::Ref in SpecificSingleObjectWriter::write_ref() - #439

Closed
flxo wants to merge 1 commit into
apache:mainfrom
flxo:recursive-single-object
Closed

flxo wants to merge 1 commit into
apache:mainfrom
flxo:recursive-single-object

Conversation

@flxo

@flxo flxo commented Jan 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes a bug in SpecificSingleObjectWriter::write_ref() where Schema::Ref types failed to resolve during serialization of recursive schemas.

Problem

The write_ref() method was calling write_avro_datum_ref() which created a serializer with an empty names map. This caused Schema::Ref types to fail resolution during serialization, preventing recursive schemas from being properly serialized.

Solution

Modified SpecificSingleObjectWriter::write_ref() to:

  • Use the resolved schema names from the inner GenericSingleObjectWriter's ResolvedOwnedSchema
  • Pass these names to the serializer, enabling proper Schema::Ref resolution

Also updated write_avro_datum_ref() to create a ResolvedSchema from the input schema and use its names map instead of an empty map.

Test Plan

Added test_specific_single_object_writer_with_schema_ref() which:

  • Defines a recursive Node schema with a self-referencing field
  • Creates a nested node structure (parent with child)
  • Verifies the writer successfully serializes the recursive structure
  • Validates the serialized output contains both "parent" and "child" values

Open questions:

- [x] This change makes write_avro_datum_ref use SchemAwareWriteSerializer which is probably more expensive than a plain write_avro_datum_ref. Shall this be part of the MR?

@martin-g

Copy link
Copy Markdown
Member
  • This change makes write_avro_datum_ref use SchemAwareWriteSerializer which is probably more expensive than a plain write_avro_datum_ref. Shall this be part of the MR?

I don't understand the question.
https://github.com/apache/avro-rs/pull/439/changes#diff-ceb4cd185f56c284ac854e0689daddc24c72649c7c791ebd09e934cb0c1049eaL733 - it was already using SchemaAwareWriteSerializer::new(writer, schema, &names, None);
With your fix it uses the resolved names. This looks good to me!

Comment thread avro/src/writer.rs
.inner
.resolved
.get_names()
.iter()

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.

Why do we need to iterate + map + collect ?
Can't we just use the result of get_names() ?

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.

I we need a NamesRef and get_names return Names:

ResolvedOwnedSchema::get_names() returns Names which is the "owned" version pub type Names = HashMap<Name, Schema>;.

SchemaAwareSerializer::new takes a NamesRef which is pub type NamesRef<'a> = HashMap<Name, &'a Schema>;.

I haven't found a way to avoid the nasty map duplication. I decided to keep it in here - it's a onetime thing.

Previously, write_ref() called write_avro_datum_ref() which creates a
serializer with an empty names map, causing Schema::Ref types to fail
resolution during serialization.

Now the serializer receives the resolved schema names from
ResolvedOwnedSchema, enabling proper Schema::Ref resolution for
recursive schemas.

Added test: test_specific_single_object_writer_with_schema_ref and
extend the test schema with a recursion.
@flxo

flxo commented Jan 26, 2026

Copy link
Copy Markdown
Contributor Author
  • This change makes write_avro_datum_ref use SchemAwareWriteSerializer which is probably more expensive than a plain write_avro_datum_ref. Shall this be part of the MR?

I don't understand the question. https://github.com/apache/avro-rs/pull/439/changes#diff-ceb4cd185f56c284ac854e0689daddc24c72649c7c791ebd09e934cb0c1049eaL733 - it was already using SchemaAwareWriteSerializer::new(writer, schema, &names, None); With your fix it uses the resolved names. This looks good to me!

Thanks having a look. I'm not sure anymore why I wrote that - probably I looked at a wrong fn;. You're right that write_avro_datum_ref now just got proper Names.

@flxo
flxo force-pushed the recursive-single-object branch from ca60165 to 88a48e1 Compare January 26, 2026 13:48
@Kriskras99 Kriskras99 mentioned this pull request Jan 26, 2026

@Kriskras99 Kriskras99 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think this is the right way to solve this. To properly fix this, ResolvedOwnedSchema needs to be reworked, I've started work on this in a branch and I'll open a draft PR tomorrow

@flxo

flxo commented Jan 27, 2026

Copy link
Copy Markdown
Contributor Author

I don't think this is the right way to solve this. To properly fix this, ResolvedOwnedSchema needs to be reworked, I've started work on this in a branch and I'll open a draft PR tomorrow

Thanks for your review @Kriskras99!

You're writing in #442: "This is related to #439, but doesn't directly support fixing it". What's the forward path then to fix single object encoding for recursive types?

@flxo

flxo commented Jan 27, 2026 •

Copy link
Copy Markdown
Contributor Author

I don't think this is the right way to solve this. To properly fix this, ResolvedOwnedSchema needs to be reworked, I've started work on this in a branch and I'll open a draft PR tomorrow

Thanks for your review @Kriskras99!

You're writing in #442: "This is related to #439, but doesn't directly support fixing it". What's the forward path then to fix single object encoding for recursive types?

Looks like #445 is solving the issue addressed in this PR. @Kriskras99: feel free to close #439 if applicable.

@Kriskras99

Copy link
Copy Markdown
Contributor

If #445 is merged, Github will automatically close this PR.

@flxo thanks for notifying us of this issue and supplying a patch!

@flxo
flxo deleted the recursive-single-object branch May 4, 2026 08:53
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.

3 participants