mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-06 15:40:54 +00:00
Update security samples for swift. (#5324)
This commit is contained in:
parent
5b16694afc
commit
be83329946
@ -4,7 +4,10 @@ Pod::Spec.new do |s|
|
|||||||
s.osx.deployment_target = '10.9'
|
s.osx.deployment_target = '10.9'
|
||||||
s.version = '0.0.1'
|
s.version = '0.0.1'
|
||||||
s.source = { :git => 'git@github.com:swagger-api/swagger-mustache.git', :tag => 'v1.0.0' }
|
s.source = { :git => 'git@github.com:swagger-api/swagger-mustache.git', :tag => 'v1.0.0' }
|
||||||
s.license = 'Apache License, Version 2.0'
|
s.authors = 'Swagger Codegen'
|
||||||
|
s.license = 'Proprietary'
|
||||||
|
s.homepage = 'https://github.com/swagger-api/swagger-codegen'
|
||||||
|
s.summary = 'SwaggerClient Swift SDK'
|
||||||
s.source_files = 'SwaggerClient/Classes/Swaggers/**/*.swift'
|
s.source_files = 'SwaggerClient/Classes/Swaggers/**/*.swift'
|
||||||
s.dependency 'Alamofire', '~> 3.1.5'
|
s.dependency 'Alamofire', '~> 3.4.1'
|
||||||
end
|
end
|
||||||
|
@ -19,6 +19,16 @@ class APIHelper {
|
|||||||
return destination
|
return destination
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static func rejectNilHeaders(source: [String:AnyObject?]) -> [String:String] {
|
||||||
|
var destination = [String:String]()
|
||||||
|
for (key, nillableValue) in source {
|
||||||
|
if let value: AnyObject = nillableValue {
|
||||||
|
destination[key] = "\(value)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return destination
|
||||||
|
}
|
||||||
|
|
||||||
static func convertBoolToString(source: [String: AnyObject]?) -> [String:AnyObject]? {
|
static func convertBoolToString(source: [String: AnyObject]?) -> [String:AnyObject]? {
|
||||||
guard let source = source else {
|
guard let source = source else {
|
||||||
return nil
|
return nil
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public class SwaggerClientAPI {
|
public class SwaggerClientAPI {
|
||||||
public static var basePath = "https://petstore.swagger.io *_/ ' \" =end \\r\\n \\n \\r/v2 *_/ ' \" =end \\r\\n \\n \\r"
|
public static var basePath = "https://petstore.swagger.io *_/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r"
|
||||||
public static var credential: NSURLCredential?
|
public static var credential: NSURLCredential?
|
||||||
public static var customHeaders: [String:String] = [:]
|
public static var customHeaders: [String:String] = [:]
|
||||||
static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
|
static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
|
||||||
@ -31,17 +31,21 @@ public class APIBase {
|
|||||||
|
|
||||||
public class RequestBuilder<T> {
|
public class RequestBuilder<T> {
|
||||||
var credential: NSURLCredential?
|
var credential: NSURLCredential?
|
||||||
var headers: [String:String] = [:]
|
var headers: [String:String]
|
||||||
let parameters: [String:AnyObject]?
|
let parameters: [String:AnyObject]?
|
||||||
let isBody: Bool
|
let isBody: Bool
|
||||||
let method: String
|
let method: String
|
||||||
let URLString: String
|
let URLString: String
|
||||||
|
|
||||||
required public init(method: String, URLString: String, parameters: [String:AnyObject]?, isBody: Bool) {
|
/// Optional block to obtain a reference to the request's progress instance when available.
|
||||||
|
public var onProgressReady: ((NSProgress) -> ())?
|
||||||
|
|
||||||
|
required public init(method: String, URLString: String, parameters: [String:AnyObject]?, isBody: Bool, headers: [String:String] = [:]) {
|
||||||
self.method = method
|
self.method = method
|
||||||
self.URLString = URLString
|
self.URLString = URLString
|
||||||
self.parameters = parameters
|
self.parameters = parameters
|
||||||
self.isBody = isBody
|
self.isBody = isBody
|
||||||
|
self.headers = headers
|
||||||
|
|
||||||
addHeaders(SwaggerClientAPI.customHeaders)
|
addHeaders(SwaggerClientAPI.customHeaders)
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ import Alamofire
|
|||||||
|
|
||||||
public class FakeAPI: APIBase {
|
public class FakeAPI: APIBase {
|
||||||
/**
|
/**
|
||||||
To test code injection *_/ ' \" =end \\r\\n \\n \\r
|
To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
|
||||||
|
|
||||||
- parameter testCodeInjectEndRnNR: (form) To test code injection *_/ ' \" =end \\r\\n \\n \\r (optional)
|
- parameter testCodeInjectEndRnNR: (form) To test code injection *_/ ' \" =end -- \\r\\n \\n \\r (optional)
|
||||||
- parameter completion: completion handler to receive the data and the error objects
|
- parameter completion: completion handler to receive the data and the error objects
|
||||||
*/
|
*/
|
||||||
public class func testCodeInjectEndRnNR(testCodeInjectEndRnNR testCodeInjectEndRnNR: String? = nil, completion: ((error: ErrorType?) -> Void)) {
|
public class func testCodeInjectEndRnNR(testCodeInjectEndRnNR testCodeInjectEndRnNR: String? = nil, completion: ((error: ErrorType?) -> Void)) {
|
||||||
@ -24,10 +24,10 @@ public class FakeAPI: APIBase {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
To test code injection *_/ ' \" =end \\r\\n \\n \\r
|
To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
|
||||||
- PUT /fake
|
- PUT /fake
|
||||||
|
|
||||||
- parameter testCodeInjectEndRnNR: (form) To test code injection *_/ ' \" =end \\r\\n \\n \\r (optional)
|
- parameter testCodeInjectEndRnNR: (form) To test code injection *_/ ' \" =end -- \\r\\n \\n \\r (optional)
|
||||||
|
|
||||||
- returns: RequestBuilder<Void>
|
- returns: RequestBuilder<Void>
|
||||||
*/
|
*/
|
||||||
@ -36,7 +36,7 @@ public class FakeAPI: APIBase {
|
|||||||
let URLString = SwaggerClientAPI.basePath + path
|
let URLString = SwaggerClientAPI.basePath + path
|
||||||
|
|
||||||
let nillableParameters: [String:AnyObject?] = [
|
let nillableParameters: [String:AnyObject?] = [
|
||||||
"test code inject */ ' " =end \r\n \n \r": testCodeInjectEndRnNR
|
"test code inject */ ' " =end -- \r\n \n \r": testCodeInjectEndRnNR
|
||||||
]
|
]
|
||||||
|
|
||||||
let parameters = APIHelper.rejectNil(nillableParameters)
|
let parameters = APIHelper.rejectNil(nillableParameters)
|
||||||
|
@ -12,12 +12,37 @@ class AlamofireRequestBuilderFactory: RequestBuilderFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public struct SynchronizedDictionary<K: Hashable, V> {
|
||||||
|
|
||||||
|
private var dictionary = [K: V]()
|
||||||
|
private let queue = dispatch_queue_create("SynchronizedDictionary", DISPATCH_QUEUE_CONCURRENT)
|
||||||
|
|
||||||
|
public subscript(key: K) -> V? {
|
||||||
|
get {
|
||||||
|
var value: V?
|
||||||
|
|
||||||
|
dispatch_sync(queue) {
|
||||||
|
value = self.dictionary[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
set {
|
||||||
|
dispatch_barrier_sync(queue) {
|
||||||
|
self.dictionary[key] = newValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Store manager to retain its reference
|
// Store manager to retain its reference
|
||||||
private var managerStore: [String: Alamofire.Manager] = [:]
|
private var managerStore = SynchronizedDictionary<String, Alamofire.Manager>()
|
||||||
|
|
||||||
class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||||
required init(method: String, URLString: String, parameters: [String : AnyObject]?, isBody: Bool) {
|
required init(method: String, URLString: String, parameters: [String : AnyObject]?, isBody: Bool, headers: [String : String] = [:]) {
|
||||||
super.init(method: method, URLString: URLString, parameters: parameters, isBody: isBody)
|
super.init(method: method, URLString: URLString, parameters: parameters, isBody: isBody, headers: headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
override func execute(completion: (response: Response<T>?, error: ErrorType?) -> Void) {
|
override func execute(completion: (response: Response<T>?, error: ErrorType?) -> Void) {
|
||||||
@ -57,15 +82,22 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
encodingMemoryThreshold: Manager.MultipartFormDataEncodingMemoryThreshold,
|
encodingMemoryThreshold: Manager.MultipartFormDataEncodingMemoryThreshold,
|
||||||
encodingCompletion: { encodingResult in
|
encodingCompletion: { encodingResult in
|
||||||
switch encodingResult {
|
switch encodingResult {
|
||||||
case .Success(let upload, _, _):
|
case .Success(let uploadRequest, _, _):
|
||||||
self.processRequest(upload, managerId, completion)
|
if let onProgressReady = self.onProgressReady {
|
||||||
|
onProgressReady(uploadRequest.progress)
|
||||||
|
}
|
||||||
|
self.processRequest(uploadRequest, managerId, completion)
|
||||||
case .Failure(let encodingError):
|
case .Failure(let encodingError):
|
||||||
completion(response: nil, error: encodingError)
|
completion(response: nil, error: ErrorResponse.Error(415, nil, encodingError))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
processRequest(manager.request(xMethod!, URLString, parameters: parameters, encoding: encoding), managerId, completion)
|
let request = manager.request(xMethod!, URLString, parameters: parameters, encoding: encoding)
|
||||||
|
if let onProgressReady = self.onProgressReady {
|
||||||
|
onProgressReady(request.progress)
|
||||||
|
}
|
||||||
|
processRequest(request, managerId, completion)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -75,30 +107,99 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
request.authenticate(usingCredential: credential)
|
request.authenticate(usingCredential: credential)
|
||||||
}
|
}
|
||||||
|
|
||||||
request.validate().responseJSON(options: .AllowFragments) { response in
|
let cleanupRequest = {
|
||||||
managerStore.removeValueForKey(managerId)
|
managerStore[managerId] = nil
|
||||||
|
}
|
||||||
|
|
||||||
if response.result.isFailure {
|
let validatedRequest = request.validate()
|
||||||
completion(response: nil, error: response.result.error)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if () is T {
|
switch T.self {
|
||||||
completion(response: Response(response: response.response!, body: () as! T), error: nil)
|
case is String.Type:
|
||||||
return
|
validatedRequest.responseString(completionHandler: { (stringResponse) in
|
||||||
}
|
cleanupRequest()
|
||||||
if let json: AnyObject = response.result.value {
|
|
||||||
let body = Decoders.decode(clazz: T.self, source: json)
|
|
||||||
completion(response: Response(response: response.response!, body: body), error: nil)
|
|
||||||
return
|
|
||||||
} else if "" is T {
|
|
||||||
// swagger-parser currently doesn't support void, which will be fixed in future swagger-parser release
|
|
||||||
// https://github.com/swagger-api/swagger-parser/pull/34
|
|
||||||
completion(response: Response(response: response.response!, body: "" as! T), error: nil)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
completion(response: nil, error: NSError(domain: "localhost", code: 500, userInfo: ["reason": "unreacheable code"]))
|
if stringResponse.result.isFailure {
|
||||||
|
completion(
|
||||||
|
response: nil,
|
||||||
|
error: ErrorResponse.Error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error!)
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
completion(
|
||||||
|
response: Response(
|
||||||
|
response: stringResponse.response!,
|
||||||
|
body: (stringResponse.result.value ?? "") as! T
|
||||||
|
),
|
||||||
|
error: nil
|
||||||
|
)
|
||||||
|
})
|
||||||
|
case is Void.Type:
|
||||||
|
validatedRequest.responseData(completionHandler: { (voidResponse) in
|
||||||
|
cleanupRequest()
|
||||||
|
|
||||||
|
if voidResponse.result.isFailure {
|
||||||
|
completion(
|
||||||
|
response: nil,
|
||||||
|
error: ErrorResponse.Error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!)
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
completion(
|
||||||
|
response: Response(
|
||||||
|
response: voidResponse.response!,
|
||||||
|
body: nil
|
||||||
|
),
|
||||||
|
error: nil
|
||||||
|
)
|
||||||
|
})
|
||||||
|
case is NSData.Type:
|
||||||
|
validatedRequest.responseData(completionHandler: { (dataResponse) in
|
||||||
|
cleanupRequest()
|
||||||
|
|
||||||
|
if (dataResponse.result.isFailure) {
|
||||||
|
completion(
|
||||||
|
response: nil,
|
||||||
|
error: ErrorResponse.Error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!)
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
completion(
|
||||||
|
response: Response(
|
||||||
|
response: dataResponse.response!,
|
||||||
|
body: dataResponse.data as! T
|
||||||
|
),
|
||||||
|
error: nil
|
||||||
|
)
|
||||||
|
})
|
||||||
|
default:
|
||||||
|
validatedRequest.responseJSON(options: .AllowFragments) { response in
|
||||||
|
cleanupRequest()
|
||||||
|
|
||||||
|
if response.result.isFailure {
|
||||||
|
completion(response: nil, error: ErrorResponse.Error(response.response?.statusCode ?? 500, response.data, response.result.error!))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if () is T {
|
||||||
|
completion(response: Response(response: response.response!, body: () as! T), error: nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if let json: AnyObject = response.result.value {
|
||||||
|
let body = Decoders.decode(clazz: T.self, source: json)
|
||||||
|
completion(response: Response(response: response.response!, body: body), error: nil)
|
||||||
|
return
|
||||||
|
} else if "" is T {
|
||||||
|
// swagger-parser currently doesn't support void, which will be fixed in future swagger-parser release
|
||||||
|
// https://github.com/swagger-api/swagger-parser/pull/34
|
||||||
|
completion(response: Response(response: response.response!, body: "" as! T), error: nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
completion(response: nil, error: ErrorResponse.Error(500, nil, NSError(domain: "localhost", code: 500, userInfo: ["reason": "unreacheable code"])))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,11 +58,17 @@ extension Dictionary: JSONEncodable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension NSData: JSONEncodable {
|
||||||
|
func encodeToJSON() -> AnyObject {
|
||||||
|
return self.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private let dateFormatter: NSDateFormatter = {
|
private let dateFormatter: NSDateFormatter = {
|
||||||
let dateFormatter = NSDateFormatter()
|
let fmt = NSDateFormatter()
|
||||||
dateFormatter.dateFormat = "yyyy-MM-dd"
|
fmt.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
|
||||||
return dateFormatter
|
fmt.locale = NSLocale(localeIdentifier: "en_US_POSIX")
|
||||||
|
return fmt
|
||||||
}()
|
}()
|
||||||
|
|
||||||
extension NSDate: JSONEncodable {
|
extension NSDate: JSONEncodable {
|
||||||
@ -71,4 +77,10 @@ extension NSDate: JSONEncodable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension NSUUID: JSONEncodable {
|
||||||
|
func encodeToJSON() -> AnyObject {
|
||||||
|
return self.UUIDString
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,18 +10,22 @@ protocol JSONEncodable {
|
|||||||
func encodeToJSON() -> AnyObject
|
func encodeToJSON() -> AnyObject
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum ErrorResponse : ErrorType {
|
||||||
|
case Error(Int, NSData?, ErrorType)
|
||||||
|
}
|
||||||
|
|
||||||
public class Response<T> {
|
public class Response<T> {
|
||||||
public let statusCode: Int
|
public let statusCode: Int
|
||||||
public let header: [String: String]
|
public let header: [String: String]
|
||||||
public let body: T
|
public let body: T?
|
||||||
|
|
||||||
public init(statusCode: Int, header: [String: String], body: T) {
|
public init(statusCode: Int, header: [String: String], body: T?) {
|
||||||
self.statusCode = statusCode
|
self.statusCode = statusCode
|
||||||
self.header = header
|
self.header = header
|
||||||
self.body = body
|
self.body = body
|
||||||
}
|
}
|
||||||
|
|
||||||
public convenience init(response: NSHTTPURLResponse, body: T) {
|
public convenience init(response: NSHTTPURLResponse, body: T?) {
|
||||||
let rawHeader = response.allHeaderFields
|
let rawHeader = response.allHeaderFields
|
||||||
var header = [String:String]()
|
var header = [String:String]()
|
||||||
for (key, value) in rawHeader {
|
for (key, value) in rawHeader {
|
||||||
@ -62,9 +66,15 @@ class Decoders {
|
|||||||
if T.self is Int64.Type && source is NSNumber {
|
if T.self is Int64.Type && source is NSNumber {
|
||||||
return source.longLongValue as! T;
|
return source.longLongValue as! T;
|
||||||
}
|
}
|
||||||
|
if T.self is NSUUID.Type && source is String {
|
||||||
|
return NSUUID(UUIDString: source as! String) as! T
|
||||||
|
}
|
||||||
if source is T {
|
if source is T {
|
||||||
return source as! T
|
return source as! T
|
||||||
}
|
}
|
||||||
|
if T.self is NSData.Type && source is String {
|
||||||
|
return NSData(base64EncodedString: source as! String, options: NSDataBase64DecodingOptions()) as! T
|
||||||
|
}
|
||||||
|
|
||||||
let key = "\(T.self)"
|
let key = "\(T.self)"
|
||||||
if let decoder = decoders[key] {
|
if let decoder = decoders[key] {
|
||||||
@ -111,6 +121,7 @@ class Decoders {
|
|||||||
"yyyy-MM-dd'T'HH:mm:ss.SSS"
|
"yyyy-MM-dd'T'HH:mm:ss.SSS"
|
||||||
].map { (format: String) -> NSDateFormatter in
|
].map { (format: String) -> NSDateFormatter in
|
||||||
let formatter = NSDateFormatter()
|
let formatter = NSDateFormatter()
|
||||||
|
formatter.locale = NSLocale(localeIdentifier:"en_US_POSIX")
|
||||||
formatter.dateFormat = format
|
formatter.dateFormat = format
|
||||||
return formatter
|
return formatter
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,9 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
|
||||||
/** Model for testing reserved words *_/ ' \" =end \\r\\n \\n \\r */
|
/** Model for testing reserved words *_/ ' \" =end -- \\r\\n \\n \\r */
|
||||||
public class Return: JSONEncodable {
|
public class Return: JSONEncodable {
|
||||||
/** property description *_/ ' \" =end \\r\\n \\n \\r */
|
/** property description *_/ ' \" =end -- \\r\\n \\n \\r */
|
||||||
public var _return: Int32?
|
public var _return: Int32?
|
||||||
|
|
||||||
public init() {}
|
public init() {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user