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

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


The following commit(s) were added to refs/heads/main by this push:
     new 1e64604  feat: avro_derive: Allow providing a name other than the 
ident (#239)
1e64604 is described below

commit 1e64604cba473ac2628a0fe5caa400375e6d1e46
Author: Bryan Burgers <[email protected]>
AuthorDate: Wed Jul 23 07:53:57 2025 -0500

    feat: avro_derive: Allow providing a name other than the ident (#239)
    
    * avro_derive: Allow providing a name other than the ident
    
    * avro_derive: Resolve PR feedback around asserts
    
    * Update avro_derive/tests/derive.rs
    
    Co-authored-by: Martin Grigorov <[email protected]>
    
    * Update avro_derive/tests/derive.rs
    
    Co-authored-by: Martin Grigorov <[email protected]>
    
    ---------
    
    Co-authored-by: Martin Grigorov <[email protected]>
---
 avro_derive/src/lib.rs      |  5 ++-
 avro_derive/tests/derive.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+), 1 deletion(-)

diff --git a/avro_derive/src/lib.rs b/avro_derive/src/lib.rs
index 6e84658..c447e58 100644
--- a/avro_derive/src/lib.rs
+++ b/avro_derive/src/lib.rs
@@ -51,6 +51,8 @@ struct VariantOptions {
 #[derive(darling::FromAttributes)]
 #[darling(attributes(avro))]
 struct NamedTypeOptions {
+    #[darling(default)]
+    name: Option<String>,
     #[darling(default)]
     namespace: Option<String>,
     #[darling(default)]
@@ -75,8 +77,9 @@ fn derive_avro_schema(input: &mut DeriveInput) -> 
Result<TokenStream, Vec<syn::E
         
NamedTypeOptions::from_attributes(&input.attrs[..]).map_err(darling_to_syn)?;
 
     let rename_all = parse_case(named_type_options.rename_all.as_deref(), 
input.span())?;
+    let name = named_type_options.name.unwrap_or(input.ident.to_string());
 
-    let full_schema_name = vec![named_type_options.namespace, 
Some(input.ident.to_string())]
+    let full_schema_name = vec![named_type_options.namespace, Some(name)]
         .into_iter()
         .flatten()
         .collect::<Vec<String>>()
diff --git a/avro_derive/tests/derive.rs b/avro_derive/tests/derive.rs
index a379b7b..82c377b 100644
--- a/avro_derive/tests/derive.rs
+++ b/avro_derive/tests/derive.rs
@@ -209,6 +209,41 @@ mod test_derive {
         }
     }
 
+    #[test]
+    fn avro_rs_239_test_named_record() {
+        #[derive(Debug, Serialize, Deserialize, AvroSchema, Clone, PartialEq, 
Eq)]
+        #[avro(name = "Other", namespace = "com.testing.namespace")]
+        struct TestNamedRecord {
+            a: i32,
+            b: String,
+        }
+
+        let schema = r#"
+        {
+            "type":"record",
+            "name":"com.testing.namespace.Other",
+            "fields":[
+                {
+                    "name":"a",
+                    "type":"int"
+                },
+                {
+                    "name":"b",
+                    "type":"string"
+                }
+            ]
+        }
+        "#;
+        let schema = Schema::parse_str(schema).unwrap();
+        assert_eq!(schema, TestNamedRecord::get_schema());
+        if let Schema::Record(RecordSchema { name, .. }) = 
TestNamedRecord::get_schema() {
+            assert_eq!("Other", name.name.as_str());
+            assert_eq!(Some("com.testing.namespace"), 
name.namespace.as_deref())
+        } else {
+            panic!("TestNamedRecord schema must be a record schema")
+        }
+    }
+
     #[derive(Debug, Serialize, Deserialize, AvroSchema, Clone, PartialEq)]
     struct TestAllSupportedBaseTypes {
         //Basics test
@@ -621,6 +656,55 @@ mod test_derive {
         serde_assert(enum_included);
     }
 
+    #[test]
+    fn avro_rs_239_test_enum_named() {
+        #[derive(Debug, Serialize, Deserialize, AvroSchema, Clone, PartialEq, 
Eq)]
+        #[avro(name = "Other", rename_all = "snake_case")]
+        #[serde(rename_all = "snake_case")]
+        enum TestNamedEnum {
+            A,
+            B,
+            C,
+            #[avro(rename = "e")]
+            #[serde(rename = "e")]
+            D,
+        }
+
+        #[derive(Debug, Serialize, Deserialize, AvroSchema, Clone, PartialEq, 
Eq)]
+        struct TestNamedEnumNested {
+            a: TestNamedEnum,
+            b: String,
+        }
+
+        let schema = r#"
+        {
+            "type":"record",
+            "name":"TestNamedEnumNested",
+            "fields":[
+                {
+                    "name":"a",
+                    "type": {
+                        "type":"enum",
+                        "name":"Other",
+                        "symbols":["a","b","c","e"]
+                    }
+                },
+                {
+                    "name":"b",
+                    "type":"string"
+                }
+            ]
+        }
+        "#;
+        let schema = Schema::parse_str(schema).unwrap();
+        assert_eq!(schema, TestNamedEnumNested::get_schema());
+        let enum_included = TestNamedEnumNested {
+            a: TestNamedEnum::B,
+            b: "hey".to_owned(),
+        };
+        serde_assert(enum_included);
+    }
+
     #[derive(Debug, Serialize, Deserialize, AvroSchema, Clone, PartialEq)]
     struct ConsList {
         value: i32,

Reply via email to