-
-
Notifications
You must be signed in to change notification settings - Fork 67
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
Version 0.63.2 introduced serialization problems #763
Comments
I can confirm that it's also the case for Pydantic: To Reproduce from typing import Optional
from dataclasses_avroschema.pydantic import AvroBaseModel
from pydantic import Field
class A(AvroBaseModel):
x: str = Field(...)
class B(AvroBaseModel):
a: Optional[A] = Field(...)
class C(AvroBaseModel):
b: Optional[B] = Field(...)
event = C(b=B(a=A(x="hello world")))
print(event.serialize()) Investigation It seems to be a problem with the double optional (in this particular case, but my guess would be that it's for every nested union). This case goes wrong in the def _validate_record(datum, schema, named_schemas, parent_ns, raise_errors, options):
"""
Check that the data is a Mapping type with all schema defined fields
validated as True.
"""
_, fullname = schema_name(schema, parent_ns)
return (
isinstance(datum, Mapping)
and not ("-type" in datum and datum["-type"] != fullname)
and all(
_validate(
datum=datum.get(f["name"], f.get("default", NoValue)),
schema=f["type"],
named_schemas=named_schemas,
field=f"{fullname}.{f['name']}",
raise_errors=raise_errors,
options=options,
)
for f in schema["fields"]
)
) With the pydantic classes above, the fullname variable is: Extra: Not sure if this helps, but what's interesting is that the In the following example: from typing import Optional
from dataclasses_avroschema.pydantic import AvroBaseModel
from pydantic import Field
class A(AvroBaseModel):
x: str = Field(...)
class B(AvroBaseModel):
a: Optional[A] = Field(...)
class C(AvroBaseModel):
b: Optional[B] = Field(...)
class D(AvroBaseModel):
c: C = Field(...)
class E(AvroBaseModel):
d: D = Field(...)
class F(AvroBaseModel):
e: E = Field(...)
event = F(e=E(d=D(c=C(b=B(a=A(x="hello world"))))))
print(event.serialize()) Interesting this is that if I inspect the fullname variable in _validate_record, it is class F(AvroBaseModel):
e: Optional[E] = Field(...) # note this one is optional now Then the fullname variable is |
Hallo, checking it. |
I have created this issue on |
I have made some refactor to support better serialization for nested unions, however still I am not able to make it work with from typing import Optional
from dataclasses_avroschema.pydantic import AvroBaseModel
from pydantic import Field
class A(AvroBaseModel):
x: str = Field(...)
class Meta:
namespace = "a_namescpace" # NAMESPACE ADDED
class B(AvroBaseModel):
a: Optional[A] = Field(...)
class C(AvroBaseModel):
b: Optional[B] = Field(...)
event = C(b=B(a=A(x="hello world")))
print(event.serialize())
# >>> b'\x00\x00\x16hello world' If I inspected the from fastavro import parse_schema
parsed_schema = parse_schema(C.avro_schema_to_python())
print(parsed_schema["__named_schemas"].keys())
# >>> dict_keys(['C', 'B', 'a_namescpace.A'])
print(event.asdict())
# >>> {'b': {'a': {'x': 'hello world', '-type': 'a_namescpace.A'}, '-type': 'B'}} If we remove the Any thoughts? |
Describe the bug
Changes in version 0.63.2 introduced a serialization problem.
To Reproduce
Expected behavior
Not erroring.
The text was updated successfully, but these errors were encountered: