-
Notifications
You must be signed in to change notification settings - Fork 339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle change to Werkzeug 2.1.0 change to Request.get_json(). #423
Handle change to Werkzeug 2.1.0 change to Request.get_json(). #423
Conversation
This fix is ok, if you want to keep the rest of the code calling Perhaps the following? for l in self.location:
if l in {"json", "get_json"}:
value = request.get_json(silent=True)
else:
... |
@davidsm Yes, I considered a fix along the lines you're suggesting. I don't have a strong preference, I'm fine with either approach and happy to change my PR if your approach is preferred. |
I just was about to open a similar PR, dependabot rallies the community. I prefer what David's suggested, as the mantra of "explicit is better then implicit" seems applicable: if in the future some entry made it's way into The way that an upstream change in werkzeug led to an easily-traced-down exception was good, and would have been much harder to track down if that exception was being swallowed. |
As a maintainer, i prefer @davidism 's answer as well. A few other points: I actually like that Werkzeug is raising an error. I see why some people, given how disparate clients are, wouldn't like it (the example of JQuery is interesting). Given we do not know the client, keeping the old behaviour, utilizing the method provided by Werkzeug makes sense Finally, please check unit tests around this. there seems to be an issue with running |
You'll need to update Black as well, it had a bug with how it was handling compatibility with Click, but it was fixed in Black 22.3.0. psf/black#2964 |
23d74bb
to
d88f81d
Compare
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 without `silent=True`, or the `Request.json` property is accessed, and the content type is not `"application/json"`. Argument parsing allows parsing from multiple locations, and defaults to `["json", "values"]`, but if the locations include `"json"` and the content type is not `"application/json"`, a `BadRequest` is now raised with Werkzeug >= 2.1.0. Invoking `Request.get_json()` with the `silent=True` parameter now handles the situation where `"json"` is included in the locations, but the content type is not `"application/json"`.
d88f81d
to
57c5beb
Compare
PR has been updated. There is still one failing test which appears to be unrelated. |
|
Huh? get_json is not new. |
Oh, you're right, I got confused by files being reorganized in Werkzeug between 1.x and 2.x, sorry. I'm debugging some issue which at first glance seems to be linked to this patch + older Flask/Werkzeug version, and I misdiagnosed. |
Actually I think we're both correct: get_json exists, but by default wrappers.request.Request in Werkzeug 1.0.1 doesn't include JSONMixin which is what provides
See https://werkzeug.palletsprojects.com/en/1.0.x/request_data/?highlight=jsonmixin#how-to-extend-parsing from the 1.0.x documentation. It's still unclear to me whether the impact goes past the unit tests being broken, but that should be addressed anyway (unless Werkzeug < 2.x is dropped explicitly). |
FWIW, the following should be ~ equivalent and I confirmed it passes tests on both < 2 and >= 2.1. I don't know whether there was a good reason to not write the patch in this way (which seems like a smaller incremental diff), and I'm definitely not suggesting it's a good solution (let alone the best solution). I'm just providing this as a baseline for something that keeps backwards compat.
|
Can we move this forward ? Do we think there is anything blocking us merging this ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix and your patience!
Are you guys planning on creating a new release with this at any point? |
Fixes #422
pallets/werkzeug#2339 changed the behavior of Request.get_json()
and the Request.json property to raise a BadRequest if Request.json
is accessed and the content type is not
application/json
.Argument parsing allows parsing from multiple locations, but if the
locations include
json
and the content type is notapplication/json
,a
BadRequest
is now raised with Werkzeug >= 2.1.0.Handle this case and continue argument parsing.