mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 12:30:11 +03:00
Merge 29edb72659
into 836e49b535
This commit is contained in:
commit
6854e3d63e
|
@ -101,7 +101,10 @@ def get_attribute(instance, attrs):
|
||||||
if isinstance(instance, collections.Mapping):
|
if isinstance(instance, collections.Mapping):
|
||||||
instance = instance[attr]
|
instance = instance[attr]
|
||||||
else:
|
else:
|
||||||
instance = getattr(instance, attr)
|
try:
|
||||||
|
instance = getattr(instance, attr)
|
||||||
|
except KeyError as exc:
|
||||||
|
raise ValueError('Exception raised in property attribute "{0}"; original exception was: {1}'.format(attr, exc))
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
return None
|
return None
|
||||||
if is_simple_callable(instance):
|
if is_simple_callable(instance):
|
||||||
|
|
|
@ -344,6 +344,23 @@ class TestUnicodeRepr:
|
||||||
|
|
||||||
|
|
||||||
class TestNotRequiredOutput:
|
class TestNotRequiredOutput:
|
||||||
|
def test_not_required_property_exception(self):
|
||||||
|
"""
|
||||||
|
A KeyError should propogate when it is thrown in a property attribute.
|
||||||
|
"""
|
||||||
|
class ExampleSerializer(serializers.Serializer):
|
||||||
|
raises_key_error = serializers.CharField(required=False)
|
||||||
|
|
||||||
|
class ExampleObject:
|
||||||
|
@property
|
||||||
|
def raises_key_error(self):
|
||||||
|
raise KeyError()
|
||||||
|
|
||||||
|
instance = ExampleObject()
|
||||||
|
serializer = ExampleSerializer(instance)
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
serializer.data
|
||||||
|
|
||||||
def test_not_required_output_for_dict(self):
|
def test_not_required_output_for_dict(self):
|
||||||
"""
|
"""
|
||||||
'required=False' should allow a dictionary key to be missing in output.
|
'required=False' should allow a dictionary key to be missing in output.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user