Skip to content

fix: Choose correct schema when serializing an Option<Enum> - #337

Merged
Kriskras99 merged 2 commits into
apache:mainfrom
PookieBuns:main
Nov 22, 2025
Merged

Kriskras99 merged 2 commits into
apache:mainfrom
PookieBuns:main

Conversation

@PookieBuns

@PookieBuns PookieBuns commented Nov 20, 2025 •

Copy link
Copy Markdown
Contributor

There is a bug currently in serialization in which if you are serializing an Option<Enum> for a Union Schema (with null), it will automatically try to use the first not-null schema if you choose a not-null enum. This fix allows the serializer to correctly resolve the proper schema to serialize the Enum variant with.

Notes

  • This changes the error message for the Union Long test. Imo its now better because it properly shows the expected type vs the attempted type instead of showing the Union as the error. Because of this, I had to change the test assert message to allow it to pass
  • Serializing unions containing records are a bit funky right now, since structs in rust should be in Capital case but if the name is not in Capital case, you have to rename the struct itself, instead of calling ex. camelCase on the Enum. However, this is pre-existing behavior so its not a regression from pre-existing behavior

Please let me know if there are any questions or things to address!

@Kriskras99

Copy link
Copy Markdown
Contributor

I was about to comment if the entire loop cannot be removed, but you already removed it😁.

I do have a concern about how this handles nested Options, if you provide a Some(None) won't it use the Schema::Null from the outer Union?

@PookieBuns

Copy link
Copy Markdown
Contributor Author

What would a nested option map to in terms of an avro schema?

@Kriskras99

Copy link
Copy Markdown
Contributor

Ah, that would map to a nested Union which is not allowed by spec, so we don't need to worry about that.

@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.

Looks good! I'll give @martin-g some time to share his thoughts and will otherwise merge it tomorrow or Saturday.

@martin-g martin-g left a comment

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.

Just some nits.

Thank you, @PookieBuns !

Comment thread avro/src/ser_schema.rs Resolved
Comment thread avro/src/ser_schema.rs Resolved
Comment thread avro/src/ser_schema.rs Resolved
Comment thread avro/src/ser_schema.rs Resolved
@martin-g martin-g added this to the 0.22.0 milestone Nov 21, 2025
@Kriskras99
Kriskras99 merged commit f76e2ff into apache:main Nov 22, 2025
@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.

3 participants