This is an automated email from the ASF dual-hosted git repository.

kriskras99 pushed a commit to branch feat/default_in_custom_attributes
in repository https://gitbox.apache.org/repos/asf/avro-rs.git

commit 7489b84ef6e50911e728600b9a8d8929736cf133
Author: Kriskras99 <[email protected]>
AuthorDate: Wed Feb 11 14:35:12 2026 +0100

    fix: Enum and Fixed would put `default` in `custom_attributes`
---
 avro/src/schema/mod.rs    | 72 +++++++++++++++++++++++++++++++++++++++++++----
 avro/src/schema/parser.rs |  8 +++---
 2 files changed, 71 insertions(+), 9 deletions(-)

diff --git a/avro/src/schema/mod.rs b/avro/src/schema/mod.rs
index e2d67a3..6556dc5 100644
--- a/avro/src/schema/mod.rs
+++ b/avro/src/schema/mod.rs
@@ -294,19 +294,23 @@ impl FixedSchema {
         S: Serializer,
     {
         map.serialize_entry("type", "fixed")?;
-        if let Some(ref n) = self.name.namespace {
+        if let Some(n) = self.name.namespace.as_ref() {
             map.serialize_entry("namespace", n)?;
         }
         map.serialize_entry("name", &self.name.name)?;
-        if let Some(ref docstr) = self.doc {
+        if let Some(docstr) = self.doc.as_ref() {
             map.serialize_entry("doc", docstr)?;
         }
         map.serialize_entry("size", &self.size)?;
 
-        if let Some(ref aliases) = self.aliases {
+        if let Some(aliases) = self.aliases.as_ref() {
             map.serialize_entry("aliases", aliases)?;
         }
 
+        if let Some(default) = self.default.as_ref() {
+            map.serialize_entry("default", default)?;
+        }
+
         for attr in &self.attributes {
             map.serialize_entry(attr.0, attr.1)?;
         }
@@ -783,7 +787,7 @@ impl Serialize for Schema {
                 doc,
                 fields,
                 attributes,
-                ..
+                lookup: _lookup,
             }) => {
                 let mut map = serializer.serialize_map(None)?;
                 map.serialize_entry("type", "record")?;
@@ -808,7 +812,8 @@ impl Serialize for Schema {
                 symbols,
                 aliases,
                 attributes,
-                ..
+                default,
+                doc,
             }) => {
                 let mut map = serializer.serialize_map(None)?;
                 map.serialize_entry("type", "enum")?;
@@ -821,6 +826,12 @@ impl Serialize for Schema {
                 if let Some(aliases) = aliases {
                     map.serialize_entry("aliases", aliases)?;
                 }
+                if let Some(default) = default {
+                    map.serialize_entry("default", default)?;
+                }
+                if let Some(doc) = doc {
+                    map.serialize_entry("doc", doc)?;
+                }
                 for attr in attributes {
                     map.serialize_entry(attr.0, attr.1)?;
                 }
@@ -5128,4 +5139,55 @@ mod tests {
         );
         Ok(())
     }
+
+    #[test]
+    fn avro_rs_xxx_fixed_default_in_custom_attributes() -> TestResult {
+        let schema = Schema::parse_str(
+            r#"{
+            "name": "fixed_with_default",
+            "type": "fixed",
+            "size": 1,
+            "default": "\u0000",
+            "doc": "a docstring"
+        }"#,
+        )?;
+
+        assert_eq!(schema.custom_attributes().unwrap(), &BTreeMap::new());
+
+        let json = serde_json::to_string(&schema)?;
+        let schema2 = Schema::parse_str(&json)?;
+
+        let Schema::Fixed(fixed) = schema2 else {
+            panic!("Expected Schema::Fixed, got {schema2:?}");
+        };
+        assert!(fixed.default.is_some());
+
+        Ok(())
+    }
+
+    #[test]
+    fn avro_rs_xxx_enum_default_in_custom_attributes() -> TestResult {
+        let schema = Schema::parse_str(
+            r#"{
+            "name": "fixed_with_default",
+            "type": "enum",
+            "symbols": ["A", "B", "C"],
+            "default": "A",
+            "doc": "a docstring"
+        }"#,
+        )?;
+
+        assert_eq!(schema.custom_attributes().unwrap(), &BTreeMap::new());
+
+        let json = serde_json::to_string(&schema)?;
+        let schema2 = Schema::parse_str(&json)?;
+
+        let Schema::Enum(enum_schema) = schema2 else {
+            panic!("Expected Schema::Fixed, got {schema2:?}");
+        };
+        assert!(enum_schema.default.is_some());
+        assert!(enum_schema.doc.is_some());
+
+        Ok(())
+    }
 }
diff --git a/avro/src/schema/parser.rs b/avro/src/schema/parser.rs
index 704f56b..d6643c0 100644
--- a/avro/src/schema/parser.rs
+++ b/avro/src/schema/parser.rs
@@ -683,7 +683,7 @@ impl Parser {
             doc: complex.doc(),
             symbols,
             default,
-            attributes: self.get_custom_attributes(complex, vec!["symbols"]),
+            attributes: self.get_custom_attributes(complex, vec!["symbols", 
"default"]),
         });
 
         self.register_parsed_schema(&fully_qualified_name, &schema, &aliases);
@@ -785,8 +785,8 @@ impl Parser {
             _ => None,
         });
 
-        if default.is_some() {
-            let len = default.clone().unwrap().len();
+        if let Some(default) = &default {
+            let len = default.len();
             if len != size as usize {
                 return Err(Details::FixedDefaultLenSizeMismatch(len, 
size).into());
             }
@@ -802,7 +802,7 @@ impl Parser {
             doc,
             size: size as usize,
             default,
-            attributes: self.get_custom_attributes(complex, vec!["size"]),
+            attributes: self.get_custom_attributes(complex, vec!["size", 
"default"]),
         });
 
         self.register_parsed_schema(&fully_qualified_name, &schema, &aliases);

Reply via email to