Skip to content

Commit

Permalink
fix: generate proper default values when reusing Enums types (#796)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosschroh authored Nov 13, 2024
1 parent 081d897 commit 78eab46
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dataclasses_avroschema/model_generator/lang/python/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ def get_field_default(self) -> str:
python_type = func(self.default)
template_func = avro_to_python_utils.LOGICAL_TYPE_TEMPLATES[self.avro_type]
default_repr = template_func(python_type)
elif self.avro_type == field_utils.NULL:
default_repr = "None"
elif self.avro_type == self.type_hint:
# this is when a Custom type is been reused which is probably an Enum
default_repr = f"{casefy.pascalcase(self.type_hint)}.{casefy.uppercase(self.default)}"
else:
default_repr = str(self.default)

Expand Down
5 changes: 5 additions & 0 deletions tests/model_generator/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ def schema_with_enum_types() -> Dict:
],
"default": "MIN_LIMIT",
},
{
"name": "another_limit_type",
"type": "LimitTypes",
"default": "EXACT_LIMIT",
},
],
}

Expand Down
1 change: 1 addition & 0 deletions tests/model_generator/test_model_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ class User(AvroModel):
superheros: Superheros = Superheros.BATMAN
cars: typing.Optional[Cars] = None
limit_type: typing.Optional[LimitTypes] = LimitTypes.MIN_LIMIT
another_limit_type: LimitTypes = LimitTypes.EXACT_LIMIT
"""
model_generator = ModelGenerator()
result = model_generator.render(schema=schema_with_enum_types)
Expand Down

0 comments on commit 78eab46

Please sign in to comment.