mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 17:47:04 +03:00
Allow run_validators() to handle non-dict types. (#6365)
Fixes #6053. Original test case thanks to Vincent Delaitre in #6242.
This commit is contained in:
parent
0cf18c4163
commit
587058e3c2
|
@ -461,8 +461,11 @@ class Serializer(BaseSerializer):
|
|||
"""
|
||||
Add read_only fields with defaults to value before running validators.
|
||||
"""
|
||||
to_validate = self._read_only_defaults()
|
||||
to_validate.update(value)
|
||||
if isinstance(value, dict):
|
||||
to_validate = self._read_only_defaults()
|
||||
to_validate.update(value)
|
||||
else:
|
||||
to_validate = value
|
||||
super(Serializer, self).run_validators(to_validate)
|
||||
|
||||
def to_internal_value(self, data):
|
||||
|
|
|
@ -156,6 +156,33 @@ class TestSerializer:
|
|||
assert serializer.validated_data == {'char': 'abc', 'integer': 123}
|
||||
assert serializer.errors == {}
|
||||
|
||||
def test_custom_to_internal_value(self):
|
||||
"""
|
||||
to_internal_value() is expected to return a dict, but subclasses may
|
||||
return application specific type.
|
||||
"""
|
||||
class Point(object):
|
||||
def __init__(self, srid, x, y):
|
||||
self.srid = srid
|
||||
self.coords = (x, y)
|
||||
|
||||
# Declares a serializer that converts data into an object
|
||||
class NestedPointSerializer(serializers.Serializer):
|
||||
longitude = serializers.FloatField(source='x')
|
||||
latitude = serializers.FloatField(source='y')
|
||||
|
||||
def to_internal_value(self, data):
|
||||
kwargs = super(NestedPointSerializer, self).to_internal_value(data)
|
||||
return Point(srid=4326, **kwargs)
|
||||
|
||||
serializer = NestedPointSerializer(data={'longitude': 6.958307, 'latitude': 50.941357})
|
||||
assert serializer.is_valid()
|
||||
assert isinstance(serializer.validated_data, Point)
|
||||
assert serializer.validated_data.srid == 4326
|
||||
assert serializer.validated_data.coords[0] == 6.958307
|
||||
assert serializer.validated_data.coords[1] == 50.941357
|
||||
assert serializer.errors == {}
|
||||
|
||||
|
||||
class TestValidateMethod:
|
||||
def test_non_field_error_validate_method(self):
|
||||
|
|
Loading…
Reference in New Issue
Block a user