mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 11:33:59 +03:00
Merge pull request #3112 from juannyG/master
Fix nested serializer required=False multipart data validation bug
This commit is contained in:
commit
f33ca01511
|
@ -367,7 +367,7 @@ class Serializer(BaseSerializer):
|
|||
# We override the default field access in order to support
|
||||
# nested HTML forms.
|
||||
if html.is_html_input(dictionary):
|
||||
return html.parse_html_dict(dictionary, prefix=self.field_name)
|
||||
return html.parse_html_dict(dictionary, prefix=self.field_name) or empty
|
||||
return dictionary.get(self.field_name, empty)
|
||||
|
||||
def run_validation(self, data=empty):
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
from django.http import QueryDict
|
||||
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
|
@ -38,3 +40,32 @@ class TestNestedSerializer:
|
|||
}
|
||||
serializer = self.Serializer()
|
||||
assert serializer.data == expected_data
|
||||
|
||||
|
||||
class TestNotRequiredNestedSerializer:
|
||||
def setup(self):
|
||||
class NestedSerializer(serializers.Serializer):
|
||||
one = serializers.IntegerField(max_value=10)
|
||||
|
||||
class TestSerializer(serializers.Serializer):
|
||||
nested = NestedSerializer(required=False)
|
||||
|
||||
self.Serializer = TestSerializer
|
||||
|
||||
def test_json_validate(self):
|
||||
input_data = {}
|
||||
serializer = self.Serializer(data=input_data)
|
||||
assert serializer.is_valid()
|
||||
|
||||
input_data = {'nested': {'one': '1'}}
|
||||
serializer = self.Serializer(data=input_data)
|
||||
assert serializer.is_valid()
|
||||
|
||||
def test_multipart_validate(self):
|
||||
input_data = QueryDict('')
|
||||
serializer = self.Serializer(data=input_data)
|
||||
assert serializer.is_valid()
|
||||
|
||||
input_data = QueryDict('nested[one]=1')
|
||||
serializer = self.Serializer(data=input_data)
|
||||
assert serializer.is_valid()
|
||||
|
|
Loading…
Reference in New Issue
Block a user