Repository navigation
fix: Allow serializing an enum inside a union - #591
Conversation
| let schema = Schema::parse_str( | ||
| r#" | ||
| [ | ||
| "int", |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| } 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) |
There was a problem hiding this comment.
This may fail if there are two differently named enum schemas having symbol with the same name.
There was a problem hiding this comment.
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).
|
@PookieBuns Do you have any feedback about the solution ? |
|
Hey sorry I haven't had much free time to revisit these issues the past week. Will give it a look over the weekend! |
|
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 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
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. |
|
@PookieBuns thanks for your feedback! I agree that we don't want to support this, unfortunately it is required to support 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 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. |
|
I've restricted it to the exact name and we'll keep it at this for now. |
Fixes #529