Skip to content
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

Pydantic excluded fields are being exported to schema and fail serialization #314

Closed
michal-rogala opened this issue May 8, 2023 · 5 comments

Comments

@michal-rogala
Copy link
Contributor

michal-rogala commented May 8, 2023

Hello!

When using pydantic (AvroBaseModel) - if a field is marked as excluded, it's still being exported to schema and serialization raises an error. Pydantic internal fields (beginning with underscore) are being ignored but those marked as excluded are not.

Current behavior:

from dataclasses_avroschema.avrodantic import AvroBaseModel
from pydantic import Field

class Message(AvroBaseModel):
    internal_field: str = Field(exclude=True)
    public_field: str

print(Message.avro_schema())

this results in schema:
{"type": "record", "name": "Message", "fields": [{"name": "internal_field", "type": "string"}, {"name": "public_field", "type": "string"}]}

Object serialization:

m = Message(internal_field="internal", public_field="public")
print(m.serialize(serialization_type="avro-json"))

raises an error:

fastavro/_write_py.py", line 252, in write_record
    raise ValueError(f"no value and no default for {name}")
ValueError: no value and no default for internal_field

Which of course make sense as this value is not provided by pydantic - because it's excluded.

Expected behavior
Excluded message is not reflected in schema and serialization.

@michal-rogala michal-rogala changed the title Pydantic internal are being exported and result in error Pydantic internal fields are being exported to schema and fail serialization May 8, 2023
@michal-rogala michal-rogala changed the title Pydantic internal fields are being exported to schema and fail serialization Pydantic excluded fields are being exported to schema and fail serialization May 8, 2023
@marcosschroh
Copy link
Owner

Hi @michal-rogala

Thanks for reporting the bug. I will try to solve it asap

@michal-rogala
Copy link
Contributor Author

perfect, thank you :)

@marcosschroh
Copy link
Owner

I have released a new version with the fix. Here is the documentation

@michal-rogala
Copy link
Contributor Author

thank you! :)

@joaoe
Copy link

joaoe commented May 22, 2023

This fix introduced a regression
#324

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants