marcosschroh / dataclasses-avroschema

Generate avro schemas from python dataclasses, Pydantic models and Faust Records. Code generation from avro schemas. Serialize/Deserialize python instances with avro schemas.
https://marcosschroh.github.io/dataclasses-avroschema/
MIT License
219 stars 67 forks source link

feat(AvroBaseModel): non-deprecated pydantic v2 custom fields #784

Closed fajpunk closed 1 month ago

fajpunk commented 1 month ago

Add a new way to generate Avro schema and serialize custom fields for Pydantic v2 AvroBaseModels. This new strategy provides a CustomAvroEncoder class to use as an annotation on fields with types that don't natively support serialization to Avro. The existing Pydantic v2 custom serialization/schema-generation strategy depends on the json_encoders model config, which is deprecated.

My only usecase for this is so I can serialize timedeltas to float seconds in my models without having to depend on deprecated Pydantic functionality (and without having to write a custom __get_pydantic_core_schema__, because this is natively supported by Pydantic). If there's a better way to do this that I missed, please let me know (and thank you for your time)!

fajpunk commented 1 month ago

Some typing issues and doesn't work with unions yet.