1

我正在使用faust-streaming并将类python-schema-registry-client序列化为faust.Recordavro。

当我尝试反序列化两个复杂类型的 Union 时,faust 无法重建正确的记录,并且只提供字典而不是faust.Record类。

我有以下代码和架构:

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

如果我现在尝试序列化record然后反序列化它:

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

UnionRecord.loads(out)

我明白了:

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

而我希望得到这个:

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

如果我删除 Union 类型并更改架构,以便我可以拥有一个faust.Record只有这个字段的:Optional[Company],我确实得到了正确的反序列化。

4

0 回答 0