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
Multipart Form Data Encoding Fix #3438
Conversation
0c88d31
to
42e41b9
Compare
42e41b9
to
4dec591
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.
Looks great! I don't think we need to call shouldCreateTask
twice, so I'm pretty sure one use is left over. That and running the formatter and this should be good to go.
@@ -1058,6 +1063,7 @@ open class Session { | |||
guard !request.isCancelled else { return } | |||
|
|||
guard let adapter = adapter(for: request) else { | |||
guard shouldCreateTask() else { return } |
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 don't think we should do this work twice. The check below should be enough, so do we want to remove this one?
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.
Ah I see why, sorry.
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.
It doesn't. This is inside the guard
for when we don't have an adapter
.
Tests/UploadTests.swift
Outdated
XCTAssertNil(response?.error) | ||
|
||
switch uploadRequest.uploadable { | ||
case .data(let data): |
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.
Our usual style is case let
, so make sure to run the formatter and it'll fix it.
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.
On it!
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.
👍
ad7978d
to
5f96852
Compare
This PR fixes up a regression introduced by PR #3421 where clients relying on the
URLRequest
creation process to append body parts toMultipartFormData
objects were no longer doing so. The body parts would no longer be appended since theUploadable
creation was happening before theURLRequest
creation took place.As @jshier and I looked into the issue further, it became apparent that we needed to redesign the logic to instead operate as follows:
URLRequest
(this is where I'm appending the body parts)URLRequest
with any appended interceptorsUploadable
which will encode theMultipartFormData
URLSessionUploadTask
which will actually upload the dataThis approach will ensure we not only allow clients to append body parts as part of the
URLRequestConvertible
conformance prior to encoding, but it also allows us to avoid encoding theMultipartFormData
if theURLRequest
creation or adaptation fails. Win win!Issue Link 🔗
No original issue filed.
Goals ⚽
MultipartFormData
as part of theURLRequest
creation processMultipartFormData
upload process to only encode the data if theURLRequest
creation and adaptation process succeedsImplementation Details 🚧
Minor change internally where we're just re-ordering the operations for uploads.
Testing Details 🔍
Added a test which ensures the body parts are appended to the
MultipartFormData
as part of theURLRequest
creation process and also verifies the size of the data being uploaded.