Skip to content

Commit

Permalink
Allow dataclasses mapped to foreign serializer
Browse files Browse the repository at this point in the history
Previously, when dataclass types were present in the serializer field
mapping, they were required to map to a DataclassSerializer type,
because the serializer would always receive the `dataclass` argument.
Remove this restriction.

Fixes #46.
  • Loading branch information
oxan committed Oct 23, 2021
1 parent fb0cd13 commit bdf66a2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions rest_framework_dataclasses/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,10 @@ def build_dataclass_field(self, field_name: str, type_info: TypeInfo) -> Seriali
except KeyError:
field_class = self.serializer_dataclass_field

# allow user to map a dataclass to a regular serializer Field class
if not issubclass(field_class, DataclassSerializer):
return self.build_standard_field(field_name, type_info)

field_kwargs = {'dataclass': type_info.base_type,
'many': type_info.is_many}

Expand Down
4 changes: 4 additions & 0 deletions tests/test_field_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ def test_nested(self):
DataclassSerializer.serializer_field_mapping[refclass] = subclassed_serializer
self.check_field(refclass, subclassed_serializer, {'dataclass': refclass, 'many': False})

# customizing the dataclass serializer by putting regular Field in the field mapping
DataclassSerializer.serializer_field_mapping[refclass] = fields.CharField
self.check_field(refclass, fields.CharField, {})

def test_relational(self):
django.setup()

Expand Down

0 comments on commit bdf66a2

Please sign in to comment.