Skip to content

Commit

Permalink
Merge pull request #297 from kochab/fix-229
Browse files Browse the repository at this point in the history
aiohttpparser: Fix 500 error with JSON content-type and empty body
  • Loading branch information
sloria committed Oct 25, 2018
2 parents 2c265a8 + 7454c35 commit 1019518
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 9 additions & 0 deletions tests/test_aiohttpparser.py
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-

import asyncio
import webtest
import webtest_aiohttp
import pytest

from io import BytesIO
from webargs.core import MARSHMALLOW_VERSION_INFO
from webargs.testing import CommonTestCase
from tests.apps.aiohttp_app import create_app
Expand Down Expand Up @@ -63,3 +65,10 @@ def test_schema_as_kwargs_view(self, testapp):
assert testapp.get("/echo_use_schema_as_kwargs?name=Chandler").json == {
"name": "Chandler"
}

# https://github.com/sloria/webargs/pull/297
def test_empty_json_body(self, testapp):
environ = {"CONTENT_TYPE": "application/json", "wsgi.input": BytesIO(b"")}
req = webtest.TestRequest.blank("/echo", environ)
resp = testapp.do_request(req)
assert resp.json == {"name": "World"}
9 changes: 8 additions & 1 deletion webargs/aiohttpparser.py
Expand Up @@ -97,7 +97,14 @@ async def parse_json(self, req, name, field):
if json_data is None:
if not (req.body_exists and is_json_request(req)):
return core.missing
self._cache["json"] = json_data = await req.json()
try:
json_data = await req.json()
except json.JSONDecodeError as e:
if e.doc == "":
return core.missing
else:
raise e
self._cache["json"] = json_data
return core.get_value(json_data, name, field, allow_many_nested=True)

def parse_headers(self, req, name, field):
Expand Down

0 comments on commit 1019518

Please sign in to comment.