aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Christie2015-02-27 09:13:01 +0000
committerTom Christie2015-02-27 09:13:01 +0000
commitcda74b59971f85da873b0e15da8b4afb4411a026 (patch)
tree8981ad8adf5b2f16e6771be3790952bd51ab61fc
parentc66f23391ac669f2a9a27431e66588e9092300f2 (diff)
parente6b06c34c1ee526b65c92b9071c47be2ddc668c4 (diff)
downloaddjango-rest-framework-cda74b59971f85da873b0e15da8b4afb4411a026.tar.bz2
Merge pull request #2608 from ezheidtmann/dont-swallow-errors-in-callable-sources
Dont swallow errors in callable sources
-rw-r--r--rest_framework/fields.py9
-rw-r--r--tests/test_fields.py25
2 files changed, 33 insertions, 1 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py
index 13ea6dde..c0f93816 100644
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -71,7 +71,14 @@ def get_attribute(instance, attrs):
except ObjectDoesNotExist:
return None
if is_simple_callable(instance):
- instance = instance()
+ try:
+ instance = instance()
+ except (AttributeError, KeyError) as exc:
+ # If we raised an Attribute or KeyError here it'd get treated
+ # as an omitted field in `Field.get_attribute()`. Instead we
+ # raise a ValueError to ensure the exception is not masked.
+ raise ValueError('Exception raised in callable attribute "{0}"; original exception was: {1}'.format(attr, exc))
+
return instance
diff --git a/tests/test_fields.py b/tests/test_fields.py
index ab3418bd..7f5f8102 100644
--- a/tests/test_fields.py
+++ b/tests/test_fields.py
@@ -93,6 +93,31 @@ class TestSource:
"same as the field name. Remove the `source` keyword argument."
)
+ def test_callable_source(self):
+ class ExampleSerializer(serializers.Serializer):
+ example_field = serializers.CharField(source='example_callable')
+
+ class ExampleInstance(object):
+ def example_callable(self):
+ return 'example callable value'
+
+ serializer = ExampleSerializer(ExampleInstance())
+ assert serializer.data['example_field'] == 'example callable value'
+
+ def test_callable_source_raises(self):
+ class ExampleSerializer(serializers.Serializer):
+ example_field = serializers.CharField(source='example_callable', read_only=True)
+
+ class ExampleInstance(object):
+ def example_callable(self):
+ raise AttributeError('method call failed')
+
+ with pytest.raises(ValueError) as exc_info:
+ serializer = ExampleSerializer(ExampleInstance())
+ serializer.data.items()
+
+ assert 'method call failed' in str(exc_info.value)
+
class TestReadOnly:
def setup(self):