mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-12-02 06:24:05 +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
|
# 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):
|
||||||
|
|
|
@ -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()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user