Skip to content

Commit eecd30c

Browse files
authored
[swift5] Fix target SDKs for Combine option (#8476)
* Update min sdks when Combine is used * Update samples * Revert "Update min sdks when Combine is used" This reverts commit e88b8ab. * Wrap import combine with canImport directive * Update samples * Wrap functions using Combine with canImport because of compiler error on archive * Update samples * Remove unnecessary newline and update samles
1 parent ac59ab9 commit eecd30c

30 files changed

Lines changed: 309 additions & 223 deletions

File tree

modules/openapi-generator/src/main/resources/swift5/api.mustache

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import Foundation{{#usePromiseKit}}
99
import PromiseKit{{/usePromiseKit}}{{#useRxSwift}}
1010
import RxSwift{{/useRxSwift}}{{#useCombine}}
11-
import Combine{{/useCombine}}{{#swiftUseApiNamespace}}
11+
#if canImport(Combine)
12+
import Combine
13+
#endif{{/useCombine}}{{#swiftUseApiNamespace}}
1214

1315
extension {{projectName}}API {
1416
{{/swiftUseApiNamespace}}
@@ -143,6 +145,7 @@ extension {{projectName}}API {
143145
{{#isDeprecated}}
144146
@available(*, deprecated, message: "This operation is deprecated.")
145147
{{/isDeprecated}}
148+
#if canImport(Combine)
146149
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
147150
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class func {{operationId}}({{#allParams}}{{paramName}}: {{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}{{{datatypeWithEnum}}}_{{operationId}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}? = nil{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}apiResponseQueue: DispatchQueue = {{projectName}}API.apiResponseQueue) -> AnyPublisher<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}, Error> {
148151
return Future<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}, Error>.init { promise in
@@ -162,6 +165,7 @@ extension {{projectName}}API {
162165
}
163166
}.eraseToAnyPublisher()
164167
}
168+
#endif
165169
{{/useCombine}}
166170
{{#useResult}}
167171
/**

samples/client/petstore/swift5/alamofireLibrary/PetstoreClient/Classes/OpenAPIs/AlamofireImplementations.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import Alamofire
99

1010
class AlamofireRequestBuilderFactory: RequestBuilderFactory {
1111
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
12-
AlamofireRequestBuilder<T>.self
12+
return AlamofireRequestBuilder<T>.self
1313
}
1414

1515
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type {
16-
AlamofireDecodableRequestBuilder<T>.self
16+
return AlamofireDecodableRequestBuilder<T>.self
1717
}
1818
}
1919

@@ -65,15 +65,15 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
6565
the file extension). Return the desired Content-Type otherwise.
6666
*/
6767
open func contentTypeForFormPart(fileURL: URL) -> String? {
68-
nil
68+
return nil
6969
}
7070

7171
/**
7272
May be overridden by a subclass if you want to control the request
7373
configuration (e.g. to override the cache policy).
7474
*/
7575
open func makeRequest(manager: SessionManager, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) -> DataRequest {
76-
manager.request(URLString, method: method, parameters: parameters, encoding: encoding, headers: headers)
76+
return manager.request(URLString, method: method, parameters: parameters, encoding: encoding, headers: headers)
7777
}
7878

7979
override open func execute(_ apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue, _ completion: @escaping (_ result: Swift.Result<Response<T>, Error>) -> Void) {

samples/client/petstore/swift5/alamofireLibrary/PetstoreClient/Classes/OpenAPIs/Extensions.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,35 @@
77
import Foundation
88

99
extension Bool: JSONEncodable {
10-
func encodeToJSON() -> Any { self as Any }
10+
func encodeToJSON() -> Any { return self as Any }
1111
}
1212

1313
extension Float: JSONEncodable {
14-
func encodeToJSON() -> Any { self as Any }
14+
func encodeToJSON() -> Any { return self as Any }
1515
}
1616

1717
extension Int: JSONEncodable {
18-
func encodeToJSON() -> Any { self as Any }
18+
func encodeToJSON() -> Any { return self as Any }
1919
}
2020

2121
extension Int32: JSONEncodable {
22-
func encodeToJSON() -> Any { NSNumber(value: self as Int32) }
22+
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
2323
}
2424

2525
extension Int64: JSONEncodable {
26-
func encodeToJSON() -> Any { NSNumber(value: self as Int64) }
26+
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
2727
}
2828

2929
extension Double: JSONEncodable {
30-
func encodeToJSON() -> Any { self as Any }
30+
func encodeToJSON() -> Any { return self as Any }
3131
}
3232

3333
extension String: JSONEncodable {
34-
func encodeToJSON() -> Any { self as Any }
34+
func encodeToJSON() -> Any { return self as Any }
3535
}
3636

3737
extension RawRepresentable where RawValue: JSONEncodable {
38-
func encodeToJSON() -> Any { self.rawValue as Any }
38+
func encodeToJSON() -> Any { return self.rawValue as Any }
3939
}
4040

4141
private func encodeIfPossible<T>(_ object: T) -> Any {
@@ -48,7 +48,7 @@ private func encodeIfPossible<T>(_ object: T) -> Any {
4848

4949
extension Array: JSONEncodable {
5050
func encodeToJSON() -> Any {
51-
self.map(encodeIfPossible)
51+
return self.map(encodeIfPossible)
5252
}
5353
}
5454

@@ -64,44 +64,44 @@ extension Dictionary: JSONEncodable {
6464

6565
extension Data: JSONEncodable {
6666
func encodeToJSON() -> Any {
67-
self.base64EncodedString(options: Data.Base64EncodingOptions())
67+
return self.base64EncodedString(options: Data.Base64EncodingOptions())
6868
}
6969
}
7070

7171
extension Date: JSONEncodable {
7272
func encodeToJSON() -> Any {
73-
CodableHelper.dateFormatter.string(from: self) as Any
73+
return CodableHelper.dateFormatter.string(from: self) as Any
7474
}
7575
}
7676

7777
extension URL: JSONEncodable {
7878
func encodeToJSON() -> Any {
79-
self
79+
return self
8080
}
8181
}
8282

8383
extension UUID: JSONEncodable {
8484
func encodeToJSON() -> Any {
85-
uuidString
85+
return self.uuidString
8686
}
8787
}
8888

8989
extension String: CodingKey {
9090

9191
public var stringValue: String {
92-
self
92+
return self
9393
}
9494

9595
public init?(stringValue: String) {
9696
self.init(stringLiteral: stringValue)
9797
}
9898

9999
public var intValue: Int? {
100-
nil
100+
return nil
101101
}
102102

103103
public init?(intValue: Int) {
104-
nil
104+
return nil
105105
}
106106

107107
}
@@ -174,6 +174,6 @@ extension KeyedDecodingContainerProtocol {
174174

175175
extension HTTPURLResponse {
176176
var isStatusCodeSuccessful: Bool {
177-
Array(200 ..< 300).contains(statusCode)
177+
return Array(200 ..< 300).contains(statusCode)
178178
}
179179
}

samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/APIs/AnotherFakeAPI.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
//
77

88
import Foundation
9+
#if canImport(Combine)
910
import Combine
11+
#endif
1012

1113
open class AnotherFakeAPI {
1214
/**
@@ -16,6 +18,7 @@ open class AnotherFakeAPI {
1618
- parameter apiResponseQueue: The queue on which api response is dispatched.
1719
- returns: AnyPublisher<Client, Error>
1820
*/
21+
#if canImport(Combine)
1922
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
2023
open class func call123testSpecialTags(body: Client, apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue) -> AnyPublisher<Client, Error> {
2124
return Future<Client, Error>.init { promise in
@@ -29,6 +32,7 @@ open class AnotherFakeAPI {
2932
}
3033
}.eraseToAnyPublisher()
3134
}
35+
#endif
3236

3337
/**
3438
To test special tags

samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/APIs/FakeAPI.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
//
77

88
import Foundation
9+
#if canImport(Combine)
910
import Combine
11+
#endif
1012

1113
open class FakeAPI {
1214
/**
@@ -15,6 +17,7 @@ open class FakeAPI {
1517
- parameter apiResponseQueue: The queue on which api response is dispatched.
1618
- returns: AnyPublisher<Bool, Error>
1719
*/
20+
#if canImport(Combine)
1821
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
1922
open class func fakeOuterBooleanSerialize(body: Bool? = nil, apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue) -> AnyPublisher<Bool, Error> {
2023
return Future<Bool, Error>.init { promise in
@@ -28,6 +31,7 @@ open class FakeAPI {
2831
}
2932
}.eraseToAnyPublisher()
3033
}
34+
#endif
3135

3236
/**
3337
- POST /fake/outer/boolean
@@ -59,6 +63,7 @@ open class FakeAPI {
5963
- parameter apiResponseQueue: The queue on which api response is dispatched.
6064
- returns: AnyPublisher<OuterComposite, Error>
6165
*/
66+
#if canImport(Combine)
6267
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
6368
open class func fakeOuterCompositeSerialize(body: OuterComposite? = nil, apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue) -> AnyPublisher<OuterComposite, Error> {
6469
return Future<OuterComposite, Error>.init { promise in
@@ -72,6 +77,7 @@ open class FakeAPI {
7277
}
7378
}.eraseToAnyPublisher()
7479
}
80+
#endif
7581

7682
/**
7783
- POST /fake/outer/composite
@@ -103,6 +109,7 @@ open class FakeAPI {
103109
- parameter apiResponseQueue: The queue on which api response is dispatched.
104110
- returns: AnyPublisher<Double, Error>
105111
*/
112+
#if canImport(Combine)
106113
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
107114
open class func fakeOuterNumberSerialize(body: Double? = nil, apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue) -> AnyPublisher<Double, Error> {
108115
return Future<Double, Error>.init { promise in
@@ -116,6 +123,7 @@ open class FakeAPI {
116123
}
117124
}.eraseToAnyPublisher()
118125
}
126+
#endif
119127

120128
/**
121129
- POST /fake/outer/number
@@ -147,6 +155,7 @@ open class FakeAPI {
147155
- parameter apiResponseQueue: The queue on which api response is dispatched.
148156
- returns: AnyPublisher<String, Error>
149157
*/
158+
#if canImport(Combine)
150159
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
151160
open class func fakeOuterStringSerialize(body: String? = nil, apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue) -> AnyPublisher<String, Error> {
152161
return Future<String, Error>.init { promise in
@@ -160,6 +169,7 @@ open class FakeAPI {
160169
}
161170
}.eraseToAnyPublisher()
162171
}
172+
#endif
163173

164174
/**
165175
- POST /fake/outer/string
@@ -191,6 +201,7 @@ open class FakeAPI {
191201
- parameter apiResponseQueue: The queue on which api response is dispatched.
192202
- returns: AnyPublisher<Void, Error>
193203
*/
204+
#if canImport(Combine)
194205
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
195206
open class func testBodyWithFileSchema(body: FileSchemaTestClass, apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue) -> AnyPublisher<Void, Error> {
196207
return Future<Void, Error>.init { promise in
@@ -204,6 +215,7 @@ open class FakeAPI {
204215
}
205216
}.eraseToAnyPublisher()
206217
}
218+
#endif
207219

208220
/**
209221
- PUT /fake/body-with-file-schema
@@ -236,6 +248,7 @@ open class FakeAPI {
236248
- parameter apiResponseQueue: The queue on which api response is dispatched.
237249
- returns: AnyPublisher<Void, Error>
238250
*/
251+
#if canImport(Combine)
239252
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
240253
open class func testBodyWithQueryParams(query: String, body: User, apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue) -> AnyPublisher<Void, Error> {
241254
return Future<Void, Error>.init { promise in
@@ -249,6 +262,7 @@ open class FakeAPI {
249262
}
250263
}.eraseToAnyPublisher()
251264
}
265+
#endif
252266

253267
/**
254268
- PUT /fake/body-with-query-params
@@ -284,6 +298,7 @@ open class FakeAPI {
284298
- parameter apiResponseQueue: The queue on which api response is dispatched.
285299
- returns: AnyPublisher<Client, Error>
286300
*/
301+
#if canImport(Combine)
287302
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
288303
open class func testClientModel(body: Client, apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue) -> AnyPublisher<Client, Error> {
289304
return Future<Client, Error>.init { promise in
@@ -297,6 +312,7 @@ open class FakeAPI {
297312
}
298313
}.eraseToAnyPublisher()
299314
}
315+
#endif
300316

301317
/**
302318
To test \"client\" model
@@ -343,6 +359,7 @@ open class FakeAPI {
343359
- parameter apiResponseQueue: The queue on which api response is dispatched.
344360
- returns: AnyPublisher<Void, Error>
345361
*/
362+
#if canImport(Combine)
346363
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
347364
open class func testEndpointParameters(number: Double, double: Double, patternWithoutDelimiter: String, byte: Data, integer: Int? = nil, int32: Int? = nil, int64: Int64? = nil, float: Float? = nil, string: String? = nil, binary: URL? = nil, date: Date? = nil, dateTime: Date? = nil, password: String? = nil, callback: String? = nil, apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue) -> AnyPublisher<Void, Error> {
348365
return Future<Void, Error>.init { promise in
@@ -356,6 +373,7 @@ open class FakeAPI {
356373
}
357374
}.eraseToAnyPublisher()
358375
}
376+
#endif
359377

360378
/**
361379
Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트
@@ -497,6 +515,7 @@ open class FakeAPI {
497515
- parameter apiResponseQueue: The queue on which api response is dispatched.
498516
- returns: AnyPublisher<Void, Error>
499517
*/
518+
#if canImport(Combine)
500519
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
501520
open class func testEnumParameters(enumHeaderStringArray: [String]? = nil, enumHeaderString: EnumHeaderString_testEnumParameters? = nil, enumQueryStringArray: [String]? = nil, enumQueryString: EnumQueryString_testEnumParameters? = nil, enumQueryInteger: EnumQueryInteger_testEnumParameters? = nil, enumQueryDouble: EnumQueryDouble_testEnumParameters? = nil, enumFormStringArray: [String]? = nil, enumFormString: EnumFormString_testEnumParameters? = nil, apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue) -> AnyPublisher<Void, Error> {
502521
return Future<Void, Error>.init { promise in
@@ -510,6 +529,7 @@ open class FakeAPI {
510529
}
511530
}.eraseToAnyPublisher()
512531
}
532+
#endif
513533

514534
/**
515535
To test enum parameters
@@ -569,6 +589,7 @@ open class FakeAPI {
569589
- parameter apiResponseQueue: The queue on which api response is dispatched.
570590
- returns: AnyPublisher<Void, Error>
571591
*/
592+
#if canImport(Combine)
572593
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
573594
open class func testGroupParameters(requiredStringGroup: Int, requiredBooleanGroup: Bool, requiredInt64Group: Int64, stringGroup: Int? = nil, booleanGroup: Bool? = nil, int64Group: Int64? = nil, apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue) -> AnyPublisher<Void, Error> {
574595
return Future<Void, Error>.init { promise in
@@ -582,6 +603,7 @@ open class FakeAPI {
582603
}
583604
}.eraseToAnyPublisher()
584605
}
606+
#endif
585607

586608
/**
587609
Fake endpoint to test group parameters (optional)
@@ -627,6 +649,7 @@ open class FakeAPI {
627649
- parameter apiResponseQueue: The queue on which api response is dispatched.
628650
- returns: AnyPublisher<Void, Error>
629651
*/
652+
#if canImport(Combine)
630653
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
631654
open class func testInlineAdditionalProperties(param: [String: String], apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue) -> AnyPublisher<Void, Error> {
632655
return Future<Void, Error>.init { promise in
@@ -640,6 +663,7 @@ open class FakeAPI {
640663
}
641664
}.eraseToAnyPublisher()
642665
}
666+
#endif
643667

644668
/**
645669
test inline additionalProperties
@@ -673,6 +697,7 @@ open class FakeAPI {
673697
- parameter apiResponseQueue: The queue on which api response is dispatched.
674698
- returns: AnyPublisher<Void, Error>
675699
*/
700+
#if canImport(Combine)
676701
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
677702
open class func testJsonFormData(param: String, param2: String, apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue) -> AnyPublisher<Void, Error> {
678703
return Future<Void, Error>.init { promise in
@@ -686,6 +711,7 @@ open class FakeAPI {
686711
}
687712
}.eraseToAnyPublisher()
688713
}
714+
#endif
689715

690716
/**
691717
test json serialization of form data

0 commit comments

Comments
 (0)