Skip to content
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

Merged
merged 3 commits into from Apr 17, 2021
Merged

Conversation

cnoon
Copy link
Member

@cnoon cnoon commented Apr 16, 2021

This PR fixes up a regression introduced by PR #3421 where clients relying on the URLRequest creation process to append body parts to MultipartFormData objects were no longer doing so. The body parts would no longer be appended since the Uploadable creation was happening before the URLRequest 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:

  1. Create the initial URLRequest (this is where I'm appending the body parts)
  2. Adapt the URLRequest with any appended interceptors
  3. Create the Uploadable which will encode the MultipartFormData
  4. Create theURLSessionUploadTask which will actually upload the data

This 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 the MultipartFormData if the URLRequest creation or adaptation fails. Win win!

Issue Link 🔗

No original issue filed.

Goals ⚽

  • To allow clients to continue to append body parts to MultipartFormData as part of the URLRequest creation process
  • To streamline the MultipartFormData upload process to only encode the data if the URLRequest creation and adaptation process succeeds

Implementation 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 the URLRequest creation process and also verifies the size of the data being uploaded.

@cnoon cnoon requested a review from jshier April 16, 2021 23:26
@cnoon cnoon self-assigned this Apr 16, 2021
@cnoon cnoon added this to the Next milestone Apr 16, 2021
@cnoon cnoon force-pushed the cnoon/multipart-encoding-bugfix branch from 0c88d31 to 42e41b9 Compare April 17, 2021 00:06
@cnoon cnoon force-pushed the cnoon/multipart-encoding-bugfix branch from 42e41b9 to 4dec591 Compare April 17, 2021 00:17
Copy link
Contributor

@jshier jshier left a 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 }
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Member Author

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.

XCTAssertNil(response?.error)

switch uploadRequest.uploadable {
case .data(let data):
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On it!

Copy link
Contributor

@jshier jshier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@cnoon cnoon force-pushed the cnoon/multipart-encoding-bugfix branch from ad7978d to 5f96852 Compare April 17, 2021 01:03
@jshier jshier merged commit 3fbfb08 into master Apr 17, 2021
@jshier jshier deleted the cnoon/multipart-encoding-bugfix branch April 17, 2021 04:17
@jshier jshier modified the milestones: Next, 5.4.3 Apr 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants