Skip to content

Commit

Permalink
fix: make sure that metadata is not deleted when schemas are calculat…
Browse files Browse the repository at this point in the history
…ed more than once. Closes #799
  • Loading branch information
marcosschroh committed Dec 6, 2024
1 parent 90e487b commit e3fa811
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 10 additions & 3 deletions dataclasses_avroschema/fields/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,17 @@ class Field:
metadata: typing.Dict = dataclasses.field(default_factory=dict)
model_metadata: utils.SchemaMetadata = dataclasses.field(default_factory=utils.SchemaMetadata)
extra_default_types_allowed: typing.Tuple = dataclasses.field(default_factory=tuple)
# This is the metadata that the end user has defined in the dataclasses.Field or pydantic.Field
metadata_to_exclude: typing.List[str] = dataclasses.field(
default_factory=lambda: [
"exclude_default",
"inner_name",
]
)

def __post_init__(self) -> None:
self.exclude_default = self.metadata.pop("exclude_default", False) # type: ignore
self.inner_name = self.metadata.pop("inner_name", None) # type: ignore
self.exclude_default = self.metadata.get("exclude_default", False) # type: ignore
self.inner_name = self.metadata.get("inner_name", None) # type: ignore

@property
def avro_type(self) -> typing.Union[str, typing.Dict]: # type: ignore
Expand All @@ -53,7 +60,7 @@ def get_singular_name(name: str) -> str:
return inflection.singularize(name)

def get_metadata(self) -> typing.List[typing.Tuple[str, str]]:
return [(name, value) for name, value in self.metadata.items()]
return [(name, value) for name, value in self.metadata.items() if name not in self.metadata_to_exclude]

def render(self) -> OrderedDict:
"""
Expand Down
3 changes: 3 additions & 0 deletions tests/schemas/pydantic/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class Meta:
schema_doc = False

assert User.avro_schema() == json.dumps(user_avro_json)
# We execute again the test to make sure that that the result is the same
# related to https://github.com/marcosschroh/dataclasses-avroschema/issues/799
assert User.avro_schema() == json.dumps(user_avro_json)


def test_pydantic_record_schema_with_metadata():
Expand Down

0 comments on commit e3fa811

Please sign in to comment.