Skip to content

Commit

Permalink
Fixes #3804, avoiding update of Meta.extra_kwargs
Browse files Browse the repository at this point in the history
Added unit test to cover.
  • Loading branch information
Kevin Massey committed Jan 6, 2016
1 parent c46ed66 commit 7cd3933
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ def get_extra_kwargs(self):
Return a dictionary mapping field names to a dictionary of
additional keyword arguments.
"""
extra_kwargs = getattr(self.Meta, 'extra_kwargs', {})
extra_kwargs = copy.deepcopy(getattr(self.Meta, 'extra_kwargs', {}))

read_only_fields = getattr(self.Meta, 'read_only_fields', None)
if read_only_fields is not None:
Expand Down
31 changes: 31 additions & 0 deletions tests/test_model_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,3 +909,34 @@ class Meta:
serializer = TestSerializer()

assert serializer.fields['decimal_field'].max_value == 3


class TestMetaInheritance(TestCase):
def test_extra_kwargs_not_altered(self):
class TestSerializer(serializers.ModelSerializer):
non_model_field = serializers.CharField()

class Meta:
model = OneFieldModel
read_only_fields = ('char_field', 'non_model_field')
fields = read_only_fields
extra_kwargs = {}

class ChildSerializer(TestSerializer):
class Meta(TestSerializer.Meta):
read_only_fields = ()

test_expected = dedent("""
TestSerializer():
char_field = CharField(read_only=True)
non_model_field = CharField()
""")

child_expected = dedent("""
ChildSerializer():
char_field = CharField(max_length=100)
non_model_field = CharField()
""")
self.assertEqual(unicode_repr(ChildSerializer()), child_expected)
self.assertEqual(unicode_repr(TestSerializer()), test_expected)
self.assertEqual(unicode_repr(ChildSerializer()), child_expected)

0 comments on commit 7cd3933

Please sign in to comment.