Skip to content

Don't pass allow_empty to ListSerializer's children. #3364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 9, 2015
Merged
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
7 changes: 6 additions & 1 deletion rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,13 @@ def many_init(cls, *args, **kwargs):
kwargs['child'] = cls()
return CustomListSerializer(*args, **kwargs)
"""
allow_empty = kwargs.pop('allow_empty', None)
child_serializer = cls(*args, **kwargs)
list_kwargs = {'child': child_serializer}
list_kwargs = {
'child': child_serializer,
}
if allow_empty is not None:
list_kwargs['allow_empty'] = allow_empty
list_kwargs.update(dict([
(key, value) for key, value in kwargs.items()
if key in LIST_SERIALIZER_KWARGS
Expand Down
50 changes: 46 additions & 4 deletions tests/test_serializer_nested.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,23 @@ class NestedSerializer(serializers.Serializer):
class TestSerializer(serializers.Serializer):
allow_null = NestedSerializer(many=True, allow_null=True)
not_allow_null = NestedSerializer(many=True)
allow_empty = NestedSerializer(many=True, allow_empty=True)
not_allow_empty = NestedSerializer(many=True, allow_empty=False)

self.Serializer = TestSerializer

def test_null_allowed_if_allow_null_is_set(self):
input_data = {
'allow_null': None,
'not_allow_null': [{'example': '2'}, {'example': '3'}]
'not_allow_null': [{'example': '2'}, {'example': '3'}],
'allow_empty': [{'example': '2'}],
'not_allow_empty': [{'example': '2'}],
}
expected_data = {
'allow_null': None,
'not_allow_null': [{'example': 2}, {'example': 3}]
'not_allow_null': [{'example': 2}, {'example': 3}],
'allow_empty': [{'example': 2}],
'not_allow_empty': [{'example': 2}],
}
serializer = self.Serializer(data=input_data)

Expand All @@ -99,7 +105,9 @@ def test_null_allowed_if_allow_null_is_set(self):
def test_null_is_not_allowed_if_allow_null_is_not_set(self):
input_data = {
'allow_null': None,
'not_allow_null': None
'not_allow_null': None,
'allow_empty': [{'example': '2'}],
'not_allow_empty': [{'example': '2'}],
}
serializer = self.Serializer(data=input_data)

Expand All @@ -118,10 +126,44 @@ def validate_allow_null(self, value):

input_data = {
'allow_null': None,
'not_allow_null': [{'example': 2}]
'not_allow_null': [{'example': 2}],
'allow_empty': [{'example': 2}],
'not_allow_empty': [{'example': 2}],
}
serializer = TestSerializer(data=input_data)

assert serializer.is_valid()
assert serializer.validated_data == input_data
assert TestSerializer.validation_was_run

def test_empty_allowed_if_allow_empty_is_set(self):
input_data = {
'allow_null': [{'example': '2'}],
'not_allow_null': [{'example': '2'}],
'allow_empty': [],
'not_allow_empty': [{'example': '2'}],
}
expected_data = {
'allow_null': [{'example': 2}],
'not_allow_null': [{'example': 2}],
'allow_empty': [],
'not_allow_empty': [{'example': 2}],
}
serializer = self.Serializer(data=input_data)

assert serializer.is_valid(), serializer.errors
assert serializer.validated_data == expected_data

def test_empty_not_allowed_if_allow_empty_is_set_to_false(self):
input_data = {
'allow_null': [{'example': '2'}],
'not_allow_null': [{'example': '2'}],
'allow_empty': [],
'not_allow_empty': [],
}
serializer = self.Serializer(data=input_data)

assert not serializer.is_valid()

expected_errors = {'not_allow_empty': {'non_field_errors': [serializers.ListSerializer.default_error_messages['empty']]}}
assert serializer.errors == expected_errors
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy