mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
JSONEncoder: ensure empty listlikes remain lists, not dicts (#6794)
This commit is contained in:
parent
7915485c0d
commit
5c922fb39d
|
@ -57,8 +57,9 @@ class JSONEncoder(json.JSONEncoder):
|
|||
'You should be using a schema renderer instead for this view.'
|
||||
)
|
||||
elif hasattr(obj, '__getitem__'):
|
||||
cls = (list if isinstance(obj, (list, tuple)) else dict)
|
||||
try:
|
||||
return dict(obj)
|
||||
return cls(obj)
|
||||
except Exception:
|
||||
pass
|
||||
elif hasattr(obj, '__iter__'):
|
||||
|
|
|
@ -8,6 +8,7 @@ from django.utils.timezone import utc
|
|||
|
||||
from rest_framework.compat import coreapi
|
||||
from rest_framework.utils.encoders import JSONEncoder
|
||||
from rest_framework.utils.serializer_helpers import ReturnList
|
||||
|
||||
|
||||
class MockList:
|
||||
|
@ -93,3 +94,10 @@ class JSONEncoderTests(TestCase):
|
|||
"""
|
||||
foo = MockList()
|
||||
assert self.encoder.default(foo) == [1, 2, 3]
|
||||
|
||||
def test_encode_empty_returnlist(self):
|
||||
"""
|
||||
Tests encoding an empty ReturnList
|
||||
"""
|
||||
foo = ReturnList(serializer=None)
|
||||
assert self.encoder.default(foo) == []
|
||||
|
|
Loading…
Reference in New Issue
Block a user