diff --git a/src/webargs/aiohttpparser.py b/src/webargs/aiohttpparser.py index 774e1b9a..c04f0b80 100644 --- a/src/webargs/aiohttpparser.py +++ b/src/webargs/aiohttpparser.py @@ -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: @@ -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."]} diff --git a/src/webargs/core.py b/src/webargs/core.py index 96a4516e..36d9d437 100644 --- a/src/webargs/core.py +++ b/src/webargs/core.py @@ -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, @@ -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: diff --git a/src/webargs/flaskparser.py b/src/webargs/flaskparser.py index 278c4fa1..31fc2732 100644 --- a/src/webargs/flaskparser.py +++ b/src/webargs/flaskparser.py @@ -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, diff --git a/src/webargs/pyramidparser.py b/src/webargs/pyramidparser.py index 56c27365..36bd052f 100644 --- a/src/webargs/pyramidparser.py +++ b/src/webargs/pyramidparser.py @@ -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, diff --git a/src/webargs/tornadoparser.py b/src/webargs/tornadoparser.py index 1b0c73d8..67779479 100644 --- a/src/webargs/tornadoparser.py +++ b/src/webargs/tornadoparser.py @@ -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, diff --git a/tests/test_aiohttpparser.py b/tests/test_aiohttpparser.py index 6c785685..d8689d36 100644 --- a/tests/test_aiohttpparser.py +++ b/tests/test_aiohttpparser.py @@ -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"")} diff --git a/tests/test_pyramidparser.py b/tests/test_pyramidparser.py index fb8577cc..36bff23e 100644 --- a/tests/test_pyramidparser.py +++ b/tests/test_pyramidparser.py @@ -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."]}