-
-
Notifications
You must be signed in to change notification settings - Fork 565
Fix sanic adapter casting files to io.BytesIO #3751
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
Conversation
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
|
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 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
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3751 +/- ##
==========================================
- Coverage 95.05% 95.04% -0.02%
==========================================
Files 501 502 +1
Lines 32632 32682 +50
Branches 1694 1695 +1
==========================================
+ Hits 31019 31061 +42
- Misses 1341 1348 +7
- Partials 272 273 +1 🚀 New features to boost your workflow:
|
CodSpeed Performance ReportMerging #3751 will not alter performanceComparing Summary
|
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 was revisiting this, just marking as "changes requested" to make sure I remember to check it again for any changes.
I think the only thing missing is to adjust the RELEASE.md
file
@bellini666 What exactly is there to change about RELEASE.md? |
Hey @Maypher Sorry for taking long to see this. I meant this comment here: https://github.com/strawberry-graphql/strawberry/pull/3751/files#r1912412757 |
io.BytesIO which is the filetype used by AIOHTTP. This commit fixes this issue by directly assigning the file instead of casting it.
for more information, see https://pre-commit.ci
08d64bd
to
698f5e2
Compare
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.
1ad46d7
to
93f37ce
Compare
93f37ce
to
c07c3b2
Compare
Thanks for contributing to Strawberry! 🎉 You've been invited to join You can also request a free sticker by filling this form: https://forms.gle/dmnfQUPoY5gZbVT67 And don't forget to join our discord server: https://strawberry.rocks/discord 🔥 |
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
.