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

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


The following commit(s) were added to refs/heads/master by this push:
     new 763395b7e AVRO-3812: [Rust] Handle null namespace properly for 
canonicalized schema representation (#2383)
763395b7e is described below

commit 763395b7eaad6f285d7bc59d47224eede0977f60
Author: Kousuke Saruta <[email protected]>
AuthorDate: Thu Jul 27 06:49:58 2023 +0900

    AVRO-3812: [Rust] Handle null namespace properly for canonicalized schema 
representation (#2383)
    
    * Handle null namespace properly.
    
    * Apply cargo fmt.
    
    * AVRO-3812: [Rust] Small improvements
    
    Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
    
    * Add more checks for empty namespace.
    
    * AVRO-3812: [Rust] Don't use unwrap() even in test code
    
    Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
    
    ---------
    
    Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
    Co-authored-by: Martin Tzvetanov Grigorov <[email protected]>
---
 lang/rust/avro/src/schema.rs | 54 ++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 50 insertions(+), 4 deletions(-)

diff --git a/lang/rust/avro/src/schema.rs b/lang/rust/avro/src/schema.rs
index ff1809d81..289817ef8 100644
--- a/lang/rust/avro/src/schema.rs
+++ b/lang/rust/avro/src/schema.rs
@@ -252,7 +252,8 @@ impl Name {
 
         Ok(Self {
             name: type_name.unwrap_or(name),
-            namespace: namespace_from_name.or_else(|| 
complex.string("namespace")),
+            namespace: namespace_from_name
+                .or_else(|| complex.string("namespace").filter(|ns| 
!ns.is_empty())),
         })
     }
 
@@ -267,8 +268,10 @@ impl Name {
             let namespace = self.namespace.clone().or(default_namespace);
 
             match namespace {
-                Some(ref namespace) => format!("{}.{}", namespace, self.name),
-                None => self.name.clone(),
+                Some(ref namespace) if !namespace.is_empty() => {
+                    format!("{}.{}", namespace, self.name)
+                }
+                _ => self.name.clone(),
             }
         }
     }
@@ -292,7 +295,7 @@ impl Name {
             namespace: self
                 .namespace
                 .clone()
-                .or_else(|| enclosing_namespace.clone()),
+                .or_else(|| enclosing_namespace.clone().filter(|ns| 
!ns.is_empty())),
         }
     }
 }
@@ -4819,4 +4822,47 @@ mod tests {
         }
         Ok(())
     }
+
+    #[test]
+    fn test_avro_3812_handle_null_namespace_properly() -> TestResult {
+        let schema_str = r#"
+        {
+          "namespace": "",
+          "type": "record",
+          "name": "my_schema",
+          "fields": [
+            {
+              "name": "a",
+              "type": {
+                "type": "enum",
+                "name": "my_enum",
+                "namespace": "",
+                "symbols": ["a", "b"]
+              }
+            },  {
+              "name": "b",
+              "type": {
+                "type": "fixed",
+                "name": "my_fixed",
+                "namespace": "",
+                "size": 10
+              }
+            }
+          ]
+         }
+         "#;
+
+        let expected = 
r#"{"name":"my_schema","type":"record","fields":[{"name":"a","type":{"name":"my_enum","type":"enum","symbols":["a","b"]}},{"name":"b","type":{"name":"my_fixed","type":"fixed","size":10}}]}"#;
+        let schema = Schema::parse_str(schema_str)?;
+        let canonical_form = schema.canonical_form();
+        assert_eq!(canonical_form, expected);
+
+        let name = Name::new("my_name")?;
+        let fullname = name.fullname(Some("".to_string()));
+        assert_eq!(fullname, "my_name");
+        let qname = 
name.fully_qualified_name(&Some("".to_string())).to_string();
+        assert_eq!(qname, "my_name");
+
+        Ok(())
+    }
 }

Reply via email to