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

exclude_default is ignored on Pydantic model from the second call #799

Open zhavir opened 6 days ago

zhavir commented 6 days ago

Describe the bug If you try to get avro schema from a pydantic model multiple times it starts ignoring the exclude_default feature

To Reproduce

import uuid
from dataclasses_avroschema.pydantic import AvroBaseModel
from pydantic import Field

class Test(AvroBaseModel):
    id: str = Field(  # type: ignore[call-arg]
        default_factory=lambda: str(uuid.uuid4()), metadata={"exclude_default": True}
    )

a = Test()
print(a.avro_schema())  # it's works as expected
print(a.avro_schema())  # it's not works as expected and the defaults are included inside the schema

as a workaround I've overridden the method avro_schema by putting the cache decorator

    @classmethod
    @cache
    def avro_schema(cls, case_type: Any = None, **kwargs: Any) -> str:
        return super().avro_schema(case_type=case_type, **kwargs)

Expected behavior It should always exclude the default

marcosschroh commented 4 days ago

I will take a look. Thanks for reporting it