mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
allow a non-required nested serializer to validate correctly when receiving multipart data
This commit is contained in:
parent
c091addb83
commit
b0c5f9db2f
|
@ -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):
|
||||
|
|
|
@ -38,3 +38,46 @@ 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
|
||||
|
||||
class FakeMultiDict(dict):
|
||||
"""
|
||||
Use this to fake a `format="multipart"` request, because
|
||||
`utils.is_html_input()` returns `True` when the dict object has
|
||||
an attribute of "getlist".
|
||||
"""
|
||||
def getlist(self, value, default=None):
|
||||
if value in self:
|
||||
return [self[value]]
|
||||
else:
|
||||
return [] if default is None else default
|
||||
|
||||
self.FakeMultiDict = FakeMultiDict
|
||||
|
||||
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 = self.FakeMultiDict()
|
||||
serializer = self.Serializer(data=input_data)
|
||||
assert serializer.is_valid()
|
||||
|
||||
input_data = self.FakeMultiDict(**{'nested.one': '1'})
|
||||
serializer = self.Serializer(data=input_data)
|
||||
assert serializer.is_valid()
|
||||
|
|
Loading…
Reference in New Issue
Block a user