Skip to content

Fail to deserialize generated unions #73

@oringnes

Description

@oringnes

Hi,

When working with a union of records, I am unable to deserialize record using apache_avro.
Example schema Foo.avsc:

{
  "type": "record",
  "name": "Foo",
  "fields": [
    {"name": "a", "type": "int"},
    {"name": "b",
      "type":
      [
        {
          "type": "record",
          "name": "Bar",
          "fields": [
            {"name": "c", "type": "long"}
          ]
        },
        {
          "type": "record",
          "name": "Baz",
          "fields": [
            {"name": "d", "type": "int"}
          ]
        }
      ]
    }
  ]
}

Expecting struct to be identical when serialized, and then deserialized:

    #[test]
    fn test_foo_serde() {
        let before = Foo {
            a: 0,
            b: UnionBarBaz::Baz(Baz {
                d: 42,
            })
        };

        let schema = Schema::parse_str(include_str!("../avsc/Foo.avsc")).unwrap();

        let mut writer = Writer::new(&schema, Vec::new());
        writer.append_ser(before.clone()).unwrap();
        let encoded = writer.into_inner().unwrap();

        let mut reader = Reader::with_schema(&schema, &encoded[..]).unwrap();
        let value = reader.next().unwrap();
        let after: Foo = from_value::<Foo>(&value.unwrap()).unwrap();

        assert_eq!(before, after);
    }

The call to from_value() is responding with the following error:

Failed to deserialize Avro value into value: Expected a Record|Enum, but got Union(1, Record([("d", Int(42))]))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions