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

[swift5][client] allow request cancellation and authentication flow to work together #11019

Merged

Conversation

4brunu
Copy link
Contributor

@4brunu 4brunu commented Dec 2, 2021

Fixes #11002

With the introduction of cancellable request on the swift generator, it broke the authentication flow and this PR tries to fix that.

Instead of returning the URLSessionTask, we return a class of ours, OpenAPIRequestCancellable, and the users may call the cancel method of OpenAPIRequestCancellable.

There is one OpenAPIRequestCancellable instance per request.

On the other hand, the URLSessionImplementations/AlamofireImplementations save the current request, URLSessionTask in case of URLSession or Request in case of Alamofire, in the OpenAPIRequestCancellable so that the request can be later on cancelled.

In case there is a retry of the request, we save the new URLSessionTask in the current request, so that the new request can be cancelled.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (5.3.0), 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@jgavris (2017/07) @ehyche (2017/08) @Edubits (2017/09) @jaz-ah (2017/09) @4brunu (2019/11)

@4brunu
Copy link
Contributor Author

4brunu commented Dec 2, 2021

@denizdogan @davidhorvath @funzin @jarrodparkes I would appreciate if you could help to review this PR 🙂

@jarrodparkes
Copy link
Contributor

@4brunu going to try and dig into this today — thanks for reaching out

Copy link
Contributor

@jarrodparkes jarrodparkes left a comment

Choose a reason for hiding this comment

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

@4brunu I was able to copy/paste the BearerRequestBuilderFactory example without modification. Was there something else I needed to check here? From what I gather, prior to this change, the example wouldn't work/compile?

@4brunu
Copy link
Contributor Author

4brunu commented Dec 3, 2021

@4brunu I was able to copy/paste the BearerRequestBuilderFactory example without modification. Was there something else I needed to check here? From what I gather, prior to this change, the example wouldn't work/compile?

Yes, it wouldn't compile.
If you could look at the implementation of this feature, to see if you find anything wrong and if you could test the network request cancel method, it would be nice.
Just to make sure I didn't make any mistake, or introduced new bugs.

@jarrodparkes
Copy link
Contributor

jarrodparkes commented Dec 6, 2021

@4brunu just ran a little test with the Network Link Conditioner tool (so I could emulate a bad network), and the cancelling worked fine 👍

        print("starting task1")
        let task1 = PetstoreClientAPI.PetAPI.getPetById(petId: 9223372036854775807) { data, error in
            if let data = data {
                dump(data)
            }
        }
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            print("cancelling task1")
            task1.cancel()
        }
        
        print("starting task2")
        let task2 = PetstoreClientAPI.PetAPI.getPetById(petId: 9223372036854775807) { data, error in
            if let data = data {
                dump(data)
            }
        }
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 15) {
            print("cancelling task2")
            task2.cancel()
        }

And the output...

starting task1
starting task2
cancelling task1
[task2 result] ▿ PetstoreClient.PetstoreClientAPI.Pet #0
  ▿ id: Optional(9223372036854775807)
    - some: 9223372036854775807
    - ... (redacted)
cancelling task2

Notice how task2 was able to finish before the delayed (15 seconds after request) cancel. While task1 was cancelled after only 2 seconds before the request could be completed — that is what its contents were not printed 👍

@4brunu
Copy link
Contributor Author

4brunu commented Dec 6, 2021

Thanks for helping testing this.
I also made some local tests and worked as expected 👍

@4brunu
Copy link
Contributor Author

4brunu commented Dec 8, 2021

@wing328 this one is ready to merge from me 👍

@wing328 wing328 added this to the 5.3.1 milestone Dec 8, 2021
@wing328 wing328 merged commit c941044 into OpenAPITools:master Dec 8, 2021
@4brunu 4brunu deleted the fix/swift-cancellable-authentication-flow branch December 14, 2021 15:56
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.

[BUG] [swift5] Update the Swift 5 bearer token authentication examples
3 participants