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

Do not strip body before JSON decoding + do not return None on empty … #3486

Merged
merged 1 commit into from
Jan 5, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/3482.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not return `None` on `await response.json()` when body is empty. Instead, raise `json.JSONDecodeError` as expected.
8 changes: 2 additions & 6 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ def __init__(self, method: str, url: URL, *,

self._real_url = url
self._url = url.with_fragment(None)
self._body = None # type: Any
self._body = None # type: Optional[bytes]
self._writer = writer # type: Optional[asyncio.Task[None]]
self._continue = continue100 # None by default
self._closed = True
Expand Down Expand Up @@ -1028,14 +1028,10 @@ async def json(self, *, encoding: str=None,
'unexpected mimetype: %s' % ctype),
headers=self.headers)

stripped = self._body.strip() # type: ignore
if not stripped:
return None

if encoding is None:
encoding = self.get_encoding()

return loads(stripped.decode(encoding))
return loads(self._body.decode(encoding)) # type: ignore

async def __aenter__(self) -> 'ClientResponse':
return self
Expand Down
7 changes: 4 additions & 3 deletions tests/test_client_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import gc
import sys
from json import JSONDecodeError
from unittest import mock

import pytest
Expand Down Expand Up @@ -586,11 +587,11 @@ async def test_json_no_content(loop, session) -> None:
loop=loop,
session=session)
response._headers = {
'Content-Type': 'data/octet-stream'}
'Content-Type': 'application/json'}
response._body = b''

res = await response.json(content_type=None)
assert res is None
with pytest.raises(JSONDecodeError):
await response.json(content_type=None)


async def test_json_override_encoding(loop, session) -> None:
Expand Down