Skip to content

Commit

Permalink
Fix swift4 1406 (OpenAPITools#1407)
Browse files Browse the repository at this point in the history
* Fix warnings produced when using Swift 4.2

* Update Petstore client for Swift 4
  • Loading branch information
james-rant authored and Daiki Matsudate committed Nov 10, 2018
1 parent 69878c7 commit 627a9dd
Show file tree
Hide file tree
Showing 57 changed files with 774 additions and 455 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import Foundation

open class {{projectName}}API {
open static var basePath = "{{{basePath}}}"
open static var credential: URLCredential?
open static var customHeaders: [String:String] = [:]
open static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
public static var basePath = "{{{basePath}}}"
public static var credential: URLCredential?
public static var customHeaders: [String:String] = [:]
public static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
}

open class RequestBuilder<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
if stringResponse.result.isFailure {
completion(
nil,
ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error as Error!)
ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error!)
)
return
}
Expand Down Expand Up @@ -356,7 +356,7 @@ open class AlamofireDecodableRequestBuilder<T:Decodable>: AlamofireRequestBuilde
if stringResponse.result.isFailure {
completion(
nil,
ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error as Error!)
ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error!)
)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public typealias EncodeResult = (data: Data?, error: Error?)

open class CodableHelper {
open static var dateformatter: DateFormatter?
public static var dateformatter: DateFormatter?
open class func decode<T>(_ type: T.Type, from data: Data) -> (decodableObj: T?, error: Error?) where T : Decodable {
var returnedDecodable: T? = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ open class Configuration {
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
// You must set it prior to encoding any dates, and it will only be read once.
open static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public enum ErrorResponse : Error {
}

open class Response<T> {
open let statusCode: Int
open let header: [String: String]
open let body: T?
public let statusCode: Int
public let header: [String: String]
public let body: T?
public init(statusCode: Int, header: [String: String], body: T?) {
self.statusCode = statusCode
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.0-SNAPSHOT
3.3.3-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import Foundation

public struct APIHelper {
public static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
let destination = source.reduce(into: [String: Any]()) { result, item in
public static func rejectNil(_ source: [String:Any?]) -> [String:Any]? {
let destination = source.reduce(into: [String: Any]()) { (result, item) in
if let value = item.value {
result[item.key] = value
}
Expand All @@ -20,22 +20,22 @@ public struct APIHelper {
return destination
}

public static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
return source.reduce(into: [String: String]()) { result, item in
public static func rejectNilHeaders(_ source: [String:Any?]) -> [String:String] {
return source.reduce(into: [String: String]()) { (result, item) in
if let collection = item.value as? Array<Any?> {
result[item.key] = collection.filter({ $0 != nil }).map { "\($0!)" }.joined(separator: ",")
result[item.key] = collection.filter({ $0 != nil }).map{ "\($0!)" }.joined(separator: ",")
} else if let value: Any = item.value {
result[item.key] = "\(value)"
}
}
}

public static func convertBoolToString(_ source: [String: Any]?) -> [String: Any]? {
public static func convertBoolToString(_ source: [String: Any]?) -> [String:Any]? {
guard let source = source else {
return nil
}

return source.reduce(into: [String: Any](), { result, item in
return source.reduce(into: [String: Any](), { (result, item) in
switch item.value {
case let x as Bool:
result[item.key] = x.description
Expand All @@ -45,10 +45,11 @@ public struct APIHelper {
})
}

public static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
let destination = source.filter({ $0.value != nil }).reduce(into: [URLQueryItem]()) { result, item in

public static func mapValuesToQueryItems(_ source: [String:Any?]) -> [URLQueryItem]? {
let destination = source.filter({ $0.value != nil}).reduce(into: [URLQueryItem]()) { (result, item) in
if let collection = item.value as? Array<Any?> {
let value = collection.filter({ $0 != nil }).map({ "\($0!)" }).joined(separator: ",")
let value = collection.filter({ $0 != nil }).map({"\($0!)"}).joined(separator: ",")
result.append(URLQueryItem(name: item.key, value: value))
} else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)"))
Expand All @@ -61,3 +62,4 @@ public struct APIHelper {
return destination
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
import Foundation

open class PetstoreClientAPI {
open static var basePath = "http://petstore.swagger.io:80/v2"
open static var credential: URLCredential?
open static var customHeaders: [String: String] = [:]
open static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
public static var basePath = "http://petstore.swagger.io:80/v2"
public static var credential: URLCredential?
public static var customHeaders: [String:String] = [:]
public static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
}

open class RequestBuilder<T> {
var credential: URLCredential?
var headers: [String: String]
public let parameters: [String: Any]?
var headers: [String:String]
public let parameters: [String:Any]?
public let isBody: Bool
public let method: String
public let URLString: String

/// Optional block to obtain a reference to the request's progress instance when available.
public var onProgressReady: ((Progress) -> Void)?
public var onProgressReady: ((Progress) -> ())?

public required init(method: String, URLString: String, parameters: [String: Any]?, isBody: Bool, headers: [String: String] = [:]) {
required public init(method: String, URLString: String, parameters: [String:Any]?, isBody: Bool, headers: [String:String] = [:]) {
self.method = method
self.URLString = URLString
self.parameters = parameters
Expand All @@ -34,13 +34,13 @@ open class RequestBuilder<T> {
addHeaders(PetstoreClientAPI.customHeaders)
}

open func addHeaders(_ aHeaders: [String: String]) {
open func addHeaders(_ aHeaders:[String:String]) {
for (header, value) in aHeaders {
headers[header] = value
}
}

open func execute(_: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) {}
open func execute(_ completion: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) { }

public func addHeader(name: String, value: String) -> Self {
if !value.isEmpty {
Expand All @@ -50,12 +50,12 @@ open class RequestBuilder<T> {
}

open func addCredential() -> Self {
credential = PetstoreClientAPI.credential
self.credential = PetstoreClientAPI.credential
return self
}
}

public protocol RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type
func getBuilder<T:Decodable>() -> RequestBuilder<T>.Type
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@
// https://openapi-generator.tech
//

import Alamofire
import Foundation
import Alamofire



open class AnotherFakeAPI {
/**
To test special tags
- parameter client: (body) client model
- parameter client: (body) client model
- parameter completion: completion handler to receive the data and the error objects
*/
open class func call123testSpecialTags(client: Client, completion: @escaping ((_ data: Client?, _ error: Error?) -> Void)) {
open class func call123testSpecialTags(client: Client, completion: @escaping ((_ data: Client?,_ error: Error?) -> Void)) {
call123testSpecialTagsWithRequestBuilder(client: client).execute { (response, error) -> Void in
completion(response?.body, error)
}
}


/**
To test special tags
- PATCH /another-fake/dummy
- To test special tags and operation ID starting with number
- parameter client: (body) client model
- returns: RequestBuilder<Client>
- parameter client: (body) client model
- returns: RequestBuilder<Client>
*/
open class func call123testSpecialTagsWithRequestBuilder(client: Client) -> RequestBuilder<Client> {
let path = "/another-fake/dummy"
Expand All @@ -39,4 +42,5 @@ open class AnotherFakeAPI {

return requestBuilder.init(method: "PATCH", URLString: (url?.string ?? URLString), parameters: parameters, isBody: true)
}

}
Loading

0 comments on commit 627a9dd

Please sign in to comment.