Skip to content

Commit

Permalink
0.6.0: fix request json body parser
Browse files Browse the repository at this point in the history
  • Loading branch information
joway committed Jul 30, 2019
1 parent 1ccb823 commit cdaf115
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lemon/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def json_parser(body: bytes, *args) -> ImmutableMultiDict:
raise RequestBodyParserError


def url_encoded_parser(body: bytes, *args) -> dict:
def url_encoded_parser(body: bytes, *args) -> ImmutableMultiDict:
return url_decode(body, cls=ImmutableMultiDict)


Expand Down
3 changes: 2 additions & 1 deletion lemon/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ def json(self) -> dict:
"""Transform request body to dict when content_type is 'application/json'
:return: dict
"""
return self.data.to_dict(flat=True) if self.data else None
d = self.data.to_dict(flat=False)
return {k: d[k][0] if len(d[k]) <= 1 else d[k] for k in d.keys()}

@property
def cookies(self) -> dict:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup

PACKAGE_VERSION = '0.5.0'
PACKAGE_VERSION = '0.6.0'
PACKAGE_REQUIRES = [
'uvicorn==0.8.4',
'kua==0.2',
Expand Down
9 changes: 6 additions & 3 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ async def handle(ctx: Context):
ctx.body = ctx.req.json
assert ctx.req.form['int'] == 1
assert ctx.req.form['str'] == 'xxx'
assert ctx.req.data['str'] == 'xxx'
assert ctx.req.data['str'] == 'xxx'
assert ctx.req.json['int'] == 1
assert ctx.req.json['str'] == 'xxx'
assert ctx.req.json['list'][0] == 'item1'
assert ctx.req.json['list'][1] == 'item2'

self.app.use(handle)
req = await self.post('/', data={
'int': 1,
'str': 'xxx'
'str': 'xxx',
'list': ['item1', 'item2'],
})
assert req.status_code == 200
data = req.json()
Expand Down

0 comments on commit cdaf115

Please sign in to comment.