You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2023/07/21 07:20:41 UTC

[avro] branch master updated: AVRO-3809: [Rust] Fix validate record reference with implicit namespace (#2374)

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 b43101899 AVRO-3809: [Rust] Fix validate record reference with implicit namespace (#2374)
b43101899 is described below

commit b431018996e9b408cde18b9c603e318056bddb05
Author: Andrea <an...@gmail.com>
AuthorDate: Fri Jul 21 08:20:35 2023 +0100

    AVRO-3809: [Rust] Fix validate record reference with implicit namespace (#2374)
    
    * Fix validate record reference with implicit namespace
    
    * AVRO-3809: Add the ticket number to the test method
    
    ---------
    
    Co-authored-by: Martin Grigorov <ma...@users.noreply.github.com>
---
 lang/rust/avro/src/types.rs | 67 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 65 insertions(+), 2 deletions(-)

diff --git a/lang/rust/avro/src/types.rs b/lang/rust/avro/src/types.rs
index fc1e9130f..fbc4fa004 100644
--- a/lang/rust/avro/src/types.rs
+++ b/lang/rust/avro/src/types.rs
@@ -504,7 +504,15 @@ impl Value {
                     )
                 })
             }
-            (Value::Record(record_fields), Schema::Record(RecordSchema { fields, lookup, .. })) => {
+            (
+                Value::Record(record_fields),
+                Schema::Record(RecordSchema {
+                    fields,
+                    lookup,
+                    name,
+                    ..
+                }),
+            ) => {
                 let non_nullable_fields_count =
                     fields.iter().filter(|&rf| !rf.is_nullable()).count();
 
@@ -525,6 +533,11 @@ impl Value {
                 record_fields
                     .iter()
                     .fold(None, |acc, (field_name, record_field)| {
+                        let record_namespace = if name.namespace.is_none() {
+                            enclosing_namespace
+                        } else {
+                            &name.namespace
+                        };
                         match lookup.get(field_name) {
                             Some(idx) => {
                                 let field = &fields[*idx];
@@ -533,7 +546,7 @@ impl Value {
                                     record_field.validate_internal(
                                         &field.schema,
                                         names,
-                                        enclosing_namespace,
+                                        record_namespace,
                                     ),
                                 )
                             }
@@ -1055,6 +1068,56 @@ mod tests {
     use pretty_assertions::assert_eq;
     use uuid::Uuid;
 
+    #[test]
+    fn avro_3809_validate_nested_records_with_implicit_namespace() -> TestResult {
+        let schema = Schema::parse_str(
+            r#"{
+            "name": "record_name",
+            "namespace": "space",
+            "type": "record",
+            "fields": [
+              {
+                "name": "outer_field_1",
+                "type": {
+                  "type": "record",
+                  "name": "middle_record_name",
+                  "namespace": "middle_namespace",
+                  "fields": [
+                    {
+                      "name": "middle_field_1",
+                      "type": {
+                        "type": "record",
+                        "name": "inner_record_name",
+                        "fields": [
+                          { "name": "inner_field_1", "type": "double" }
+                        ]
+                      }
+                    },
+                    { "name": "middle_field_2", "type": "inner_record_name" }
+                  ]
+                }
+              }
+            ]
+          }"#,
+        )?;
+        let value = Value::Record(vec![(
+            "outer_field_1".into(),
+            Value::Record(vec![
+                (
+                    "middle_field_1".into(),
+                    Value::Record(vec![("inner_field_1".into(), Value::Double(1.2f64))]),
+                ),
+                (
+                    "middle_field_2".into(),
+                    Value::Record(vec![("inner_field_1".into(), Value::Double(1.6f64))]),
+                ),
+            ]),
+        )]);
+
+        assert!(value.validate(&schema));
+        Ok(())
+    }
+
     #[test]
     fn validate() -> TestResult {
         let value_schema_valid = vec![