Kriskras99 commented on code in PR #591:
URL: https://github.com/apache/avro-rs/pull/591#discussion_r3605830254


##########
avro/src/serde/ser_schema/union.rs:
##########
@@ -356,11 +356,37 @@ impl<'s, 'w, W: Write, S: Borrow<Schema>> Serializer for 
UnionSerializer<'s, 'w,
 
     fn serialize_unit_variant(
         self,
-        _: &'static str,
-        _: u32,
-        _: &'static str,
+        name: &'static str,
+        variant_index: u32,
+        variant: &'static str,
     ) -> Result<Self::Ok, Self::Error> {
-        Err(self.error("unit variant", "Nested unions are not supported"))
+        if let Some((index, Schema::Enum(enum_schema))) =
+            self.union.find_named_schema(name, self.config.names)?
+            && enum_schema
+                .symbols
+                .get(variant_index as usize)
+                .map(String::as_str)
+                == Some(variant)
+        {
+            // Fast path for if the name and variant match (no #[serde(skip)] 
or other trickery)
+            let mut bytes_written = zig_i32(index as i32, &mut *self.writer)?;
+            bytes_written += zig_i32(variant_index as i32, &mut *self.writer)?;
+            Ok(bytes_written)
+        } 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)

Review 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).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to