From a724c754bcecd1ca5d023613e0568dc0d7832436 Mon Sep 17 00:00:00 2001 From: Mike Welsh Date: Mon, 22 Apr 2024 08:51:12 -0400 Subject: [PATCH] Add `ResumableTask` to `URLSessionImplementations.mustache` (#18438) * Add `ResumableTask` to `URLSessionImplementations.mustache` - Makes it testable - Implementations can return something _other_ than a URLSessionDataTask if they want to implement another request format (sockets maybe?) - Default implementation for `URLSession` provided * Regenerate examples * Add more properties and rename to `CancelableResumableTask` * Regen samples * Rename missed reference * Missed some generated classes somehow * Rename from `CancellableResumableTask` to `URLSessionDataTaskProtocol` Rename from `resumableTask` to `dataTaskFromProtocol` --------- Co-authored-by: welshm-ideogram --- .../src/main/resources/swift5/Models.mustache | 4 +-- .../URLSessionImplementations.mustache | 30 ++++++++++++++++--- .../Classes/OpenAPIs/Models.swift | 4 +-- .../OpenAPIs/URLSessionImplementations.swift | 30 ++++++++++++++++--- .../Classes/OpenAPIs/Models.swift | 4 +-- .../OpenAPIs/URLSessionImplementations.swift | 30 ++++++++++++++++--- .../Classes/OpenAPIs/Models.swift | 4 +-- .../OpenAPIs/URLSessionImplementations.swift | 30 ++++++++++++++++--- .../Classes/OpenAPIs/Models.swift | 4 +-- .../OpenAPIs/URLSessionImplementations.swift | 30 ++++++++++++++++--- .../Classes/OpenAPIs/Models.swift | 4 +-- .../OpenAPIs/URLSessionImplementations.swift | 30 ++++++++++++++++--- .../Classes/OpenAPIs/Models.swift | 4 +-- .../OpenAPIs/URLSessionImplementations.swift | 30 ++++++++++++++++--- .../Classes/OpenAPIs/Models.swift | 4 +-- .../OpenAPIs/URLSessionImplementations.swift | 30 ++++++++++++++++--- .../Classes/OpenAPIs/Models.swift | 4 +-- .../OpenAPIs/URLSessionImplementations.swift | 30 ++++++++++++++++--- .../Classes/OpenAPIs/Models.swift | 4 +-- .../OpenAPIs/URLSessionImplementations.swift | 30 ++++++++++++++++--- .../Classes/OpenAPIs/Models.swift | 4 +-- .../OpenAPIs/URLSessionImplementations.swift | 30 ++++++++++++++++--- .../Classes/OpenAPIs/Models.swift | 4 +-- .../OpenAPIs/URLSessionImplementations.swift | 30 ++++++++++++++++--- .../Classes/OpenAPIs/Models.swift | 4 +-- .../OpenAPIs/URLSessionImplementations.swift | 30 ++++++++++++++++--- .../Classes/OpenAPIs/Models.swift | 4 +-- .../OpenAPIs/URLSessionImplementations.swift | 30 ++++++++++++++++--- .../Sources/PetstoreClient/Models.swift | 4 +-- .../URLSessionImplementations.swift | 30 ++++++++++++++++--- .../Classes/OpenAPIs/Models.swift | 4 +-- .../OpenAPIs/URLSessionImplementations.swift | 30 ++++++++++++++++--- .../Classes/OpenAPIs/Models.swift | 4 +-- .../OpenAPIs/URLSessionImplementations.swift | 30 ++++++++++++++++--- 34 files changed, 476 insertions(+), 102 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/swift5/Models.mustache b/modules/openapi-generator/src/main/resources/swift5/Models.mustache index 9a8af56a8a..3bd5e1c4f6 100644 --- a/modules/openapi-generator/src/main/resources/swift5/Models.mustache +++ b/modules/openapi-generator/src/main/resources/swift5/Models.mustache @@ -130,9 +130,9 @@ extension NullEncodable: Codable where Wrapped: Codable { } {{/useAlamofire}} {{^useAlamofire}} - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/modules/openapi-generator/src/main/resources/swift5/libraries/urlsession/URLSessionImplementations.mustache b/modules/openapi-generator/src/main/resources/swift5/libraries/urlsession/URLSessionImplementations.mustache index 0c8cc2863c..a17db02e68 100644 --- a/modules/openapi-generator/src/main/resources/swift5/libraries/urlsession/URLSessionImplementations.mustache +++ b/modules/openapi-generator/src/main/resources/swift5/libraries/urlsession/URLSessionImplementations.mustache @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ private var credentialStore = SynchronizedDictionary() } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest() diff --git a/samples/client/petstore/swift5/anycodableLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift b/samples/client/petstore/swift5/anycodableLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift index 7094040c9f..3d9e25e363 100644 --- a/samples/client/petstore/swift5/anycodableLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift +++ b/samples/client/petstore/swift5/anycodableLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift @@ -112,9 +112,9 @@ open class Response { public final class RequestTask { private var lock = NSRecursiveLock() - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/samples/client/petstore/swift5/anycodableLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/anycodableLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index ec201fa099..f05a180874 100644 --- a/samples/client/petstore/swift5/anycodableLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/anycodableLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -public protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +public protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +public protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ open class URLSessionRequestBuilder: RequestBuilder { } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest() diff --git a/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift b/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift index a32cdd64a8..c215138097 100644 --- a/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift +++ b/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift @@ -112,9 +112,9 @@ open class Response { public final class RequestTask: @unchecked Sendable { private var lock = NSRecursiveLock() - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index ec201fa099..f05a180874 100644 --- a/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -public protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +public protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +public protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ open class URLSessionRequestBuilder: RequestBuilder { } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest() diff --git a/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift b/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift index 7094040c9f..3d9e25e363 100644 --- a/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift +++ b/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift @@ -112,9 +112,9 @@ open class Response { public final class RequestTask { private var lock = NSRecursiveLock() - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index ec201fa099..f05a180874 100644 --- a/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -public protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +public protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +public protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ open class URLSessionRequestBuilder: RequestBuilder { } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest() diff --git a/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Models.swift b/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Models.swift index 7094040c9f..3d9e25e363 100644 --- a/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Models.swift +++ b/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Models.swift @@ -112,9 +112,9 @@ open class Response { public final class RequestTask { private var lock = NSRecursiveLock() - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index ec201fa099..f05a180874 100644 --- a/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -public protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +public protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +public protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ open class URLSessionRequestBuilder: RequestBuilder { } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest() diff --git a/samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/Models.swift b/samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/Models.swift index 7094040c9f..3d9e25e363 100644 --- a/samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/Models.swift +++ b/samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/Models.swift @@ -112,9 +112,9 @@ open class Response { public final class RequestTask { private var lock = NSRecursiveLock() - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index ec201fa099..f05a180874 100644 --- a/samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -public protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +public protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +public protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ open class URLSessionRequestBuilder: RequestBuilder { } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest() diff --git a/samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/Models.swift b/samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/Models.swift index 7094040c9f..3d9e25e363 100644 --- a/samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/Models.swift +++ b/samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/Models.swift @@ -112,9 +112,9 @@ open class Response { public final class RequestTask { private var lock = NSRecursiveLock() - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index ec201fa099..f05a180874 100644 --- a/samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -public protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +public protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +public protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ open class URLSessionRequestBuilder: RequestBuilder { } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest() diff --git a/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/Models.swift b/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/Models.swift index 8411465a49..e719b88675 100644 --- a/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/Models.swift +++ b/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/Models.swift @@ -112,9 +112,9 @@ internal class Response { internal final class RequestTask { private var lock = NSRecursiveLock() - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index ce6e6927f0..6d67c0361f 100644 --- a/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -internal protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +internal protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +internal protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + internal func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ internal class URLSessionRequestBuilder: RequestBuilder { } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest() diff --git a/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/Models.swift b/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/Models.swift index 7094040c9f..3d9e25e363 100644 --- a/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/Models.swift +++ b/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/Models.swift @@ -112,9 +112,9 @@ open class Response { public final class RequestTask { private var lock = NSRecursiveLock() - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index ec201fa099..f05a180874 100644 --- a/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -public protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +public protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +public protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ open class URLSessionRequestBuilder: RequestBuilder { } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest() diff --git a/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/Models.swift b/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/Models.swift index 7094040c9f..3d9e25e363 100644 --- a/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/Models.swift +++ b/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/Models.swift @@ -112,9 +112,9 @@ open class Response { public final class RequestTask { private var lock = NSRecursiveLock() - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index ec201fa099..f05a180874 100644 --- a/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -public protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +public protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +public protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ open class URLSessionRequestBuilder: RequestBuilder { } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest() diff --git a/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift b/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift index 7094040c9f..3d9e25e363 100644 --- a/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift +++ b/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift @@ -112,9 +112,9 @@ open class Response { public final class RequestTask { private var lock = NSRecursiveLock() - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index ec201fa099..f05a180874 100644 --- a/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -public protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +public protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +public protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ open class URLSessionRequestBuilder: RequestBuilder { } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest() diff --git a/samples/client/petstore/swift5/readonlyProperties/PetstoreClient/Classes/OpenAPIs/Models.swift b/samples/client/petstore/swift5/readonlyProperties/PetstoreClient/Classes/OpenAPIs/Models.swift index 7094040c9f..3d9e25e363 100644 --- a/samples/client/petstore/swift5/readonlyProperties/PetstoreClient/Classes/OpenAPIs/Models.swift +++ b/samples/client/petstore/swift5/readonlyProperties/PetstoreClient/Classes/OpenAPIs/Models.swift @@ -112,9 +112,9 @@ open class Response { public final class RequestTask { private var lock = NSRecursiveLock() - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/samples/client/petstore/swift5/readonlyProperties/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/readonlyProperties/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index ec201fa099..f05a180874 100644 --- a/samples/client/petstore/swift5/readonlyProperties/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/readonlyProperties/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -public protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +public protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +public protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ open class URLSessionRequestBuilder: RequestBuilder { } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest() diff --git a/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift b/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift index 7094040c9f..3d9e25e363 100644 --- a/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift +++ b/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift @@ -112,9 +112,9 @@ open class Response { public final class RequestTask { private var lock = NSRecursiveLock() - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index ec201fa099..f05a180874 100644 --- a/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -public protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +public protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +public protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ open class URLSessionRequestBuilder: RequestBuilder { } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest() diff --git a/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift b/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift index 7094040c9f..3d9e25e363 100644 --- a/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift +++ b/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift @@ -112,9 +112,9 @@ open class Response { public final class RequestTask { private var lock = NSRecursiveLock() - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index ec201fa099..f05a180874 100644 --- a/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -public protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +public protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +public protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ open class URLSessionRequestBuilder: RequestBuilder { } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest() diff --git a/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/Models.swift b/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/Models.swift index 7094040c9f..3d9e25e363 100644 --- a/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/Models.swift +++ b/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/Models.swift @@ -112,9 +112,9 @@ open class Response { public final class RequestTask { private var lock = NSRecursiveLock() - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/URLSessionImplementations.swift b/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/URLSessionImplementations.swift index ec201fa099..f05a180874 100644 --- a/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/URLSessionImplementations.swift @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -public protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +public protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +public protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ open class URLSessionRequestBuilder: RequestBuilder { } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest() diff --git a/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/Models.swift b/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/Models.swift index 7094040c9f..3d9e25e363 100644 --- a/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/Models.swift +++ b/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/Models.swift @@ -112,9 +112,9 @@ open class Response { public final class RequestTask { private var lock = NSRecursiveLock() - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index ec201fa099..f05a180874 100644 --- a/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -public protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +public protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +public protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ open class URLSessionRequestBuilder: RequestBuilder { } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest() diff --git a/samples/client/petstore/swift5/x-swift-hashable/PetstoreClient/Classes/OpenAPIs/Models.swift b/samples/client/petstore/swift5/x-swift-hashable/PetstoreClient/Classes/OpenAPIs/Models.swift index 7094040c9f..3d9e25e363 100644 --- a/samples/client/petstore/swift5/x-swift-hashable/PetstoreClient/Classes/OpenAPIs/Models.swift +++ b/samples/client/petstore/swift5/x-swift-hashable/PetstoreClient/Classes/OpenAPIs/Models.swift @@ -112,9 +112,9 @@ open class Response { public final class RequestTask { private var lock = NSRecursiveLock() - private var task: URLSessionTask? + private var task: URLSessionDataTaskProtocol? - internal func set(task: URLSessionTask) { + internal func set(task: URLSessionDataTaskProtocol) { lock.lock() defer { lock.unlock() } self.task = task diff --git a/samples/client/petstore/swift5/x-swift-hashable/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/x-swift-hashable/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index ec201fa099..f05a180874 100644 --- a/samples/client/petstore/swift5/x-swift-hashable/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/x-swift-hashable/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -12,11 +12,33 @@ import MobileCoreServices import UniformTypeIdentifiers #endif -public protocol URLSessionProtocol { - func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask +// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since +// you may not want to create or return a real URLSessionDataTask. +public protocol URLSessionDataTaskProtocol { + func resume() + + var taskIdentifier: Int { get } + + var progress: Progress { get } + + func cancel() } -extension URLSession: URLSessionProtocol {} +// Protocol allowing implementations to alter what is returned or to test their implementations. +public protocol URLSessionProtocol { + // Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request + // is sent off when `.resume()` is called. + func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol +} + +extension URLSession: URLSessionProtocol { + // Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data. + public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol { + return dataTask(with: request, completionHandler: completionHandler) + } +} + +extension URLSessionDataTask: URLSessionDataTaskProtocol {} class URLSessionRequestBuilderFactory: RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type { @@ -136,7 +158,7 @@ open class URLSessionRequestBuilder: RequestBuilder { } } - let dataTask = urlSession.dataTask(with: request) { data, response, error in + let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in apiResponseQueue.async { self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion) cleanupRequest()