flask-restx icon indicating copy to clipboard operation
flask-restx copied to clipboard

Change to Request.get_json() in Werkzeug 2.1.0 breaks Argument.parse()

Open stacywsmith opened this issue 2 years ago • 1 comments

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)

  1. Install Werkzeug >= 2.1.0
  2. Call parse() on an Argument instance that is from a Request that is NOT content type application/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

stacywsmith avatar Mar 29 '22 00:03 stacywsmith

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

deamos avatar Jun 21 '22 14:06 deamos

I fixed flask-restx/flask_restx/reqparse.py in my restx-monkey patches. I hope it will help you, feedback is welcome. :)

Ryu-CZ avatar Aug 31 '22 14:08 Ryu-CZ