flask-restx
flask-restx copied to clipboard
Change to Request.get_json() in Werkzeug 2.1.0 breaks Argument.parse()
pallets/werkzeug#2339 changed the behavior of Request.get_json()
and the Request.json
property to raise a BadRequest
if Request.get_json()
is called, or the Request.json
property
is accessed and the content type is not application/json
.
Repro Steps (if applicable)
- Install Werkzeug >= 2.1.0
- Call
parse()
on anArgument
instance that is from aRequest
that is NOT content typeapplication/json
Expected Behavior
Argument should be parsed from the query string.
Actual Behavior
raises
werkzeug.exceptions.BadRequest: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
Error Messages/Stack Trace
File "./rest/api/v1/endpoints/services.py", line 207, in get
args = SERVICES_PARSER.parse_args()
File "./.tox/py38/lib/python3.8/site-packages/flask_restx/reqparse.py", line 386, in parse_args
value, found = arg.parse(req, self.bundle_errors)
File "./.tox/py38/lib/python3.8/site-packages/flask_restx/reqparse.py", line 215, in parse
source = self.source(request)
File "./.tox/py38/lib/python3.8/site-packages/flask_restx/reqparse.py", line 149, in source
value = getattr(request, l, None)
File "./.tox/py38/lib/python3.8/site-packages/werkzeug/wrappers/request.py", line 540, in json
return self.get_json()
File "./.tox/py38/lib/python3.8/site-packages/werkzeug/wrappers/request.py", line 571, in get_json
return self.on_json_loading_failed(None)
File "./.tox/py38/lib/python3.8/site-packages/flask/wrappers.py", line 133, in on_json_loading_failed
raise BadRequest() from e
werkzeug.exceptions.BadRequest: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
Environment
- Python version: CPython 3.8.12
- Flask version: 2.1.0
- Flask-RESTX version: 0.5.1
- Other installed Flask extensions: N/A
- Werkzeug: 2.1.0
Any movement on this issue? I've run into this, and it has been blocking a long change of much needed dependency updates. For now, I've been able to find a work around by adding the code from the following link: https://github.com/pallets/flask/issues/4552#issuecomment-1109785314
I fixed flask-restx/flask_restx/reqparse.py in my restx-monkey patches. I hope it will help you, feedback is welcome. :)