-
-
Notifications
You must be signed in to change notification settings - Fork 543
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 sanic adapter casting files to io.BytesIO #3751
base: main
Are you sure you want to change the base?
Conversation
io.BytesIO which is the filetype used by AIOHTTP. This commit fixes this issue by directly assigning the file instead of casting it.
Reviewer's Guide by SourceryThis PR fixes a bug in the Sequence diagram for Sanic file upload handling before and after fixsequenceDiagram
participant Client
participant SanicServer
participant FileHandler
Note over Client,FileHandler: Before Fix
Client->>SanicServer: Upload file
SanicServer->>FileHandler: convert_request_to_files_dict()
FileHandler->>FileHandler: Convert to BytesIO
FileHandler-->>SanicServer: Return BytesIO object
Note over Client,FileHandler: After Fix
Client->>SanicServer: Upload file
SanicServer->>FileHandler: convert_request_to_files_dict()
FileHandler-->>SanicServer: Return original Sanic File object
Class diagram showing the file handling type changesclassDiagram
class Request {
+files: dict
}
class FileHandler {
+convert_request_to_files_dict(request: Request) dict
}
class File {
+body: bytes
+name: str
+type: str
}
note for FileHandler "Changed return type from\ndict[str, BytesIO] to\ndict[str, File]"
Request -- FileHandler
FileHandler -- File
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
for more information, see https://pre-commit.ci
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.
Hey @Maypher - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟡 General issues: 2 issues found
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
@@ -29,12 +28,12 @@ def convert_request_to_files_dict(request: Request) -> dict[str, Any]: | |||
if not request_files: | |||
return {} | |||
|
|||
files_dict: dict[str, Union[BytesIO, list[BytesIO]]] = {} | |||
files_dict: dict[str, Union[File, list[File]]] = {} | |||
|
|||
for field_name, file_list in request_files.items(): | |||
assert len(file_list) == 1 |
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.
issue: Consider handling multiple files per field name instead of asserting there will be exactly one
The assertion could cause server crashes if multiple files are uploaded with the same field name. Consider returning a list of File objects when multiple files are present, matching the type hint Union[File, list[File]]
.
RELEASE.md
Outdated
Release type: patch | ||
|
||
Fix bug where files would be converted into io.BytesIO when using the sanic GraphQLView | ||
instead of using the sanic File type |
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.
suggestion: Check casing and specific name for "sanic File type"
Should it be "Sanic File", "Sanic file type", or something more specific? Referencing the Sanic documentation might help.
Thanks for adding the Here's a preview of the changelog: Fix bug where files would be converted into io.BytesIO when using the sanic GraphQLView Here's the tweet text:
|
Thanks for adding the Here's a preview of the changelog: Fix bug where files would be converted into io.BytesIO when using the sanic GraphQLView Here's the tweet text:
|
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.
This looks great, I might spend a bit of time moving the tests to the http tests, so we have one test for all integrations 😊
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.
LGTM :)
Will leave final review to @patrick91
Release type: patch | ||
|
||
Fix bug where files would be converted into io.BytesIO when using the sanic GraphQLView | ||
instead of using the sanic File type |
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.
thought: although a bug, someone using it might have gotten already used to it being BytesIO
, so maybe we should mark this as a minor and mention that anyone relying on old behavior should adjust their code?
What do you think @patrick91 ?
The current implementation of
strawberry.sanic.utils.convert_request_to_files_dict
casts uploaded files toio.BytesIO
, making it return a type that doesn't coincide with the docs. This commit removes this casting and makes the function work in accordance with the docs.Description
Changed
files_dict[field_name] = BytesIO(file_list[0].body)
tofiles_dict[field_name] = file_list[0]
Types of Changes
Issues Fixed or Closed by This PR
Fix #3750
Checklist
Summary by Sourcery
Bug Fixes:
File
objects instead of being cast toio.BytesIO
.