Skip to content

Commit e679a85

Browse files
committed
Fixed encode#2761 - ListField truncation on HTTP PATCH
- Checked ``partial`` state when getting value in appropriate field classes; return ``empty`` immediately if key not submitted.
1 parent bf4d11a commit e679a85

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

rest_framework/fields.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,14 +1262,13 @@ def __init__(self, *args, **kwargs):
12621262
super(MultipleChoiceField, self).__init__(*args, **kwargs)
12631263

12641264
def get_value(self, dictionary):
1265+
if self.field_name not in dictionary:
1266+
if getattr(self.root, 'partial', False):
1267+
return empty
12651268
# We override the default field access in order to support
12661269
# lists in HTML forms.
12671270
if html.is_html_input(dictionary):
1268-
ret = dictionary.getlist(self.field_name)
1269-
if getattr(self.root, 'partial', False) and not ret:
1270-
ret = empty
1271-
return ret
1272-
1271+
return dictionary.getlist(self.field_name)
12731272
return dictionary.get(self.field_name, empty)
12741273

12751274
def to_internal_value(self, data):
@@ -1416,6 +1415,9 @@ def __init__(self, *args, **kwargs):
14161415
self.child.bind(field_name='', parent=self)
14171416

14181417
def get_value(self, dictionary):
1418+
if self.field_name not in dictionary:
1419+
if getattr(self.root, 'partial', False):
1420+
return empty
14191421
# We override the default field access in order to support
14201422
# lists in HTML forms.
14211423
if html.is_html_input(dictionary):

tests/test_serializer_lists.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ class ListSerializer(serializers.Serializer):
304304
listdata = serializers.ListField()
305305
serializer = ListSerializer(data=MultiValueDict(), partial=True)
306306
result = serializer.to_internal_value(data={})
307-
assert result == {}
307+
assert "listdata" not in result
308308
assert serializer.is_valid()
309309
assert serializer.validated_data == {}
310310
assert serializer.errors == {}
@@ -314,7 +314,7 @@ class MultipleChoiceSerializer(serializers.Serializer):
314314
multiplechoice = serializers.MultipleChoiceField(choices=[1, 2, 3])
315315
serializer = MultipleChoiceSerializer(data=MultiValueDict(), partial=True)
316316
result = serializer.to_internal_value(data={})
317-
assert result == {}
317+
assert "multiplechoice" not in result
318318
assert serializer.is_valid()
319319
assert serializer.validated_data == {}
320320
assert serializer.errors == {}

0 commit comments

Comments
 (0)