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

[REQ] [Swift5] Cancellation of requests #8658

Closed
mkj-is opened this issue Feb 9, 2021 · 2 comments · Fixed by #10855
Closed

[REQ] [Swift5] Cancellation of requests #8658

mkj-is opened this issue Feb 9, 2021 · 2 comments · Fixed by #10855

Comments

@mkj-is
Copy link
Contributor

mkj-is commented Feb 9, 2021

Is your feature request related to a problem? Please describe.

Swift 5 generated code does not support cancellation of requests. This is the case for all libraries you can configure using responseAs option (PromiseKit, RxSwift, Result, Combine) and even the standard completion handler.

Combine and RXSwift

Reactive frameworks have the concept of cancellation built-in.

The Combine generator uses Future, which does not support cancellation and is primarily designed to wrap APIs based on completion handlers. Similarly, the RxSwift Observable is created in a way the task is cannot be disposed during its execution.

PromiseKit

PromiseKit does not have the concept of cancellation built-in as of version 6.x and therefore there is not a strong need to do any updates in here.

Describe the solution you'd like

If the generated API call methods would return URL session data task, the user would be able to control the lifecycle of the request.

@discardableResult
open class func addPet(
    pet: Pet,
    apiResponseQueue: DispatchQueue = OpenAPIClientAPI.apiResponseQueue,
    completion: @escaping ((_ data: Pet?, _ error: Error?) -> Void)
) -> URLSessionTask

Discardable result is added to keep backward compatibility, so no warnings are thrown when you regenerate the code.

Combine and RxSwift

We would neet to change the execute method on RequestBuilder to also return the data task, so we can properly wrap the data task in a Publisher or an Observable.

Describe alternatives you've considered

  • Copy the templates and rewrite them to fulfill our use-case and to have expanded feture set. (We did and it works fine for us. I believe this is core feature of client-server communication and many of the other users could benefit from such feature.)
  • Do not care about cancellation. (We want to optimize the data requirements and it makes sense to end some requests in case we no longer need to finish loading them. This is the case for most of the GET requests or more generally for all the requests which are idempotent.)

Additional context

What do you think about this approach? I'm willing to help with the implementation, I see many ways how the Swift generator could be improved and in my opinion this is the primary feature we miss in the standard generator templates.

@4brunu
Copy link
Contributor

4brunu commented May 31, 2021

This looks like a nice addition to the project, would you like to create a PR with this?

@c-villain
Copy link

c-villain commented Aug 26, 2021

any news about this pr @mkj-is ?

davidhorvath added a commit to davidhorvath/openapi-generator that referenced this issue Nov 14, 2021
wing328 pushed a commit that referenced this issue Nov 18, 2021
* Implement cancellation of URLSession requests for Combine & RxSwift

resolve #8658

* Add generated sample code

* Add request cancellation for alamofire library and Result configuration

* Add generated sample code

* Add request cancellation for async await configuration

* Add generated sample code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants