diff --git a/rest_framework/utils/encoders.py b/rest_framework/utils/encoders.py index 8203a7bc8c..8896e4f2c6 100644 --- a/rest_framework/utils/encoders.py +++ b/rest_framework/utils/encoders.py @@ -55,6 +55,11 @@ def default(self, obj): elif hasattr(obj, 'tolist'): # Numpy arrays and array scalars. return obj.tolist() + elif (coreapi is not None) and isinstance(obj, (coreapi.Document, coreapi.Error)): + raise RuntimeError( + 'Cannot return a coreapi object from a JSON view. ' + 'You should be using a schema renderer instead for this view.' + ) elif hasattr(obj, '__getitem__'): try: return dict(obj) @@ -62,9 +67,4 @@ def default(self, obj): pass elif hasattr(obj, '__iter__'): return tuple(item for item in obj) - elif (coreapi is not None) and isinstance(obj, (coreapi.Document, coreapi.Error)): - raise RuntimeError( - 'Cannot return a coreapi object from a JSON view. ' - 'You should be using a schema renderer instead for this view.' - ) return super(JSONEncoder, self).default(obj) diff --git a/tests/test_encoders.py b/tests/test_encoders.py index 6871414766..403b1e9b2e 100644 --- a/tests/test_encoders.py +++ b/tests/test_encoders.py @@ -4,6 +4,7 @@ from django.test import TestCase +from rest_framework.compat import coreapi from rest_framework.utils.encoders import JSONEncoder @@ -79,3 +80,13 @@ def test_encode_uuid(self): """ unique_id = uuid4() assert self.encoder.default(unique_id) == str(unique_id) + + def test_encode_coreapi_raises_error(self): + """ + Tests encoding a coreapi objects raises proper error + """ + with self.assertRaises(RuntimeError): + self.encoder.default(coreapi.Document()) + + with self.assertRaises(RuntimeError): + self.encoder.default(coreapi.Error())
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: