Skip to content
Closed
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
8 changes: 6 additions & 2 deletions src/webargs/aiohttpparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async def load_json(self, req, schema: Schema):
if exc.doc == "":
return core.missing
return self._handle_invalid_json_error(exc, req)
except UnicodeDecodeError as exc:
except (UnicodeDecodeError, LookupError) as exc:
return self._handle_invalid_json_error(exc, req)

def load_headers(self, req, schema: Schema) -> MultiDictProxy:
Expand Down Expand Up @@ -170,7 +170,11 @@ def handle_error(
)

def _handle_invalid_json_error(
self, error: json.JSONDecodeError | UnicodeDecodeError, req, *args, **kwargs
self,
error: json.JSONDecodeError | UnicodeDecodeError | LookupError,
req,
*args,
**kwargs,
) -> typing.NoReturn:
error_class = exception_map[400]
messages = {"json": ["Invalid JSON body."]}
Expand Down
4 changes: 2 additions & 2 deletions src/webargs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ def pre_load(

def _handle_invalid_json_error(
self,
error: json.JSONDecodeError | UnicodeDecodeError,
error: json.JSONDecodeError | UnicodeDecodeError | LookupError,
req: Request,
*args: typing.Any,
**kwargs: typing.Any,
Expand Down Expand Up @@ -806,7 +806,7 @@ def load_json(self, req: Request, schema: ma.Schema) -> typing.Any:
if exc.doc == "":
return missing
return self._handle_invalid_json_error(exc, req)
except UnicodeDecodeError as exc:
except (UnicodeDecodeError, LookupError) as exc:
return self._handle_invalid_json_error(exc, req)

def load_json_or_form(self, req: Request, schema: ma.Schema) -> typing.Any:
Expand Down
2 changes: 1 addition & 1 deletion src/webargs/flaskparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _raw_load_json(self, req: flask.Request) -> typing.Any:

def _handle_invalid_json_error(
self,
error: json.JSONDecodeError | UnicodeDecodeError,
error: json.JSONDecodeError | UnicodeDecodeError | LookupError,
req: flask.Request,
*args: typing.Any,
**kwargs: typing.Any,
Expand Down
2 changes: 1 addition & 1 deletion src/webargs/pyramidparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def handle_error(

def _handle_invalid_json_error(
self,
error: json.JSONDecodeError | UnicodeDecodeError,
error: json.JSONDecodeError | UnicodeDecodeError | LookupError,
req: Request,
*args: typing.Any,
**kwargs: typing.Any,
Expand Down
2 changes: 1 addition & 1 deletion src/webargs/tornadoparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def handle_error(

def _handle_invalid_json_error(
self,
error: json.JSONDecodeError | UnicodeDecodeError,
error: json.JSONDecodeError | UnicodeDecodeError | LookupError,
req: HTTPServerRequest,
*args: typing.Any,
**kwargs: typing.Any,
Expand Down
11 changes: 11 additions & 0 deletions tests/test_aiohttpparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ def test_schema_as_kwargs_view(self, testapp):
"name": "Chandler"
}

# https://github.com/marshmallow-code/webargs/issues/1120
def test_parse_json_with_unknown_charset(self, testapp):
res = testapp.post(
"/echo_json",
'{"name": "Steve"}',
headers={"Content-Type": "application/json; charset=not-a-charset"},
expect_errors=True,
)
assert res.status_code == 400
assert res.json == {"json": ["Invalid JSON body."]}

# https://github.com/marshmallow-code/webargs/pull/297
def test_empty_json_body(self, testapp):
environ = {"CONTENT_TYPE": "application/json", "wsgi.input": BytesIO(b"")}
Expand Down
11 changes: 11 additions & 0 deletions tests/test_pyramidparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ def test_use_args_with_callable_view(self, testapp):

def test_parse_matchdict(self, testapp):
assert testapp.get("/echo_matchdict/42").json == {"mymatch": 42}

# https://github.com/marshmallow-code/webargs/issues/1120
def test_parse_json_with_unknown_charset(self, testapp):
res = testapp.post(
"/echo_json",
'{"name": "Steve"}',
headers={"Content-Type": "application/json; charset=not-a-charset"},
expect_errors=True,
)
assert res.status_code == 400
assert res.json == {"json": ["Invalid JSON body."]}