mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-03 17:00:43 +00:00
Fixes for swift4 language (#6116)
* Fix build error in Xcode 9 beta 3, as .compact is no longer defined * Add test schema for Swift 4 and associated script and config files * Add test app for swift4Test.json schema * Make integer, Integer, int, and Int32 types map to Swift Int type instead of Int32 type * Add CodingKeys to model template, which allows us to serialize/deserialize variable names that are different than property names * Make updates to Swift 4 test schema * Fixes for unit test app for swift4Test.json Swift 4 test schema
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
// APIHelper.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class APIHelper {
|
||||
static func rejectNil(_ source: [String:Any?]) -> [String:Any]? {
|
||||
var destination = [String:Any]()
|
||||
for (key, nillableValue) in source {
|
||||
if let value: Any = nillableValue {
|
||||
destination[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
if destination.isEmpty {
|
||||
return nil
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
static func rejectNilHeaders(_ source: [String:Any?]) -> [String:String] {
|
||||
var destination = [String:String]()
|
||||
for (key, nillableValue) in source {
|
||||
if let value: Any = nillableValue {
|
||||
destination[key] = "\(value)"
|
||||
}
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
static func convertBoolToString(_ source: [String: Any]?) -> [String:Any]? {
|
||||
guard let source = source else {
|
||||
return nil
|
||||
}
|
||||
var destination = [String:Any]()
|
||||
let theTrue = NSNumber(value: true as Bool)
|
||||
let theFalse = NSNumber(value: false as Bool)
|
||||
for (key, value) in source {
|
||||
switch value {
|
||||
case let x where x as? NSNumber === theTrue || x as? NSNumber === theFalse:
|
||||
destination[key] = "\(value as! Bool)" as Any?
|
||||
default:
|
||||
destination[key] = value
|
||||
}
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
|
||||
static func mapValuesToQueryItems(values: [String:Any?]) -> [URLQueryItem]? {
|
||||
let returnValues = values
|
||||
.filter { $0.1 != nil }
|
||||
.map { (item: (_key: String, _value: Any?)) -> URLQueryItem in
|
||||
URLQueryItem(name: item._key, value:"\(item._value!)")
|
||||
}
|
||||
if returnValues.count == 0 {
|
||||
return nil
|
||||
}
|
||||
return returnValues
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// APIs.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
open class TestClientAPI {
|
||||
open static var basePath = "http://api.example.com/basePath"
|
||||
open static var credential: URLCredential?
|
||||
open static var customHeaders: [String:String] = [:]
|
||||
open static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
|
||||
}
|
||||
|
||||
open class RequestBuilder<T> {
|
||||
var credential: URLCredential?
|
||||
var headers: [String:String]
|
||||
let parameters: [String:Any]?
|
||||
let isBody: Bool
|
||||
let method: String
|
||||
let URLString: String
|
||||
|
||||
/// Optional block to obtain a reference to the request's progress instance when available.
|
||||
public var onProgressReady: ((Progress) -> ())?
|
||||
|
||||
required public init(method: String, URLString: String, parameters: [String:Any]?, isBody: Bool, headers: [String:String] = [:]) {
|
||||
self.method = method
|
||||
self.URLString = URLString
|
||||
self.parameters = parameters
|
||||
self.isBody = isBody
|
||||
self.headers = headers
|
||||
|
||||
addHeaders(TestClientAPI.customHeaders)
|
||||
}
|
||||
|
||||
open func addHeaders(_ aHeaders:[String:String]) {
|
||||
for (header, value) in aHeaders {
|
||||
headers[header] = value
|
||||
}
|
||||
}
|
||||
|
||||
open func execute(_ completion: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) { }
|
||||
|
||||
public func addHeader(name: String, value: String) -> Self {
|
||||
if !value.isEmpty {
|
||||
headers[name] = value
|
||||
}
|
||||
return self
|
||||
}
|
||||
|
||||
open func addCredential() -> Self {
|
||||
self.credential = TestClientAPI.credential
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
public protocol RequestBuilderFactory {
|
||||
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type
|
||||
func getBuilder<T:Decodable>() -> RequestBuilder<T>.Type
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
//
|
||||
// Swift4TestAPI.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Alamofire
|
||||
|
||||
|
||||
|
||||
open class Swift4TestAPI {
|
||||
/**
|
||||
Get all of the models
|
||||
|
||||
- parameter clientId: (query) id that represent the Api client
|
||||
- parameter completion: completion handler to receive the data and the error objects
|
||||
*/
|
||||
open class func getAllModels(clientId: String, completion: @escaping ((_ data: GetAllModelsResult?,_ error: Error?) -> Void)) {
|
||||
getAllModelsWithRequestBuilder(clientId: clientId).execute { (response, error) -> Void in
|
||||
completion(response?.body, error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Get all of the models
|
||||
- GET /allModels
|
||||
- This endpoint tests get a dictionary which contains examples of all of the models.
|
||||
- examples: [{contentType=application/json, example={
|
||||
"myPrimitive" : {
|
||||
"myDateTimeArray" : [ "2000-01-23T04:56:07.000+00:00" ],
|
||||
"myStringArray" : [ "aeiou" ],
|
||||
"myFile" : "",
|
||||
"myFloatArray" : [ 2.302136 ],
|
||||
"myBytes" : "aeiou",
|
||||
"myLong" : 1,
|
||||
"myBooleanArray" : [ true ],
|
||||
"myDoubleArray" : [ 9.301444243932576 ],
|
||||
"myInteger" : 0,
|
||||
"myString" : "aeiou",
|
||||
"myBytesArray" : [ "aeiou" ],
|
||||
"myDouble" : 7.061401241503109,
|
||||
"myDate" : "2000-01-23",
|
||||
"myDateArray" : [ "2000-01-23" ],
|
||||
"myDateTime" : "2000-01-23T04:56:07.000+00:00",
|
||||
"myLongArray" : [ 5 ],
|
||||
"myIntegerArray" : [ 6 ],
|
||||
"myUUID" : "046b6c7f-0b8a-43b9-b35d-6489e6daee91",
|
||||
"myBoolean" : true,
|
||||
"myFileArray" : [ "" ],
|
||||
"myStringEnum" : { },
|
||||
"myFloat" : 5.637377,
|
||||
"myStringEnumArray" : [ null ],
|
||||
"myUUIDArray" : [ "046b6c7f-0b8a-43b9-b35d-6489e6daee91" ]
|
||||
},
|
||||
"myVariableNameTest" : {
|
||||
"for" : "aeiou",
|
||||
"example_name" : "aeiou"
|
||||
},
|
||||
"myPrimitiveArray" : [ {
|
||||
"myDateTimeArray" : [ "2000-01-23T04:56:07.000+00:00" ],
|
||||
"myStringArray" : [ "aeiou" ],
|
||||
"myFile" : "",
|
||||
"myFloatArray" : [ 2.302136 ],
|
||||
"myBytes" : "aeiou",
|
||||
"myLong" : 1,
|
||||
"myBooleanArray" : [ true ],
|
||||
"myDoubleArray" : [ 9.301444243932576 ],
|
||||
"myInteger" : 0,
|
||||
"myString" : "aeiou",
|
||||
"myBytesArray" : [ "aeiou" ],
|
||||
"myDouble" : 7.061401241503109,
|
||||
"myDate" : "2000-01-23",
|
||||
"myDateArray" : [ "2000-01-23" ],
|
||||
"myDateTime" : "2000-01-23T04:56:07.000+00:00",
|
||||
"myLongArray" : [ 5 ],
|
||||
"myIntegerArray" : [ 6 ],
|
||||
"myUUID" : "046b6c7f-0b8a-43b9-b35d-6489e6daee91",
|
||||
"myBoolean" : true,
|
||||
"myFileArray" : [ "" ],
|
||||
"myStringEnum" : { },
|
||||
"myFloat" : 5.637377,
|
||||
"myStringEnumArray" : [ null ],
|
||||
"myUUIDArray" : [ "046b6c7f-0b8a-43b9-b35d-6489e6daee91" ]
|
||||
} ]
|
||||
}}]
|
||||
|
||||
- parameter clientId: (query) id that represent the Api client
|
||||
|
||||
- returns: RequestBuilder<GetAllModelsResult>
|
||||
*/
|
||||
open class func getAllModelsWithRequestBuilder(clientId: String) -> RequestBuilder<GetAllModelsResult> {
|
||||
let path = "/allModels"
|
||||
let URLString = TestClientAPI.basePath + path
|
||||
let parameters: [String:Any]? = nil
|
||||
|
||||
let url = NSURLComponents(string: URLString)
|
||||
url?.queryItems = APIHelper.mapValuesToQueryItems(values:[
|
||||
"client_id": clientId
|
||||
])
|
||||
|
||||
|
||||
let requestBuilder: RequestBuilder<GetAllModelsResult>.Type = TestClientAPI.requestBuilderFactory.getBuilder()
|
||||
|
||||
return requestBuilder.init(method: "GET", URLString: (url?.string ?? URLString), parameters: parameters, isBody: false)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,307 @@
|
||||
// AlamofireImplementations.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Alamofire
|
||||
|
||||
class AlamofireRequestBuilderFactory: RequestBuilderFactory {
|
||||
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
|
||||
return AlamofireRequestBuilder<T>.self
|
||||
}
|
||||
|
||||
func getBuilder<T:Decodable>() -> RequestBuilder<T>.Type {
|
||||
return AlamofireDecodableRequestBuilder<T>.self
|
||||
}
|
||||
}
|
||||
|
||||
// Store manager to retain its reference
|
||||
private var managerStore: [String: Alamofire.SessionManager] = [:]
|
||||
|
||||
open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
required public init(method: String, URLString: String, parameters: [String : Any]?, isBody: Bool, headers: [String : String] = [:]) {
|
||||
super.init(method: method, URLString: URLString, parameters: parameters, isBody: isBody, headers: headers)
|
||||
}
|
||||
|
||||
/**
|
||||
May be overridden by a subclass if you want to control the session
|
||||
configuration.
|
||||
*/
|
||||
open func createSessionManager() -> Alamofire.SessionManager {
|
||||
let configuration = URLSessionConfiguration.default
|
||||
configuration.httpAdditionalHeaders = buildHeaders()
|
||||
return Alamofire.SessionManager(configuration: configuration)
|
||||
}
|
||||
|
||||
/**
|
||||
May be overridden by a subclass if you want to control the Content-Type
|
||||
that is given to an uploaded form part.
|
||||
|
||||
Return nil to use the default behavior (inferring the Content-Type from
|
||||
the file extension). Return the desired Content-Type otherwise.
|
||||
*/
|
||||
open func contentTypeForFormPart(fileURL: URL) -> String? {
|
||||
return nil
|
||||
}
|
||||
|
||||
/**
|
||||
May be overridden by a subclass if you want to control the request
|
||||
configuration (e.g. to override the cache policy).
|
||||
*/
|
||||
open func makeRequest(manager: SessionManager, method: HTTPMethod, encoding: ParameterEncoding, headers: [String:String]) -> DataRequest {
|
||||
return manager.request(URLString, method: method, parameters: parameters, encoding: encoding, headers: headers)
|
||||
}
|
||||
|
||||
override open func execute(_ completion: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) {
|
||||
let managerId:String = UUID().uuidString
|
||||
// Create a new manager for each request to customize its request header
|
||||
let manager = createSessionManager()
|
||||
managerStore[managerId] = manager
|
||||
|
||||
let encoding:ParameterEncoding = isBody ? JSONDataEncoding() : URLEncoding()
|
||||
|
||||
let xMethod = Alamofire.HTTPMethod(rawValue: method)
|
||||
let fileKeys = parameters == nil ? [] : parameters!.filter { $1 is NSURL }
|
||||
.map { $0.0 }
|
||||
|
||||
if fileKeys.count > 0 {
|
||||
manager.upload(multipartFormData: { mpForm in
|
||||
for (k, v) in self.parameters! {
|
||||
switch v {
|
||||
case let fileURL as URL:
|
||||
if let mimeType = self.contentTypeForFormPart(fileURL: fileURL) {
|
||||
mpForm.append(fileURL, withName: k, fileName: fileURL.lastPathComponent, mimeType: mimeType)
|
||||
}
|
||||
else {
|
||||
mpForm.append(fileURL, withName: k)
|
||||
}
|
||||
break
|
||||
case let string as String:
|
||||
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
|
||||
break
|
||||
case let number as NSNumber:
|
||||
mpForm.append(number.stringValue.data(using: String.Encoding.utf8)!, withName: k)
|
||||
break
|
||||
default:
|
||||
fatalError("Unprocessable value \(v) with key \(k)")
|
||||
break
|
||||
}
|
||||
}
|
||||
}, to: URLString, method: xMethod!, headers: nil, encodingCompletion: { encodingResult in
|
||||
switch encodingResult {
|
||||
case .success(let upload, _, _):
|
||||
if let onProgressReady = self.onProgressReady {
|
||||
onProgressReady(upload.uploadProgress)
|
||||
}
|
||||
self.processRequest(request: upload, managerId, completion)
|
||||
case .failure(let encodingError):
|
||||
completion(nil, ErrorResponse.Error(415, nil, encodingError))
|
||||
}
|
||||
})
|
||||
} else {
|
||||
let request = makeRequest(manager: manager, method: xMethod!, encoding: encoding, headers: headers)
|
||||
if let onProgressReady = self.onProgressReady {
|
||||
onProgressReady(request.progress)
|
||||
}
|
||||
processRequest(request: request, managerId, completion)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fileprivate func processRequest(request: DataRequest, _ managerId: String, _ completion: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) {
|
||||
if let credential = self.credential {
|
||||
request.authenticate(usingCredential: credential)
|
||||
}
|
||||
|
||||
let cleanupRequest = {
|
||||
_ = managerStore.removeValue(forKey: managerId)
|
||||
}
|
||||
|
||||
let validatedRequest = request.validate()
|
||||
|
||||
switch T.self {
|
||||
case is String.Type:
|
||||
validatedRequest.responseString(completionHandler: { (stringResponse) in
|
||||
cleanupRequest()
|
||||
|
||||
if stringResponse.result.isFailure {
|
||||
completion(
|
||||
nil,
|
||||
ErrorResponse.Error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error as Error!)
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
completion(
|
||||
Response(
|
||||
response: stringResponse.response!,
|
||||
body: ((stringResponse.result.value ?? "") as! T)
|
||||
),
|
||||
nil
|
||||
)
|
||||
})
|
||||
case is Void.Type:
|
||||
validatedRequest.responseData(completionHandler: { (voidResponse) in
|
||||
cleanupRequest()
|
||||
|
||||
if voidResponse.result.isFailure {
|
||||
completion(
|
||||
nil,
|
||||
ErrorResponse.Error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!)
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
completion(
|
||||
Response(
|
||||
response: voidResponse.response!,
|
||||
body: nil),
|
||||
nil
|
||||
)
|
||||
})
|
||||
default:
|
||||
validatedRequest.responseData(completionHandler: { (dataResponse) in
|
||||
cleanupRequest()
|
||||
|
||||
if (dataResponse.result.isFailure) {
|
||||
completion(
|
||||
nil,
|
||||
ErrorResponse.Error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!)
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
completion(
|
||||
Response(
|
||||
response: dataResponse.response!,
|
||||
body: (dataResponse.data as! T)
|
||||
),
|
||||
nil
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
open func buildHeaders() -> [String: String] {
|
||||
var httpHeaders = SessionManager.defaultHTTPHeaders
|
||||
for (key, value) in self.headers {
|
||||
httpHeaders[key] = value
|
||||
}
|
||||
return httpHeaders
|
||||
}
|
||||
}
|
||||
|
||||
public enum AlamofireDecodableRequestBuilderError: Error {
|
||||
case emptyDataResponse
|
||||
case nilHTTPResponse
|
||||
case jsonDecoding(DecodingError)
|
||||
case generalError(Error)
|
||||
}
|
||||
|
||||
open class AlamofireDecodableRequestBuilder<T:Decodable>: AlamofireRequestBuilder<T> {
|
||||
|
||||
override fileprivate func processRequest(request: DataRequest, _ managerId: String, _ completion: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) {
|
||||
if let credential = self.credential {
|
||||
request.authenticate(usingCredential: credential)
|
||||
}
|
||||
|
||||
let cleanupRequest = {
|
||||
_ = managerStore.removeValue(forKey: managerId)
|
||||
}
|
||||
|
||||
let validatedRequest = request.validate()
|
||||
|
||||
switch T.self {
|
||||
case is String.Type:
|
||||
validatedRequest.responseString(completionHandler: { (stringResponse) in
|
||||
cleanupRequest()
|
||||
|
||||
if stringResponse.result.isFailure {
|
||||
completion(
|
||||
nil,
|
||||
ErrorResponse.Error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error as Error!)
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
completion(
|
||||
Response(
|
||||
response: stringResponse.response!,
|
||||
body: ((stringResponse.result.value ?? "") as! T)
|
||||
),
|
||||
nil
|
||||
)
|
||||
})
|
||||
case is Void.Type:
|
||||
validatedRequest.responseData(completionHandler: { (voidResponse) in
|
||||
cleanupRequest()
|
||||
|
||||
if voidResponse.result.isFailure {
|
||||
completion(
|
||||
nil,
|
||||
ErrorResponse.Error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!)
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
completion(
|
||||
Response(
|
||||
response: voidResponse.response!,
|
||||
body: nil),
|
||||
nil
|
||||
)
|
||||
})
|
||||
case is Data.Type:
|
||||
validatedRequest.responseData(completionHandler: { (dataResponse) in
|
||||
cleanupRequest()
|
||||
|
||||
if (dataResponse.result.isFailure) {
|
||||
completion(
|
||||
nil,
|
||||
ErrorResponse.Error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!)
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
completion(
|
||||
Response(
|
||||
response: dataResponse.response!,
|
||||
body: (dataResponse.data as! T)
|
||||
),
|
||||
nil
|
||||
)
|
||||
})
|
||||
default:
|
||||
validatedRequest.responseData(completionHandler: { (dataResponse: DataResponse<Data>) in
|
||||
cleanupRequest()
|
||||
|
||||
guard dataResponse.result.isSuccess else {
|
||||
completion(nil, ErrorResponse.Error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!))
|
||||
return
|
||||
}
|
||||
|
||||
guard let data = dataResponse.data, !data.isEmpty else {
|
||||
completion(nil, ErrorResponse.Error(-1, nil, AlamofireDecodableRequestBuilderError.emptyDataResponse))
|
||||
return
|
||||
}
|
||||
|
||||
guard let httpResponse = dataResponse.response else {
|
||||
completion(nil, ErrorResponse.Error(-2, nil, AlamofireDecodableRequestBuilderError.nilHTTPResponse))
|
||||
return
|
||||
}
|
||||
|
||||
var responseObj: Response<T>? = nil
|
||||
|
||||
let decodeResult: (decodableObj: T?, error: Error?) = CodableHelper.decode(T.self, from: data)
|
||||
if decodeResult.error == nil {
|
||||
responseObj = Response(response: httpResponse, body: decodeResult.decodableObj)
|
||||
}
|
||||
|
||||
completion(responseObj, decodeResult.error)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
//
|
||||
// CodableHelper.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public typealias EncodeResult = (data: Data?, error: Error?)
|
||||
|
||||
open class CodableHelper {
|
||||
|
||||
open class func decode<T>(_ type: T.Type, from data: Data) -> (decodableObj: T?, error: Error?) where T : Decodable {
|
||||
var returnedDecodable: T? = nil
|
||||
var returnedError: Error? = nil
|
||||
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dataDecodingStrategy = .base64Decode
|
||||
if #available(iOS 10.0, *) {
|
||||
decoder.dateDecodingStrategy = .iso8601
|
||||
}
|
||||
|
||||
do {
|
||||
returnedDecodable = try decoder.decode(type, from: data)
|
||||
} catch {
|
||||
returnedError = error
|
||||
}
|
||||
|
||||
return (returnedDecodable, returnedError)
|
||||
}
|
||||
|
||||
open class func encode<T>(_ value: T, prettyPrint: Bool = false) -> EncodeResult where T : Encodable {
|
||||
var returnedData: Data?
|
||||
var returnedError: Error? = nil
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
if prettyPrint {
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
}
|
||||
encoder.dataEncodingStrategy = .base64Encode
|
||||
if #available(iOS 10.0, *) {
|
||||
encoder.dateEncodingStrategy = .iso8601
|
||||
}
|
||||
|
||||
do {
|
||||
returnedData = try encoder.encode(value)
|
||||
} catch {
|
||||
returnedError = error
|
||||
}
|
||||
|
||||
return (returnedData, returnedError)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Configuration.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
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"
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
// Extensions.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Alamofire
|
||||
|
||||
extension Bool: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
}
|
||||
|
||||
extension Float: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
}
|
||||
|
||||
extension Int: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
}
|
||||
|
||||
extension Int32: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
|
||||
}
|
||||
|
||||
extension Int64: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
|
||||
}
|
||||
|
||||
extension Double: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
}
|
||||
|
||||
extension String: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
}
|
||||
|
||||
private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
if let encodableObject = object as? JSONEncodable {
|
||||
return encodableObject.encodeToJSON()
|
||||
} else {
|
||||
return object as Any
|
||||
}
|
||||
}
|
||||
|
||||
extension Array: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.map(encodeIfPossible)
|
||||
}
|
||||
}
|
||||
|
||||
extension Dictionary: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
var dictionary = [AnyHashable: Any]()
|
||||
for (key, value) in self {
|
||||
dictionary[key as! NSObject] = encodeIfPossible(value)
|
||||
}
|
||||
return dictionary as Any
|
||||
}
|
||||
}
|
||||
|
||||
extension Data: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
}
|
||||
}
|
||||
|
||||
private let dateFormatter: DateFormatter = {
|
||||
let fmt = DateFormatter()
|
||||
fmt.dateFormat = Configuration.dateFormat
|
||||
fmt.locale = Locale(identifier: "en_US_POSIX")
|
||||
return fmt
|
||||
}()
|
||||
|
||||
extension Date: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return dateFormatter.string(from: self) as Any
|
||||
}
|
||||
}
|
||||
|
||||
extension UUID: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.uuidString
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// JSONDataEncoding.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Alamofire
|
||||
|
||||
public struct JSONDataEncoding: ParameterEncoding {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private static let jsonDataKey = "jsonData"
|
||||
|
||||
// MARK: Encoding
|
||||
|
||||
/// Creates a URL request by encoding parameters and applying them onto an existing request.
|
||||
///
|
||||
/// - parameter urlRequest: The request to have parameters applied.
|
||||
/// - parameter parameters: The parameters to apply. This should have a single key/value
|
||||
/// pair with "jsonData" as the key and a Data object as the value.
|
||||
///
|
||||
/// - throws: An `Error` if the encoding process encounters an error.
|
||||
///
|
||||
/// - returns: The encoded request.
|
||||
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
|
||||
var urlRequest = try urlRequest.asURLRequest()
|
||||
|
||||
guard let jsonData = parameters?[JSONDataEncoding.jsonDataKey] as? Data, !jsonData.isEmpty else {
|
||||
return urlRequest
|
||||
}
|
||||
|
||||
if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
|
||||
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
}
|
||||
|
||||
urlRequest.httpBody = jsonData
|
||||
|
||||
return urlRequest
|
||||
}
|
||||
|
||||
public static func encodingParameters(jsonData: Data?) -> Parameters? {
|
||||
var returnedParams: Parameters? = nil
|
||||
if let jsonData = jsonData, !jsonData.isEmpty {
|
||||
var params = Parameters()
|
||||
params[jsonDataKey] = jsonData
|
||||
returnedParams = params
|
||||
}
|
||||
return returnedParams
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// JSONEncodingHelper.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Alamofire
|
||||
|
||||
open class JSONEncodingHelper {
|
||||
|
||||
open class func encodingParameters<T:Encodable>(forEncodableObject encodableObj: T?) -> Parameters? {
|
||||
var params: Parameters? = nil
|
||||
|
||||
// Encode the Encodable object
|
||||
if let encodableObj = encodableObj {
|
||||
let encodeResult = CodableHelper.encode(encodableObj, prettyPrint: true)
|
||||
if encodeResult.error == nil {
|
||||
params = JSONDataEncoding.encodingParameters(jsonData: encodeResult.data)
|
||||
}
|
||||
}
|
||||
|
||||
return params
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// Models.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol JSONEncodable {
|
||||
func encodeToJSON() -> Any
|
||||
}
|
||||
|
||||
public enum ErrorResponse : Error {
|
||||
case Error(Int, Data?, Error)
|
||||
}
|
||||
|
||||
open class Response<T> {
|
||||
open let statusCode: Int
|
||||
open let header: [String: String]
|
||||
open let body: T?
|
||||
|
||||
public init(statusCode: Int, header: [String: String], body: T?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
}
|
||||
|
||||
public convenience init(response: HTTPURLResponse, body: T?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String:String]()
|
||||
for (key, value) in rawHeader {
|
||||
header[key as! String] = value as? String
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
//
|
||||
// AllPrimitives.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
/** Object which contains lots of different primitive Swagger types */
|
||||
open class AllPrimitives: Codable {
|
||||
|
||||
public var myInteger: Int?
|
||||
public var myIntegerArray: [Int]?
|
||||
public var myLong: Int64?
|
||||
public var myLongArray: [Int64]?
|
||||
public var myFloat: Float?
|
||||
public var myFloatArray: [Float]?
|
||||
public var myDouble: Double?
|
||||
public var myDoubleArray: [Double]?
|
||||
public var myString: String?
|
||||
public var myStringArray: [String]?
|
||||
public var myBytes: Data?
|
||||
public var myBytesArray: [Data]?
|
||||
public var myBoolean: Bool?
|
||||
public var myBooleanArray: [Bool]?
|
||||
public var myDate: Date?
|
||||
public var myDateArray: [Date]?
|
||||
public var myDateTime: Date?
|
||||
public var myDateTimeArray: [Date]?
|
||||
public var myFile: URL?
|
||||
public var myFileArray: [URL]?
|
||||
public var myUUID: UUID?
|
||||
public var myUUIDArray: [UUID]?
|
||||
public var myStringEnum: StringEnum?
|
||||
public var myStringEnumArray: [StringEnum]?
|
||||
|
||||
public init() {}
|
||||
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case myInteger = "myInteger"
|
||||
case myIntegerArray = "myIntegerArray"
|
||||
case myLong = "myLong"
|
||||
case myLongArray = "myLongArray"
|
||||
case myFloat = "myFloat"
|
||||
case myFloatArray = "myFloatArray"
|
||||
case myDouble = "myDouble"
|
||||
case myDoubleArray = "myDoubleArray"
|
||||
case myString = "myString"
|
||||
case myStringArray = "myStringArray"
|
||||
case myBytes = "myBytes"
|
||||
case myBytesArray = "myBytesArray"
|
||||
case myBoolean = "myBoolean"
|
||||
case myBooleanArray = "myBooleanArray"
|
||||
case myDate = "myDate"
|
||||
case myDateArray = "myDateArray"
|
||||
case myDateTime = "myDateTime"
|
||||
case myDateTimeArray = "myDateTimeArray"
|
||||
case myFile = "myFile"
|
||||
case myFileArray = "myFileArray"
|
||||
case myUUID = "myUUID"
|
||||
case myUUIDArray = "myUUIDArray"
|
||||
case myStringEnum = "myStringEnum"
|
||||
case myStringEnumArray = "myStringEnumArray"
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// ErrorInfo.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
/** Example Error object */
|
||||
open class ErrorInfo: Codable {
|
||||
|
||||
public var code: Int?
|
||||
public var message: String?
|
||||
public var details: [String]?
|
||||
|
||||
public init() {}
|
||||
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case code = "code"
|
||||
case message = "message"
|
||||
case details = "details"
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// GetAllModelsResult.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
/** Response object containing AllPrimitives object */
|
||||
open class GetAllModelsResult: Codable {
|
||||
|
||||
public var myPrimitiveArray: [AllPrimitives]?
|
||||
public var myPrimitive: AllPrimitives?
|
||||
public var myVariableNameTest: VariableNameTest?
|
||||
|
||||
public init() {}
|
||||
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case myPrimitiveArray = "myPrimitiveArray"
|
||||
case myPrimitive = "myPrimitive"
|
||||
case myVariableNameTest = "myVariableNameTest"
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// StringEnum.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
public enum StringEnum: String, Codable {
|
||||
case stringenumvalue1 = "stringEnumValue1"
|
||||
case stringenumvalue2 = "stringEnumValue2"
|
||||
case stringenumvalue3 = "stringEnumValue3"
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// VariableNameTest.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
/** This object contains property names which we know will be different from their variable name. Examples of this include snake case property names and property names which are Swift 4 reserved words. */
|
||||
open class VariableNameTest: Codable {
|
||||
|
||||
/** This snake-case examle_name property name should be converted to a camelCase variable name like exampleName */
|
||||
public var exampleName: String?
|
||||
/** This property name is a reserved word in most languages, including Swift 4. */
|
||||
public var _for: String?
|
||||
|
||||
public init() {}
|
||||
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case exampleName = "example_name"
|
||||
case _for = "for"
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user