File tree Expand file tree Collapse file tree 2 files changed +16
-5
lines changed Expand file tree Collapse file tree 2 files changed +16
-5
lines changed Original file line number Diff line number Diff line change @@ -55,16 +55,16 @@ def default(self, obj):
55
55
elif hasattr (obj , 'tolist' ):
56
56
# Numpy arrays and array scalars.
57
57
return obj .tolist ()
58
+ elif (coreapi is not None ) and isinstance (obj , (coreapi .Document , coreapi .Error )):
59
+ raise RuntimeError (
60
+ 'Cannot return a coreapi object from a JSON view. '
61
+ 'You should be using a schema renderer instead for this view.'
62
+ )
58
63
elif hasattr (obj , '__getitem__' ):
59
64
try :
60
65
return dict (obj )
61
66
except :
62
67
pass
63
68
elif hasattr (obj , '__iter__' ):
64
69
return tuple (item for item in obj )
65
- elif (coreapi is not None ) and isinstance (obj , (coreapi .Document , coreapi .Error )):
66
- raise RuntimeError (
67
- 'Cannot return a coreapi object from a JSON view. '
68
- 'You should be using a schema renderer instead for this view.'
69
- )
70
70
return super (JSONEncoder , self ).default (obj )
Original file line number Diff line number Diff line change 4
4
5
5
from django .test import TestCase
6
6
7
+ from rest_framework .compat import coreapi
7
8
from rest_framework .utils .encoders import JSONEncoder
8
9
9
10
@@ -79,3 +80,13 @@ def test_encode_uuid(self):
79
80
"""
80
81
unique_id = uuid4 ()
81
82
assert self .encoder .default (unique_id ) == str (unique_id )
83
+
84
+ def test_encode_coreapi_raises_error (self ):
85
+ """
86
+ Tests encoding a coreapi objects raises proper error
87
+ """
88
+ with self .assertRaises (RuntimeError ):
89
+ self .encoder .default (coreapi .Document ())
90
+
91
+ with self .assertRaises (RuntimeError ):
92
+ self .encoder .default (coreapi .Error ())
You can’t perform that action at this time.
0 commit comments