Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aiohttpparser: Fix 500 error with JSON content-type and empty body #297

Merged
merged 3 commits into from Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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