Skip to content

Commit bf3357e

Browse files
committed
fix: Don't allow serializing a unit variant inside the UnionSerializer as that would require a nested union
1 parent 6dd2abc commit bf3357e

1 file changed

Lines changed: 8 additions & 11 deletions

File tree

avro/src/serde/ser_schema/union.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ use crate::{
3535
util::{zig_i32, zig_i64},
3636
};
3737

38+
/// Serializer that finds the right union variant for the type being serialized.
39+
///
40+
/// `serialize_*_variant`, `serialize_some`, and `serialize_none` will all return an error as nested
41+
/// unions are invalid in the Avro specification.
3842
pub struct UnionSerializer<'s, 'w, W: Write, S: Borrow<Schema>> {
3943
writer: &'w mut W,
4044
union: &'s UnionSchema,
@@ -343,18 +347,11 @@ impl<'s, 'w, W: Write, S: Borrow<Schema>> Serializer for UnionSerializer<'s, 'w,
343347

344348
fn serialize_unit_variant(
345349
self,
346-
name: &'static str,
347-
variant_index: u32,
348-
variant: &'static str,
350+
_: &'static str,
351+
_: u32,
352+
_: &'static str,
349353
) -> Result<Self::Ok, Self::Error> {
350-
match self.union.find_named_schema(name, self.config.names)? {
351-
Some((index, Schema::Enum(schema))) if schema.symbols.get(variant_index as usize) == Some(&variant.to_string()) => {
352-
let mut bytes_written = zig_i32(index as i32, &mut *self.writer)?;
353-
bytes_written += zig_i32(variant_index as i32, &mut *self.writer)?;
354-
Ok(bytes_written)
355-
}
356-
_ => Err(self.error("unit variant", format!("Expected Schema::Enum(name: {name}, symbols[{variant_index}] == {variant}) in variants"))),
357-
}
354+
Err(self.error("unit variant", "Nested unions are not supported"))
358355
}
359356

360357
fn serialize_newtype_struct<T>(

0 commit comments

Comments
 (0)