-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Request json raises error if no arguments passed #3302
Comments
I disagree. |
@asvetlov, seems we consider the problem from different sides. Does |
|
I agree with Andrew. If body is empty, then it's an empty string, but you can't decode it as a JSON, because it's invalid. |
Fixes as the location loading code undergoes refactoring for the flaskparser: - Restore missing mimetype check for JSON payloads - Treat form data as a multidict without extraneous AttributeError check (after testing, flask will return '{}' for form data when it is missing or empty -- just respect/allow this) - req.headers is a multidict type as well - fix some mistakes with new argument ordering Changes to the base test app at src/webargs/testing.py of note: - Because each route can only specify that it loads data from a single location (unless you pull extra tricks), several testing cases have had their routes modified, e.g. "/echo" -> "/echo_json" - New test, test_parse_json_list_error_malformed_data added to check that webargs does not incorrectly treat non-list data as singleton lists when passing data to a schema with list fields. The previous behavior was questionable before, but with full data going to schemas it definitely becomes incorrect. - Many tests which previously expected success with malformed data ignored now expect 422 errors. For example, sending a non-dict JSON value (e.g. `1`) should fail because it cannot be loaded by the schema. It should *not* be silently ignored because data was provided and failed to parse. This is a major behavior change for webargs. - Reverse the decision on marshmallow-code#297 . After reading the referent material, I agree with the aiohttp maintainers in aio-libs/aiohttp#3302 . "" and "{}" should not be treated equivalently -- they are different inputs. Additionally, I foresee that supporting "" as "{}" will pose an issue if the proposal for "json_or_form" in marshmallow-code#419 is accepted. More detailed explanation is provided in the inline comment, and the test for this case is preserved with its expectations reversed Supporting modifications were made in tests/apps/flask_app.py , including the addition of `ma.EXCLUDE` when the detected marshmallow version is >= 3. Minor fix in core: restore the cache clearing step. This is apparently used by the flaskparser. Possibly this only shows up under test usage? The main parse function should always be cloning the parser before its cache could be populated.
Fixes as the location loading code undergoes refactoring for the flaskparser: - Restore missing mimetype check for JSON payloads - Treat form data as a multidict without extraneous AttributeError check (after testing, flask will return '{}' for form data when it is missing or empty -- just respect/allow this) - req.headers is a multidict type as well - fix some mistakes with new argument ordering Changes to the base test app at src/webargs/testing.py of note: - Because each route can only specify that it loads data from a single location (unless you pull extra tricks), several testing cases have had their routes modified, e.g. "/echo" -> "/echo_json" - New test, test_parse_json_list_error_malformed_data added to check that webargs does not incorrectly treat non-list data as singleton lists when passing data to a schema with list fields. The previous behavior was questionable before, but with full data going to schemas it definitely becomes incorrect. - Many tests which previously expected success with malformed data ignored now expect 422 errors. For example, sending a non-dict JSON value (e.g. `1`) should fail because it cannot be loaded by the schema. It should *not* be silently ignored because data was provided and failed to parse. This is a major behavior change for webargs. - Reverse the decision on marshmallow-code#297 . After reading the referent material, I agree with the aiohttp maintainers in aio-libs/aiohttp#3302 . "" and "{}" should not be treated equivalently -- they are different inputs. Additionally, I foresee that supporting "" as "{}" will pose an issue if the proposal for "json_or_form" in marshmallow-code#419 is accepted. More detailed explanation is provided in the inline comment, and the test for this case is preserved with its expectations reversed Supporting modifications were made in tests/apps/flask_app.py , including the addition of `ma.EXCLUDE` when the detected marshmallow version is >= 3.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs. |
Long story short
The following code:
Raises an exception:
After my http request without (!)
PUT
parameters:Expected behaviour
I expect, if length of
request.text()
equals 0,request.json()
will return empty dictionary{}
instead of raising.Actual behaviour
While I pass parameter, my
request.text()
equals{"key": "value"}
andrequest.json()
equals{'key': 'value'}
. When I do not pass parameter,JSONDecodeError
is raised.Steps to reproduce
Your environment
The text was updated successfully, but these errors were encountered: