Closed joaoe closed 1 year ago
Hi @joaoe
Thansk for reporting this issue. I will fix it asap. Regarding unpacking enums I think it is not a good idea as I would like to have the same default behavior as pydantic
@marcosschroh Hi. Thanks for the fix, but unfortunately it is incomplete. Your patch fixes so that nested objects are traversed if they are AvroModels. Well, that still skips for other container types, like list. e.g.
import enum
from dataclasses_avroschema.avrodantic import AvroBaseModel
class MyEnum(enum.IntEnum):
x = 1
class ModelA(AvroBaseModel):
a: int = 1
d: MyEnum = MyEnum.x
class ModelB(AvroBaseModel):
b: ModelA = ModelA()
d: MyEnum = MyEnum.x
e: list[ModelA] = [ModelA()]
f: dict[str, ModelA] = {"g": ModelA()}
target = repr({'b': {'a': 1, 'd': 1}, 'd': 1, 'e': [{'a': 1, 'd': 1}], 'f': {'g': {'a': 1, 'd': 1}}})
res_asdict = repr(ModelB().asdict())
assert res_asdict == target, res_asdict
I tried the fix I suggested above in my local setup because it was still in schema_generator with AvroModel in scope and it worked for my project where I had lists of objects. But then I did a reduced testcase and I did not think of puting ModelA() inside nested lists.
@joaoe - good news - we have a fix for your problem: https://github.com/marcosschroh/dataclasses-avroschema/pull/346
Describe the bug Bug introduced here https://github.com/marcosschroh/dataclasses-avroschema/commit/9d107086944f41a16a0d3238fcb9a65760b0ed50
Deeply nested Avro Models no longer recursively convert to PODs when calling
AvroModel.asdict()
To Reproduce
This shall pass.
asdict()
call fails to traverse to nested models, but at least unpacks the enum at the top level.dict()
call works from before and follows nested models, but it fails to convert the enum already before this regression was introduced.asdict()
internally calleddict()+AvroModel.standardize_custom_type()
which both converted nested objects and unpacked enums. I need that for my project :) thanks!Expected behavior Here https://github.com/marcosschroh/dataclasses-avroschema/blob/5e26fb3315351fafd6b368b759739f6ef8e59119/dataclasses_avroschema/schema_generator.py#L162
That function misses a check for
PS: could perhaps
dict()
unpack enums as well, please ? But as far as I see, that call is 100% in pydantic, so either you'd need to wrap it or find a solution inside pydantic. Seeuse_enum_values
in https://docs.pydantic.dev/latest/usage/model_config/