From 16ffe5e31f80058389139fe5dae5184cc22319a6 Mon Sep 17 00:00:00 2001 From: Evan Heidtmann Date: Thu, 26 Feb 2015 08:34:14 -0800 Subject: [PATCH 1/3] Add tests for callable attributes raising exceptions --- tests/test_fields.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/test_fields.py b/tests/test_fields.py index ab3418bd6..7f5f81029 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): From bdb73d558891192c96368d5ca2266327302dba54 Mon Sep 17 00:00:00 2001 From: Evan Heidtmann Date: Thu, 26 Feb 2015 09:00:51 -0800 Subject: [PATCH 2/3] Avoid swallowing exceptions thrown in callable attributes --- rest_framework/fields.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index a5348922a..01e7c78c8 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -71,7 +71,11 @@ def get_attribute(instance, attrs): except ObjectDoesNotExist: return None if is_simple_callable(instance): - instance = instance() + try: + instance = instance() + except (AttributeError, KeyError) as exc: + raise ValueError('Exception raised in callable attribute "{0}"; original exception was: {1}'.format(attr, exc)) + return instance From e6b06c34c1ee526b65c92b9071c47be2ddc668c4 Mon Sep 17 00:00:00 2001 From: Evan Heidtmann Date: Thu, 26 Feb 2015 09:20:17 -0800 Subject: [PATCH 3/3] Add explanation for this exception mutation --- rest_framework/fields.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 01e7c78c8..f2791a13f 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -74,6 +74,9 @@ def get_attribute(instance, attrs): 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