Skip to content

fix: Allow serializing an enum inside a union - #591

Merged
Kriskras99 merged 3 commits into
mainfrom
fix/enum_in_union
Jul 25, 2026
Merged

Kriskras99 merged 3 commits into
mainfrom
fix/enum_in_union

Conversation

@Kriskras99

Copy link
Copy Markdown
Contributor

Fixes #529

@Kriskras99 Kriskras99 added this to the 0.22.0 milestone Jul 15, 2026
@Kriskras99 Kriskras99 mentioned this pull request Jul 15, 2026
Comment thread avro/src/serde/ser_schema/mod.rs Resolved
let schema = Schema::parse_str(
r#"
[
"int",

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.

If I change this to "null" then the assertion below fails. It still works fine with any other primitive type (double, long, string) and "array".
Looking at the code above I'd expect it to fail with an empty record too.

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.

That's unfortunately expected behaviour. The serializer cannot tell the difference between a user having an enum with a unit variant, and a user having a untagged enum with an enum with a unit variant.
I'll add a note to the documentation that untagged enums with a null variant should not contain enums with unit variants.

Comment thread avro/src/serde/ser_schema/union.rs Resolved
Comment thread avro/src/serde/ser_schema/union.rs Outdated
} else {
for (index, schema) in self.union.variants().iter().enumerate() {
if let Schema::Enum(enum_schema) = schema
&& let Some(symbol) = enum_schema.symbols.iter().position(|s| s == variant)

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.

This may fail if there are two differently named enum schemas having symbol with the same name.

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.

Yes, but then it's the users fault that the name of the enum is wrong (either not having a rename on the enum or some other serde attribute mess). This is only the backup logic. We could also disable this logic and only allow enums with a matching name and variant (then we could even check that inside the union logic so that we also don't have the null variant footgun).

@martin-g

Copy link
Copy Markdown
Member

@PookieBuns Do you have any feedback about the solution ?

@PookieBuns

Copy link
Copy Markdown
Contributor

Hey sorry I haven't had much free time to revisit these issues the past week. Will give it a look over the weekend!

@PookieBuns

PookieBuns commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Hi thanks for the work you all put in this. Just wanted to drop my 2 cents here.

I feel uneasy about the fix at best. Primarily because https://github.com/apache/avro-rs/pull/591/changes#diff-8854c9473e6bcbc7e33057928eaaf78aed3463f171b425658f5af852855e6afeR2280 this fails, and in a way thats silent in that it doesn't fail serialization but instead serializes incorrectly. This is also a clear edge case that is I feel is common enough to appear in the wild from my experience. (i.e., this is the typical way to express nullable enums in avro).

Furthermore, my stance as a user is its okay / annoying to fail serialization and I just need to fix my code to get it running, but its potentially catastrophic if it fails silently and sends "incorrect" data out

If we wanted to support this, I feel like a less "faulty" solution would be to leverage the name field passed into the serialize_unit_variant like mentioned #591 (comment) by @Kriskras99.

I'd be much more okay with a stricter check that lets serialization fail if you don't follow the "secret handshake" over a "silent failure" that looks like I successfully serialized but actually serialized something unexpected, which is why my initial stance on this issue was

My preference would just to disallow serializing Non union rust representations using a union schema that contains that representation, but that's also a breaking change because I believe previously it was supported, although also sometimes unreliable).
because it adds complexity to the "secret handshake" users need to understand if they want potentially more ambiguous use cases (serializing an enum with a union schema) rather than more explicit mappings (serialize an enum with a enum schema).

But I'm going to leave the 2 of you up for the final say, because obviously the two of you have much more experience on this project and potentially understand more use cases users need than me, as I may have a narrow perspective thats biased towards my use cases.

@Kriskras99

Copy link
Copy Markdown
Contributor Author

@PookieBuns thanks for your feedback! I agree that we don't want to support this, unfortunately it is required to support #[serde(untagged)].

I did send in a suggestion to the Avro developer mailing list to add support for custom attributes on unions, so we can add a "org.apache.avro.rust.untagged": true attribute which would allow us to have a significantly better implementation. Unfortunately, I did not yet get a response on that email.

For now, I think further restricting it by name would be the best option and then we can further restrict it to requiring the attribute if/when that is supported by the other Avro parsers.

@Kriskras99

Copy link
Copy Markdown
Contributor Author

I've restricted it to the exact name and we'll keep it at this for now.

@Kriskras99
Kriskras99 merged commit 784294d into main Jul 25, 2026
16 checks passed
@Kriskras99
Kriskras99 deleted the fix/enum_in_union branch July 25, 2026 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants