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


##########
avro_derive/tests/derive.rs:
##########
@@ -1686,4 +1686,103 @@ mod test_derive {
             panic!("Unexpected schema type for Foo")
         }
     }
+
+    #[test]
+    fn avro_247_serde_flatten_support() {
+        #[derive(Debug, Serialize, Deserialize, AvroSchema, Clone, PartialEq)]
+        struct Nested {
+            a: bool,
+        }
+
+        #[derive(Debug, Serialize, Deserialize, AvroSchema, Clone, PartialEq)]
+        struct Foo {
+            #[serde(flatten)]
+            #[avro(flatten)]
+            nested: Nested,
+            b: i32,
+        }
+
+        let schema = r#"
+        {
+            "type":"record",
+            "name":"Foo",
+            "fields": [
+                {
+                    "name":"a",
+                    "type":"boolean"
+                },
+                {
+                    "name":"b",
+                    "type":"int"
+                }
+            ]
+        }
+        "#;
+
+        let schema = Schema::parse_str(schema).unwrap();
+        let derived_schema = Foo::get_schema();
+        if let Schema::Record(RecordSchema { name, fields, .. }) = 
&derived_schema {
+            assert_eq!("Foo", name.fullname(None));
+            for field in fields {
+                match field.name.as_str() {
+                    "a" | "b" => (), // expected
+                    name => panic!("Unexpected field name '{name}'"),
+                }
+            }
+        } else {
+            panic!("Foo schema must be a record schema: {derived_schema:?}")
+        }
+        assert_eq!(schema, derived_schema);
+
+        serde_assert(Foo {
+            nested: Nested { a: true },
+            b: 321,
+        });
+    }
+
+    #[test]
+    fn avro_247_serde_nested_flatten_support() {
+        use apache_avro::{AvroSchema, Reader, Writer, from_value};
+        use serde::{Deserialize, Serialize};
+
+        #[derive(AvroSchema, Serialize, Deserialize, PartialEq, Debug)]
+        pub struct NestedFoo {
+            one: u32,
+        }
+
+        #[derive(AvroSchema, Debug, Serialize, Deserialize, PartialEq)]
+        pub struct Foo {
+            #[serde(flatten)]
+            #[avro(flatten)]
+            nested_foo: NestedFoo,
+        }
+
+        #[derive(AvroSchema, Serialize, Debug, Deserialize, PartialEq)]
+        struct Bar {
+            foo: Foo,
+            two: u32,
+        }
+
+        let bar = Bar {
+            foo: Foo {
+                nested_foo: NestedFoo { one: 42 },
+            },
+            two: 2,
+            // test_enum: TestEnum::B,
+        };
+
+        let schema = Bar::get_schema();
+        println!("Generated schema: {:#?}", schema);
+
+        // When appending a value, use this crate's special extension method
+        let mut writer = Writer::new(&schema, Vec::new()).unwrap();
+        writer.append_ser(&bar).unwrap();
+
+        // Check that it was correctly serialized and is deserializable
+        let encoded = writer.into_inner().unwrap();
+        let mut reader = Reader::new(&encoded[..]).unwrap();
+        let value = reader.next().unwrap().unwrap();
+        let result: Bar = from_value(&value).unwrap();
+        assert_eq!(result, bar);
+    }

Review Comment:
   I also made the tests a lot better (like actually using serde_assert for the 
second test)



-- 
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