-
Notifications
You must be signed in to change notification settings - Fork 51
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
Fix issue with posting with no content #120
Conversation
Related to #94 |
flask_potion/schema.py
Outdated
# TODO change to request.get_json(silent=False) to catch invalid JSON | ||
data = request.get_json(silent=True) | ||
|
||
if request.method in ('POST', 'PATCH', 'PUT', 'DELETE'): | ||
if data and request.mimetype != 'application/json': |
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.
I believe data and request.mimetype != 'application/json'
is always False
. request.get_json()
will always return None
if the mimetype is not 'application/json'
so RequestMustBeJSON
never gets raised.
The check that should be made instead is if data is required, e.g. self.fields
is not empty, require a JSON request otherwise allow an empty request.
if request.method in ('POST', 'PATCH', 'PUT', 'DELETE'):
if self.fields and request.mimetype != 'application/json':
raise RequestMustBeJSON()
This could be moved back into the top of the function.
A unit test should be added to make sure.
1 similar comment
apparently my test doesn't work in python 2 |
Is this not something that can be tested with the regular test client? |
@lyschoening Not without creating an entire test app, which seems frivolous. The Flask test client is better suited for Flask apps, then extensions. |
Check if there is no content before aborting the request.
This fixes issues with posts to endpoints that have no parameters i.e.
Without this fix, you'd have one of two issues: