Skip to content

Commit 31c1a86

Browse files
authored
[swift6] Change JSONEncodable protocol to always return String (#20906)
* [swift6] Change JSONEncodable protocol to always return String * [swift6] Upgrade Alamofire * Add missing @sendable attributes * Fix APIHelperTests * Fix URLSessionImplementations
1 parent 1eb8c58 commit 31c1a86

File tree

592 files changed

+2453
-2798
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

592 files changed

+2453
-2798
lines changed

modules/openapi-generator/src/main/resources/swift6/APIHelper.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import Foundation{{#useVapor}}
88
import Vapor{{/useVapor}}
99

1010
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct APIHelper {
11-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
12-
let destination = source.reduce(into: [String: Any]()) { result, item in
11+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func rejectNil(_ source: [String: (any Sendable)?]) -> [String: any Sendable]? {
12+
let destination = source.reduce(into: [String: any Sendable]()) { result, item in
1313
if let value = item.value {
1414
result[item.key] = value
1515
}
@@ -21,7 +21,7 @@ import Vapor{{/useVapor}}
2121
return destination
2222
}
2323

24-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
24+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func rejectNilHeaders(_ source: [String: (any Sendable)?]) -> [String: String] {
2525
return source.reduce(into: [String: String]()) { result, item in
2626
if let collection = item.value as? [Any?] {
2727
result[item.key] = collection
@@ -33,12 +33,12 @@ import Vapor{{/useVapor}}
3333
}
3434
}
3535

36-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func convertBoolToString(_ source: [String: Any]?) -> [String: Any]? {
36+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func convertBoolToString(_ source: [String: any Sendable]?) -> [String: any Sendable]? {
3737
guard let source = source else {
3838
return nil
3939
}
4040

41-
return source.reduce(into: [String: Any]()) { result, item in
41+
return source.reduce(into: [String: any Sendable]()) { result, item in
4242
switch item.value {
4343
case let x as Bool:
4444
result[item.key] = x.description
@@ -100,7 +100,7 @@ import Vapor{{/useVapor}}
100100
/// maps all values from source to query parameters
101101
///
102102
/// collection values are always exploded
103-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
103+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValuesToQueryItems(_ source: [String: (any Sendable)?]) -> [URLQueryItem]? {
104104
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
105105
if let collection = item.value as? [Any?] {
106106
collection

modules/openapi-generator/src/main/resources/swift6/APIs.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ import Alamofire{{/useAlamofire}}
8585
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class RequestBuilder<T>: @unchecked Sendable {
8686
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var credential: URLCredential?
8787
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var headers: [String: String]
88-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let parameters: [String: Any]?
88+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let parameters: [String: any Sendable]?
8989
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let method: String
9090
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let URLString: String
9191
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let requestTask: RequestTask = RequestTask()
@@ -95,7 +95,7 @@ import Alamofire{{/useAlamofire}}
9595
/// Optional block to obtain a reference to the request's progress instance when available.
9696
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var onProgressReady: ((Progress) -> Void)?
9797

98-
required {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:], requiresAuthentication: Bool, apiConfiguration: {{projectName}}APIConfiguration = {{projectName}}APIConfiguration.shared) {
98+
required {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init(method: String, URLString: String, parameters: [String: any Sendable]?, headers: [String: String] = [:], requiresAuthentication: Bool, apiConfiguration: {{projectName}}APIConfiguration = {{projectName}}APIConfiguration.shared) {
9999
self.method = method
100100
self.URLString = URLString
101101
self.parameters = parameters
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{{#useAlamofire}}
2-
# TODO: Alamofire versions 5.10.0 and above are not currently supported. If you need a newer version, please consider submitting a Pull Request with the required changes.
3-
github "Alamofire/Alamofire" = 5.9.1{{/useAlamofire}}{{#usePromiseKit}}
2+
github "Alamofire/Alamofire" = 5.10.2{{/useAlamofire}}{{#usePromiseKit}}
43
github "mxcl/PromiseKit" ~> 8.1{{/usePromiseKit}}{{#useRxSwift}}
54
github "ReactiveX/RxSwift" ~> 6.8{{/useRxSwift}}

modules/openapi-generator/src/main/resources/swift6/Extensions.mustache

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,98 +11,74 @@ import FoundationNetworking
1111
@preconcurrency import PromiseKit{{/usePromiseKit}}{{#useVapor}}
1212
import Vapor{{/useVapor}}{{^useVapor}}
1313

14-
extension Bool: JSONEncodable {
15-
func encodeToJSON(codableHelper: CodableHelper) -> Any { self }
16-
}
17-
18-
extension Float: JSONEncodable {
19-
func encodeToJSON(codableHelper: CodableHelper) -> Any { self }
20-
}
21-
22-
extension Int: JSONEncodable {
23-
func encodeToJSON(codableHelper: CodableHelper) -> Any { self }
24-
}
25-
26-
extension Int32: JSONEncodable {
27-
func encodeToJSON(codableHelper: CodableHelper) -> Any { self }
28-
}
29-
30-
extension Int64: JSONEncodable {
31-
func encodeToJSON(codableHelper: CodableHelper) -> Any { self }
32-
}
33-
34-
extension Double: JSONEncodable {
35-
func encodeToJSON(codableHelper: CodableHelper) -> Any { self }
36-
}
37-
38-
extension Decimal: JSONEncodable {
39-
func encodeToJSON(codableHelper: CodableHelper) -> Any { self }
40-
}
41-
42-
extension String: JSONEncodable {
43-
func encodeToJSON(codableHelper: CodableHelper) -> Any { self }
44-
}
45-
46-
extension URL: JSONEncodable {
47-
func encodeToJSON(codableHelper: CodableHelper) -> Any { self }
48-
}
14+
extension Bool: QueryStringEncodable {}
15+
extension Float: QueryStringEncodable {}
16+
extension Int: QueryStringEncodable {}
17+
extension Int32: QueryStringEncodable {}
18+
extension Int64: QueryStringEncodable {}
19+
extension Double: QueryStringEncodable {}
20+
extension Decimal: QueryStringEncodable {}
21+
extension String: QueryStringEncodable {}
22+
extension URL: QueryStringEncodable {}
23+
extension UUID: QueryStringEncodable {}
4924

50-
extension UUID: JSONEncodable {
51-
func encodeToJSON(codableHelper: CodableHelper) -> Any { self }
25+
extension QueryStringEncodable {
26+
@_disfavoredOverload
27+
func encodeToQueryString(codableHelper: CodableHelper) -> String { String(describing: self) }
5228
}
5329

54-
extension RawRepresentable where RawValue: JSONEncodable {
55-
func encodeToJSON(codableHelper: CodableHelper) -> Any { return self.rawValue }
30+
extension RawRepresentable where RawValue: QueryStringEncodable {
31+
func encodeToQueryString(codableHelper: CodableHelper) -> String { String(describing: rawValue) }
5632
}
5733

58-
private func encodeIfPossible<T>(_ object: T, codableHelper: CodableHelper) -> Any {
59-
if let encodableObject = object as? JSONEncodable {
60-
return encodableObject.encodeToJSON(codableHelper: codableHelper)
34+
private func encodeIfPossible<T>(_ object: T, codableHelper: CodableHelper) -> String {
35+
if let encodableObject = object as? QueryStringEncodable {
36+
return encodableObject.encodeToQueryString(codableHelper: codableHelper)
6137
} else {
62-
return object
38+
return String(describing: object)
6339
}
6440
}
6541

66-
extension Array: JSONEncodable {
67-
func encodeToJSON(codableHelper: CodableHelper) -> Any {
42+
extension Array {
43+
func encodeToQueryString(codableHelper: CodableHelper) -> [String] {
6844
return self.map { encodeIfPossible($0, codableHelper: codableHelper) }
6945
}
7046
}
7147

72-
extension Set: JSONEncodable {
73-
func encodeToJSON(codableHelper: CodableHelper) -> Any {
74-
return Array(self).encodeToJSON(codableHelper: codableHelper)
48+
extension Set {
49+
func encodeToQueryString(codableHelper: CodableHelper) -> [String] {
50+
return Array(self).encodeToQueryString(codableHelper: codableHelper)
7551
}
7652
}
7753

78-
extension Dictionary: JSONEncodable {
79-
func encodeToJSON(codableHelper: CodableHelper) -> Any {
80-
var dictionary = [AnyHashable: Any]()
54+
extension Dictionary {
55+
func encodeToQueryString(codableHelper: CodableHelper) -> [Key: String] {
56+
var dictionary = [Key: String]()
8157
for (key, value) in self {
8258
dictionary[key] = encodeIfPossible(value, codableHelper: codableHelper)
8359
}
8460
return dictionary
8561
}
8662
}
8763

88-
extension Data: JSONEncodable {
89-
func encodeToJSON(codableHelper: CodableHelper) -> Any {
64+
extension Data: QueryStringEncodable {
65+
func encodeToQueryString(codableHelper: CodableHelper) -> String {
9066
return self.base64EncodedString(options: Data.Base64EncodingOptions())
9167
}
9268
}
9369

94-
extension Date: JSONEncodable {
95-
func encodeToJSON(codableHelper: CodableHelper) -> Any {
70+
extension Date: QueryStringEncodable {
71+
func encodeToQueryString(codableHelper: CodableHelper) -> String {
9672
return codableHelper.dateFormatter.string(from: self)
9773
}
9874
}
9975

100-
extension JSONEncodable where Self: Encodable {
101-
func encodeToJSON(codableHelper: CodableHelper) -> Any {
76+
extension QueryStringEncodable where Self: Encodable {
77+
func encodeToQueryString(codableHelper: CodableHelper) -> String {
10278
guard let data = try? codableHelper.jsonEncoder.encode(self) else {
10379
fatalError("Could not encode to json: \(self)")
10480
}
105-
return data.encodeToJSON(codableHelper: codableHelper)
81+
return data.encodeToQueryString(codableHelper: codableHelper)
10682
}
10783
}{{/useVapor}}{{#generateModelAdditionalProperties}}
10884

modules/openapi-generator/src/main/resources/swift6/JSONDataEncoding.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010
import FoundationNetworking
1111
#endif
1212

13-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct JSONDataEncoding {
13+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct JSONDataEncoding: Sendable {
1414
1515
// MARK: Properties
1616
@@ -27,8 +27,8 @@ import FoundationNetworking
2727
/// - throws: An `Error` if the encoding process encounters an error.
2828
///
2929
/// - returns: The encoded request.
30-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func encode(_ urlRequest: URLRequest, with parameters: [String: Any]?) -> URLRequest {
31-
var urlRequest = urlRequest
30+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func encode(request: URLRequest, with parameters: [String: any Sendable]?) -> URLRequest {
31+
var urlRequest = request
3232
3333
guard let jsonData = parameters?[JSONDataEncoding.jsonDataKey] as? Data, !jsonData.isEmpty else {
3434
return urlRequest
@@ -43,10 +43,10 @@ import FoundationNetworking
4343
return urlRequest
4444
}
4545

46-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func encodingParameters(jsonData: Data?) -> [String: Any]? {
47-
var returnedParams: [String: Any]?
46+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func encodingParameters(jsonData: Data?) -> [String: any Sendable]? {
47+
var returnedParams: [String: any Sendable]?
4848
if let jsonData = jsonData, !jsonData.isEmpty {
49-
var params: [String: Any] = [:]
49+
var params: [String: any Sendable] = [:]
5050
params[jsonDataKey] = jsonData
5151
returnedParams = params
5252
}

modules/openapi-generator/src/main/resources/swift6/JSONEncodingHelper.mustache

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

1010
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class JSONEncodingHelper {
1111
12-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class func encodingParameters<T: Encodable>(forEncodableObject encodableObj: T?, codableHelper: CodableHelper) -> [String: Any]? {
13-
var params: [String: Any]?
12+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class func encodingParameters<T: Encodable>(forEncodableObject encodableObj: T?, codableHelper: CodableHelper) -> [String: any Sendable]? {
13+
var params: [String: any Sendable]?
1414
1515
// Encode the Encodable object
1616
if let encodableObj = encodableObj {
@@ -26,8 +26,8 @@ import Foundation
2626
return params
2727
}
2828

29-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class func encodingParameters(forEncodableObject encodableObj: Any?, codableHelper: CodableHelper) -> [String: Any]? {
30-
var params: [String: Any]?
29+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class func encodingParameters(forEncodableObject encodableObj: Any?, codableHelper: CodableHelper) -> [String: any Sendable]? {
30+
var params: [String: any Sendable]?
3131
3232
if let encodableObj = encodableObj {
3333
do {

modules/openapi-generator/src/main/resources/swift6/Models.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import FoundationNetworking
1010
#endif{{#useAlamofire}}
1111
import Alamofire{{/useAlamofire}}
1212

13-
protocol JSONEncodable {
14-
func encodeToJSON(codableHelper: CodableHelper) -> Any
13+
protocol QueryStringEncodable {
14+
func encodeToQueryString(codableHelper: CodableHelper) -> String
1515
}
1616

1717
/// An enum where the last case value can be used as a default catch-all.
@@ -119,9 +119,9 @@ extension Response : Sendable where T : Sendable {}{{#useAlamofire}}
119119
/// Type-erased ResponseSerializer
120120
///
121121
/// This is needed in order to use `ResponseSerializer` as a Type in `Configuration`. Obsolete with `any` keyword in Swift >= 5.7
122-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct AnyResponseSerializer<T>: ResponseSerializer {
122+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct AnyResponseSerializer<T: Sendable>: ResponseSerializer {
123123
124-
let _serialize: (URLRequest?, HTTPURLResponse?, Data?, Error?) throws -> T
124+
let _serialize: @Sendable (URLRequest?, HTTPURLResponse?, Data?, Error?) throws -> T
125125
126126
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init<V: ResponseSerializer>(_ delegatee: V) where V.SerializedObject == T {
127127
_serialize = delegatee.serialize

modules/openapi-generator/src/main/resources/swift6/OpenAPIDateWithoutTime.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ import Foundation
7676
}
7777
}
7878

79-
extension OpenAPIDateWithoutTime: JSONEncodable {
80-
func encodeToJSON(codableHelper: CodableHelper) -> Any {
79+
extension OpenAPIDateWithoutTime: QueryStringEncodable {
80+
func encodeToQueryString(codableHelper: CodableHelper) -> String {
8181
return OpenISO8601DateFormatter.withoutTime.string(from: self.normalizedWrappedDate())
8282
}
8383
}

modules/openapi-generator/src/main/resources/swift6/Package.swift.mustache

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ let package = Package(
2525
dependencies: [
2626
// Dependencies declare other packages that this package depends on.
2727
{{#useAlamofire}}
28-
// TODO: Alamofire versions 5.10.0 and above are not currently supported. If you need a newer version, please consider submitting a Pull Request with the required changes.
29-
.package(url: "https://github.com/Alamofire/Alamofire", exact: "5.9.1"),
28+
.package(url: "https://github.com/Alamofire/Alamofire", exact: "5.10.2"),
3029
{{/useAlamofire}}
3130
{{#usePromiseKit}}
3231
.package(url: "https://github.com/mxcl/PromiseKit", .upToNextMajor(from: "8.1.2")),

modules/openapi-generator/src/main/resources/swift6/Podspec.mustache

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ Pod::Spec.new do |s|
2727
{{/podDocumentationURL}}
2828
s.source_files = '{{swiftPackagePath}}{{^swiftPackagePath}}{{#useSPMFileStructure}}Sources/{{projectName}}{{/useSPMFileStructure}}{{^useSPMFileStructure}}{{projectName}}/Classes{{/useSPMFileStructure}}{{/swiftPackagePath}}/**/*.swift'
2929
{{#useAlamofire}}
30-
# TODO: Alamofire versions 5.10.0 and above are not currently supported. If you need a newer version, please consider submitting a Pull Request with the required changes.
31-
s.dependency 'Alamofire', '5.9.1'
30+
s.dependency 'Alamofire', '5.10.2'
3231
{{/useAlamofire}}
3332
{{#usePromiseKit}}
3433
s.dependency 'PromiseKit/CorePromise', '~> 8.1'

0 commit comments

Comments
 (0)