-
-
Notifications
You must be signed in to change notification settings - Fork 384
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 Django-Ninja PUT/PATCH request with UploadedFile #397
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -31,6 +31,28 @@ | |||||
__all__ = ["Operation", "PathView", "ResponseObject"] | ||||||
|
||||||
|
||||||
def load_request(request: HttpRequest): | ||||||
""" | ||||||
Loads a non-json PUT/PATCH request as a POST, reading data from request body | ||||||
and placing them in request.POST and request.FILES. | ||||||
This is required to fix the behaviour when we have a PUT/PATCH endpoint that also | ||||||
receives a File as parameter, forcing the data to be sent as multipart/form-data and | ||||||
making the ParamModel look up at request.POST and request.FILES | ||||||
""" | ||||||
if request.method in ("PUT", "PATCH") and request.content_type != "application/json": | ||||||
original_method = request.method | ||||||
if hasattr(request, '_post'): | ||||||
del request._post | ||||||
del request._files | ||||||
try: | ||||||
request.method = "POST" | ||||||
request.META['REQUEST_METHOD'] = 'POST' | ||||||
request._load_post_and_files() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should just hit
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct me if i am wrong but this is probably due to how django handles Quoting @deniswvieira,
The alternative is to,
Which in turn adds churn There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @baseplate-admin |
||||||
request.META['REQUEST_METHOD'] = original_method | ||||||
request.method = original_method | ||||||
except Exception: | ||||||
pass | ||||||
Comment on lines
+47
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we do this.. why not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is more than just a style fix too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with contextlib.supress(Exception):
request.method = "POST"
request.META['REQUEST_METHOD'] = 'POST'
request._load_post_and_files()
request.META['REQUEST_METHOD'] = original_method
request.method = original_method I was thinking something like this
I have a small question ( if you dont mind ).. Can this method actually fail ? If it does, don't you think its a problem on either There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, your code suggestion is what I had in mind. |
||||||
|
||||||
class Operation: | ||||||
def __init__( | ||||||
self, | ||||||
|
@@ -90,6 +112,7 @@ def __init__( | |||||
view_func._ninja_contribute_to_operation(self) # type: ignore | ||||||
|
||||||
def run(self, request: HttpRequest, **kw: Any) -> HttpResponseBase: | ||||||
load_request(request) | ||||||
error = self._run_checks(request) | ||||||
if error: | ||||||
return error | ||||||
|
@@ -240,6 +263,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: | |||||
self.is_async = True | ||||||
|
||||||
async def run(self, request: HttpRequest, **kw: Any) -> HttpResponseBase: # type: ignore | ||||||
load_request(request) | ||||||
error = self._run_checks(request) | ||||||
if error: | ||||||
return error | ||||||
|
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.
def load_request(request: HttpRequest) -> None: