Merge pull request #3112 from juannyG/master

Fix nested serializer required=False multipart data validation bug
This commit is contained in:
Xavier Ordoquy 2015-07-08 15:15:14 +02:00
commit f33ca01511
2 changed files with 32 additions and 1 deletions

View File

@ -367,7 +367,7 @@ class Serializer(BaseSerializer):
# We override the default field access in order to support # We override the default field access in order to support
# nested HTML forms. # nested HTML forms.
if html.is_html_input(dictionary): 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) return dictionary.get(self.field_name, empty)
def run_validation(self, data=empty): def run_validation(self, data=empty):

View File

@ -1,3 +1,5 @@
from django.http import QueryDict
from rest_framework import serializers from rest_framework import serializers
@ -38,3 +40,32 @@ class TestNestedSerializer:
} }
serializer = self.Serializer() serializer = self.Serializer()
assert serializer.data == expected_data 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()