Skip to content

Commit a985fc5

Browse files
Merge pull request #3364 from linovia/bug/3361
Don't pass `allow_empty` to `ListSerializer`'s children.
2 parents b57e9cf + aa48182 commit a985fc5

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

rest_framework/serializers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,13 @@ def many_init(cls, *args, **kwargs):
113113
kwargs['child'] = cls()
114114
return CustomListSerializer(*args, **kwargs)
115115
"""
116+
allow_empty = kwargs.pop('allow_empty', None)
116117
child_serializer = cls(*args, **kwargs)
117-
list_kwargs = {'child': child_serializer}
118+
list_kwargs = {
119+
'child': child_serializer,
120+
}
121+
if allow_empty is not None:
122+
list_kwargs['allow_empty'] = allow_empty
118123
list_kwargs.update(dict([
119124
(key, value) for key, value in kwargs.items()
120125
if key in LIST_SERIALIZER_KWARGS

tests/test_serializer_nested.py

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,23 @@ class NestedSerializer(serializers.Serializer):
7979
class TestSerializer(serializers.Serializer):
8080
allow_null = NestedSerializer(many=True, allow_null=True)
8181
not_allow_null = NestedSerializer(many=True)
82+
allow_empty = NestedSerializer(many=True, allow_empty=True)
83+
not_allow_empty = NestedSerializer(many=True, allow_empty=False)
8284

8385
self.Serializer = TestSerializer
8486

8587
def test_null_allowed_if_allow_null_is_set(self):
8688
input_data = {
8789
'allow_null': None,
88-
'not_allow_null': [{'example': '2'}, {'example': '3'}]
90+
'not_allow_null': [{'example': '2'}, {'example': '3'}],
91+
'allow_empty': [{'example': '2'}],
92+
'not_allow_empty': [{'example': '2'}],
8993
}
9094
expected_data = {
9195
'allow_null': None,
92-
'not_allow_null': [{'example': 2}, {'example': 3}]
96+
'not_allow_null': [{'example': 2}, {'example': 3}],
97+
'allow_empty': [{'example': 2}],
98+
'not_allow_empty': [{'example': 2}],
9399
}
94100
serializer = self.Serializer(data=input_data)
95101

@@ -99,7 +105,9 @@ def test_null_allowed_if_allow_null_is_set(self):
99105
def test_null_is_not_allowed_if_allow_null_is_not_set(self):
100106
input_data = {
101107
'allow_null': None,
102-
'not_allow_null': None
108+
'not_allow_null': None,
109+
'allow_empty': [{'example': '2'}],
110+
'not_allow_empty': [{'example': '2'}],
103111
}
104112
serializer = self.Serializer(data=input_data)
105113

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

119127
input_data = {
120128
'allow_null': None,
121-
'not_allow_null': [{'example': 2}]
129+
'not_allow_null': [{'example': 2}],
130+
'allow_empty': [{'example': 2}],
131+
'not_allow_empty': [{'example': 2}],
122132
}
123133
serializer = TestSerializer(data=input_data)
124134

125135
assert serializer.is_valid()
126136
assert serializer.validated_data == input_data
127137
assert TestSerializer.validation_was_run
138+
139+
def test_empty_allowed_if_allow_empty_is_set(self):
140+
input_data = {
141+
'allow_null': [{'example': '2'}],
142+
'not_allow_null': [{'example': '2'}],
143+
'allow_empty': [],
144+
'not_allow_empty': [{'example': '2'}],
145+
}
146+
expected_data = {
147+
'allow_null': [{'example': 2}],
148+
'not_allow_null': [{'example': 2}],
149+
'allow_empty': [],
150+
'not_allow_empty': [{'example': 2}],
151+
}
152+
serializer = self.Serializer(data=input_data)
153+
154+
assert serializer.is_valid(), serializer.errors
155+
assert serializer.validated_data == expected_data
156+
157+
def test_empty_not_allowed_if_allow_empty_is_set_to_false(self):
158+
input_data = {
159+
'allow_null': [{'example': '2'}],
160+
'not_allow_null': [{'example': '2'}],
161+
'allow_empty': [],
162+
'not_allow_empty': [],
163+
}
164+
serializer = self.Serializer(data=input_data)
165+
166+
assert not serializer.is_valid()
167+
168+
expected_errors = {'not_allow_empty': {'non_field_errors': [serializers.ListSerializer.default_error_messages['empty']]}}
169+
assert serializer.errors == expected_errors

0 commit comments

Comments
 (0)