Skip to content

Commit a87d604

Browse files
demianbrechtbgrant
andauthored
fix json emitter for empty response bodies (#178)
* fix json emitter for empty response bodies * Update django_declarative_apis/resources/emitters.py Co-authored-by: bgrant <[email protected]> * pr feedback * changelog update, version bump --------- Co-authored-by: bgrant <[email protected]>
1 parent 7a57ce9 commit a87d604

File tree

6 files changed

+27
-11
lines changed

6 files changed

+27
-11
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.33.0
2+
current_version = 0.34.0
33

44
[bumpversion:file:pyproject.toml]
55

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
# [Unreleased]
88

9+
# [0.34.0]
10+
- [PR 178](https://github.com/salesforce/django-declarative-apis/pull/178) Fix JSONEmitter for empty response bodies.
11+
912
# [0.33.0]
1013
- [PR 170](https://github.com/salesforce/django-declarative-apis/pull/170) Update publish workflow
1114
- [PR 173](https://github.com/salesforce/django-declarative-apis/pull/173) handle request.GET or request.POST being None + a few other improvements

django_declarative_apis/resources/emitters.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,24 @@ def decode(self, data):
182182

183183
def render(self, request):
184184
cb = request.GET.get("callback", None)
185-
assert cb is None, "JSONP Callbacks not suppoted"
186-
seria = json.dumps(
187-
self.decode(self.construct()),
188-
cls=DjangoJSONEncoder,
189-
ensure_ascii=False,
190-
indent=4,
191-
)
185+
assert cb is None, "JSONP Callbacks not supported"
186+
seria = self.decode(self.construct())
187+
if isinstance(seria, list):
188+
if len(seria) == 0 or (len(seria) == 1 and len(seria[0]) == 0):
189+
# the body is empty, no need to run json.dumps
190+
return ""
192191

193192
# Callback
194193
# TODO: do we care about JSONP?
195194
# if cb and is_valid_jsonp_callback_value(cb):
196195
# return '%s(%s)' % (cb, seria)
197196

198-
return seria
197+
return json.dumps(
198+
seria,
199+
cls=DjangoJSONEncoder,
200+
ensure_ascii=False,
201+
indent=4,
202+
)
199203

200204

201205
Emitter.register("json", JSONEmitter, "application/json; charset=utf-8")

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
# built documents.
7676

7777
# The full version, including alpha/beta/rc tags.
78-
release = "0.33.0" # set by bumpversion
78+
release = "0.34.0" # set by bumpversion
7979

8080
# The short X.Y version.
8181
version = release.rsplit(".", 1)[0]

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "django-declarative-apis"
7-
version = "0.33.0" # set by bumpversion
7+
version = "0.34.0" # set by bumpversion
88
description = "Simple, readable, declarative APIs for Django"
99
readme = "README.md"
1010
dependencies = [

tests/resources/test_emitters.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ def test_decode(self):
109109
resp = em.render(django.test.RequestFactory().get("/"))
110110
self.assertEqual(json.loads(resp), ["foo", "bar"])
111111

112+
def test_decode_empty_list(self):
113+
em = emitters.JSONEmitter([""], lambda: None)
114+
resp = em.render(django.test.RequestFactory().get("/"))
115+
self.assertEqual(resp, "")
116+
117+
em = emitters.JSONEmitter([], lambda: None)
118+
resp = em.render(django.test.RequestFactory().get("/"))
119+
self.assertEqual(resp, "")
120+
112121

113122
class DjangoEmitterTestCase(unittest.TestCase):
114123
def test_render_http_response_succes(self):

0 commit comments

Comments
 (0)