Skip to content

SchemaNotFoundInUnion with simple serialization #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tkubicz opened this issue Nov 17, 2020 · 1 comment · Fixed by #7
Closed

SchemaNotFoundInUnion with simple serialization #6

tkubicz opened this issue Nov 17, 2020 · 1 comment · Fixed by #7

Comments

@tkubicz
Copy link

tkubicz commented Nov 17, 2020

Hello,

I've encountered an issue while trying to serialize a simple struct with union type into avro recrod.

{
    "name": "Reference",
    "type": "record",
    "fields": [
        {
            "name": "feedReference",
            "type": [
                "null",
                {
                    "name": "FeedReference",
                    "type": "record",
                    "fields": [
                        {
                            "name": "instance",
                            "type": "string"
                        },
                        {
                            "name": "provider",
                            "type": "string"
                        }
                    ]
                }
            ],
            "default": null
        }
    ]
}

Simple rust code to reproduce the issue:

#[derive(Serialize, Deserialize)]
    struct Reference {
        #[serde(rename = "feedReference")]
        pub feed_reference: Option<FeedReference>,
    }

    #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
    pub struct FeedReference {
        pub instance: String,
        pub provider: String,
    }

    impl Default for FeedReference {
        fn default() -> FeedReference {
            FeedReference {
                instance: String::default(),
                provider: String::default(),
            }
        }
    }

    #[test]
    fn validate_schema_avrow() {
        let reference = Reference {
            feed_reference: Some(FeedReference::default()),
        };

        let schema_str = std::fs::read_to_string("avro/feed_reference.avsc").unwrap();
        let schema = Schema::from_str(&schema_str).unwrap();
        let mut writer = Writer::new(&schema, vec![]).unwrap();
        writer.serialize(reference).unwrap();
    }

And the result:

thread 'test::validate_schema_avrow' panicked at 'called `Result::unwrap()` on an `Err` value: SchemaNotFoundInUnion', crates/platform_content/src/lib.rs:36:37
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test test::validate_schema_avrow ... FAILED

The funny thing is, that when I pass None into the feed_reference field, struct seems to serialize correctly.
The same schema and struct serializes just fine using e.g avro-rs.

@creativcoder
Copy link
Owner

creativcoder commented Nov 22, 2020

Hi @tkubicz

Thanks for reporting this!

Looks like the union schema resolution method, did not account for implicitly defined named schemas.
This has been fixed with #7. Will do a point release post merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants