'faust Record does not deserialize avro Union type correctly

I'm using faust-streaming and python-schema-registry-client to serialize faust.Record classes to avro.

When I try to deserialize a Union of two complex types however faust cannot reconstruct the correct records and only presents a dictionary instead of a faust.Record class.

I have the following code and schema:

schema_union = schema.AvroSchema({
    'type': 'record',
    'name': 'UnionRecord',
    'fields': [
        {
            'name': 'entity',
            'type': [
                'null',
                {
                    'name': 'company',
                    'type': 'record',
                    'fields': [
                        {'name': 'name_company', 'type': 'string'},
                    ]
                },
                {
                    'name': 'person',
                    'type': 'record',
                    'fields': [
                        {'name': 'name_person', 'type': 'string'},
                    ]
                }
            ]
        }
    ]
})

client = SchemaRegistryClient(url='http://localhost:8081')
faust_serializer_union = FaustSerializer(client, 'schema-union', schema_union)

class Company(faust.Record):
    name_company: str
    
class Person(faust.Record):
    name_person: str

class UnionRecord(
    faust.Record,
    validation=True,
    serializer=faust_serializer_union
):
    entity: Optional[Union[Company, Person]]  # Union is typing.Union

record = UnionRecord(entity=Company(name_company='name'))

If I now try to serialize record and then deserialize it:

out = record.dumps()  # == b'\x00\x00\x00\x00\x13\x02\x08name'

UnionRecord.loads(out)

I get this:

<UnionRecord: entity={'name_company': 'name'}>

Whereas I expect to get this:

<UnionNoneRecord: entity=<Company: name_company='name'>>

If I remove the Union type and alter the schema so that I can have a faust.Record that has only this field: Optional[Company], I do get the correct deserialization.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source