Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
a5fcf10c
Commit
a5fcf10c
authored
Jan 07, 2017
by
Artem Muterko
Committed by
Tom Christie
Jan 06, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Raise RuntimeError when trying to encode coreapi objects (#4790)
parent
cf3862d5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
5 deletions
+16
-5
rest_framework/utils/encoders.py
+5
-5
tests/test_encoders.py
+11
-0
No files found.
rest_framework/utils/encoders.py
View file @
a5fcf10c
...
@@ -55,6 +55,11 @@ class JSONEncoder(json.JSONEncoder):
...
@@ -55,6 +55,11 @@ class JSONEncoder(json.JSONEncoder):
elif
hasattr
(
obj
,
'tolist'
):
elif
hasattr
(
obj
,
'tolist'
):
# Numpy arrays and array scalars.
# Numpy arrays and array scalars.
return
obj
.
tolist
()
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__'
):
elif
hasattr
(
obj
,
'__getitem__'
):
try
:
try
:
return
dict
(
obj
)
return
dict
(
obj
)
...
@@ -62,9 +67,4 @@ class JSONEncoder(json.JSONEncoder):
...
@@ -62,9 +67,4 @@ class JSONEncoder(json.JSONEncoder):
pass
pass
elif
hasattr
(
obj
,
'__iter__'
):
elif
hasattr
(
obj
,
'__iter__'
):
return
tuple
(
item
for
item
in
obj
)
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
)
return
super
(
JSONEncoder
,
self
)
.
default
(
obj
)
tests/test_encoders.py
View file @
a5fcf10c
...
@@ -4,6 +4,7 @@ from uuid import uuid4
...
@@ -4,6 +4,7 @@ from uuid import uuid4
from
django.test
import
TestCase
from
django.test
import
TestCase
from
rest_framework.compat
import
coreapi
from
rest_framework.utils.encoders
import
JSONEncoder
from
rest_framework.utils.encoders
import
JSONEncoder
...
@@ -79,3 +80,13 @@ class JSONEncoderTests(TestCase):
...
@@ -79,3 +80,13 @@ class JSONEncoderTests(TestCase):
"""
"""
unique_id
=
uuid4
()
unique_id
=
uuid4
()
assert
self
.
encoder
.
default
(
unique_id
)
==
str
(
unique_id
)
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
())
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment