Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

restore the behavior of parameter many in the serializers #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions rest_marshmallow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from marshmallow import Schema as MarshmallowSchema
from marshmallow import fields # noqa
from marshmallow.exceptions import ValidationError as MarshmallowValidationError
from rest_framework.serializers import BaseSerializer, ValidationError
from rest_framework.serializers import BaseSerializer, ValidationError, ListSerializer
from types import MethodType


IS_MARSHMALLOW_LT_3 = int(marshmallow.__version__.split('.')[0]) < 3
Expand All @@ -15,13 +16,19 @@
)


def _dump(self, obj, *args, many=None, **kwargs):
return [self.child.dump(o) for o in obj]


class Schema(BaseSerializer, MarshmallowSchema):

def __new__(cls, *args, **kwargs):
# We're overriding the DRF implementation here, because ListSerializer
# clashes with Nested implementation.
kwargs.pop('many', False)
return super(Schema, cls).__new__(cls, *args, **kwargs)
result = super(Schema, cls).__new__(cls, *args, **kwargs)
if isinstance(result, ListSerializer):
result.dump = MethodType(_dump, result)
return result

def __init__(self, *args, **kwargs):
schema_kwargs = {
Expand Down
36 changes: 36 additions & 0 deletions tests/test_marshmallow.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ def test_deserialize():
assert serializer.validated_data == {'number': 123, 'text': 'abc'}


def test_deserialize_many():
data = [
{'number': 123, 'text': 'abc'},
{'number': 123, 'text': 'abc'},
{'number': 123, 'text': 'abc'},
]
serializer = ExampleSerializer(data=data, many=True)
assert serializer.is_valid()
assert serializer.validated_data == [
{'number': 123, 'text': 'abc'},
{'number': 123, 'text': 'abc'},
{'number': 123, 'text': 'abc'},
]


def test_deserialize_validation_failed():
data = {'number': 'abc', 'text': 'abc'}
serializer = ExampleSerializer(data=data)
Expand All @@ -105,6 +120,27 @@ def test_create():
assert serializer.data == {'number': 123, 'text': 'abc'}


def test_create_many():
data = [
{'number': 123, 'text': 'abc'},
{'number': 123, 'text': 'abc'},
{'number': 123, 'text': 'abc'},
]
serializer = ExampleSerializer(data=data, many=True)
assert serializer.is_valid()
instances = serializer.save()
assert instances
for instance in instances:
assert isinstance(instance, Object)
assert instance.number == 123
assert instance.text == 'abc'
assert serializer.data == [
{'number': 123, 'text': 'abc'},
{'number': 123, 'text': 'abc'},
{'number': 123, 'text': 'abc'},
]


def test_update():
instance = Object(number=123, text='abc')
data = {'number': 456, 'text': 'def'}
Expand Down