Skip to content

missing support for #[serde(flatten)] for derive AvroSchema #247

@elad-yosifon

Description

@elad-yosifon

I tried to play around with the proc-macro, and was able to make this test pass:

#[test]
    fn avro_flatten_fields() {
        #[derive(Debug, Serialize, Deserialize, AvroSchema, Clone, PartialEq)]
        struct Nested {
            a: bool,
        }


        #[derive(Debug, Serialize, Deserialize, AvroSchema, Clone, PartialEq)]
        struct Foo {
            #[serde(flatten)]
            nested: Nested,
            b: i32,
        }

        let schema = r#"
        {
            "type":"record",
            "name":"Foo",
            "fields": [
                {
                    "name":"a1",
                    "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);

       // deserializing not support
       // serde_assert(Foo {
       //     nested: Nested { a: true },
       //     b: 321,
       // });
    }

but while running the other tests, it broke the serde(rename) related tests since the schema code genrator changed in order to allow nested fields to be pushed, and the tests verify the code generation.

I am having a hard time implementing this correctly mainly due to my lack of familiarity with the project.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions