forked from loafle/openapi-generator-original
Introduce Decoders class
This commit is contained in:
parent
be7fccce68
commit
bb3ec04ed1
@ -63,8 +63,6 @@ public class SwiftGenerator extends DefaultCodegen implements CodegenConfig {
|
||||
supportingFiles.add(new SupportingFile("Extensions.mustache", sourceFolder, "Extensions.swift"));
|
||||
supportingFiles.add(new SupportingFile("Models.mustache", sourceFolder, "Models.swift"));
|
||||
supportingFiles.add(new SupportingFile("APIs.mustache", sourceFolder, "APIs.swift"));
|
||||
supportingFiles.add(new SupportingFile("DecodableDictionary.mustache", sourceFolder, "DecodableDictionary.swift"));
|
||||
supportingFiles.add(new SupportingFile("DecodableArray.mustache", sourceFolder, "DecodableArray.swift"));
|
||||
|
||||
languageSpecificPrimitives = new HashSet<String>(
|
||||
Arrays.asList(
|
||||
@ -140,11 +138,11 @@ public class SwiftGenerator extends DefaultCodegen implements CodegenConfig {
|
||||
if (p instanceof ArrayProperty) {
|
||||
ArrayProperty ap = (ArrayProperty) p;
|
||||
Property inner = ap.getItems();
|
||||
return "DecodableArray<" + getTypeDeclaration(inner) + ">";
|
||||
return "[" + getTypeDeclaration(inner) + "]";
|
||||
} else if (p instanceof MapProperty) {
|
||||
MapProperty mp = (MapProperty) p;
|
||||
Property inner = mp.getAdditionalProperties();
|
||||
return "DecodableDictionary<String, " + getTypeDeclaration(inner) + ">";
|
||||
return "[String:" + getTypeDeclaration(inner) + "]";
|
||||
}
|
||||
return super.getTypeDeclaration(p);
|
||||
}
|
||||
|
@ -13,10 +13,23 @@ class {{projectName}}API {
|
||||
static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
|
||||
|
||||
class APIBase {
|
||||
func toParameters(encodable: JSONEncodable?) -> [String: AnyObject]? {
|
||||
let encoded: AnyObject? = encodable?.encode()
|
||||
|
||||
if encoded! is [AnyObject] {
|
||||
var dictionary = [String:AnyObject]()
|
||||
for (index, item) in enumerate(encoded as! [AnyObject]) {
|
||||
dictionary["\(index)"] = item
|
||||
}
|
||||
return dictionary
|
||||
} else {
|
||||
return encoded as? [String:AnyObject]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class RequestBuilder<T: JSONDecodable> {
|
||||
class RequestBuilder<T> {
|
||||
var credential: NSURLCredential?
|
||||
var headers: [String:String] = [:]
|
||||
let parameters: [String:AnyObject]?
|
||||
@ -47,6 +60,6 @@ class RequestBuilder<T: JSONDecodable> {
|
||||
}
|
||||
|
||||
protocol RequestBuilderFactory {
|
||||
func getBuilder<T: JSONDecodable>() -> RequestBuilder<T>.Type
|
||||
func getBuilder<T>() -> RequestBuilder<T>.Type
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@ import Alamofire
|
||||
import PromiseKit
|
||||
|
||||
class AlamofireRequestBuilderFactory: RequestBuilderFactory {
|
||||
func getBuilder<T: JSONDecodable>() -> RequestBuilder<T>.Type {
|
||||
func getBuilder<T>() -> RequestBuilder<T>.Type {
|
||||
return AlamofireRequestBuilder<T>.self
|
||||
}
|
||||
}
|
||||
@ -16,7 +16,7 @@ class AlamofireRequestBuilderFactory: RequestBuilderFactory {
|
||||
// Store manager to retain its reference
|
||||
private var managerStore: [String: Alamofire.Manager] = [:]
|
||||
|
||||
class AlamofireRequestBuilder<T: JSONDecodable>: RequestBuilder<T> {
|
||||
class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
required init(method: String, URLString: String, parameters: [String : AnyObject]?, isBody: Bool) {
|
||||
super.init(method: method, URLString: URLString, parameters: parameters, isBody: isBody)
|
||||
}
|
||||
@ -50,18 +50,15 @@ class AlamofireRequestBuilder<T: JSONDecodable>: RequestBuilder<T> {
|
||||
return
|
||||
}
|
||||
|
||||
if Empty.instance is T {
|
||||
let response = Response(response: res!, body: Empty.instance as! T)
|
||||
if () is T {
|
||||
let response = Response(response: res!, body: () as! T)
|
||||
defer.fulfill(response)
|
||||
return
|
||||
}
|
||||
if let json: AnyObject = json {
|
||||
if let body = T.decode(json) {
|
||||
let response = Response(response: res!, body: body)
|
||||
defer.fulfill(response)
|
||||
} else {
|
||||
defer.reject(NSError(domain: "localhost", code: 500, userInfo: ["reason": "cannot convertible: \(json)"]))
|
||||
}
|
||||
let body = Decoders.decode(clazz: T.self, source: json)
|
||||
let response = Response(response: res!, body: body)
|
||||
defer.fulfill(response)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -1,28 +0,0 @@
|
||||
// DecodableArray.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
|
||||
final class DecodableArray<T where T: JSONDecodable, T: JSONEncodable>: JSONEncodable, JSONDecodable, ArrayLiteralConvertible {
|
||||
let value: [T]
|
||||
|
||||
required init(arrayLiteral elements: T...) {
|
||||
self.value = elements
|
||||
}
|
||||
|
||||
init(array elements: [T]) {
|
||||
self.value = elements
|
||||
}
|
||||
|
||||
static func decode(source: AnyObject) -> DecodableArray? {
|
||||
let array = source as! [AnyObject]
|
||||
let decodedArray = DecodableArray(array: array.map({ T.decode($0)! }))
|
||||
return decodedArray
|
||||
}
|
||||
|
||||
func encode() -> AnyObject {
|
||||
return value.map { $0.encode() }
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
// DecodableDictionary.swift
|
||||
//
|
||||
// Generated by swagger-codegen
|
||||
// https://github.com/swagger-api/swagger-codegen
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
final class DecodableDictionary<Key, Value where Key: Hashable, Key: JSONEncodable, Key: JSONDecodable, Value: JSONEncodable, Value: JSONDecodable>: JSONEncodable, JSONDecodable, DictionaryLiteralConvertible {
|
||||
let value: [Key:Value]
|
||||
|
||||
init(dictionaryLiteral elements: (Key, Value)...) {
|
||||
var dic = [Key:Value]()
|
||||
for (key, value) in elements {
|
||||
dic[key] = value
|
||||
}
|
||||
self.value = dic
|
||||
}
|
||||
|
||||
init(dictionary elements: [Key:Value]) {
|
||||
self.value = elements
|
||||
}
|
||||
|
||||
static func decode(source: AnyObject) -> DecodableDictionary? {
|
||||
var destination = [Key:Value]()
|
||||
let dictionary = source as! [NSObject:AnyObject]
|
||||
for (key, value) in dictionary {
|
||||
let decodedKey = Key.decode(key)!
|
||||
let decodedValue = Value.decode(value)!
|
||||
destination[decodedKey] = decodedValue
|
||||
}
|
||||
return DecodableDictionary(dictionary: destination)
|
||||
}
|
||||
|
||||
func encode() -> AnyObject {
|
||||
var destination = [String: AnyObject]()
|
||||
for (key, value) in self.value {
|
||||
let keyStr = "\(key.encode())"
|
||||
let valueObj: AnyObject = value.encode()
|
||||
destination[keyStr] = valueObj
|
||||
}
|
||||
return destination
|
||||
}
|
||||
}
|
@ -9,73 +9,26 @@ import PromiseKit
|
||||
import SwiftyJSON
|
||||
|
||||
extension Request {
|
||||
func responsePromise<T>() -> Promise<Response<T>> {
|
||||
let defer = Promise<Response<T>>.defer()
|
||||
responseJSON(options: .AllowFragments) { (req, res, json, error) in
|
||||
if let error = error {
|
||||
defer.reject(error)
|
||||
return
|
||||
}
|
||||
if res!.statusCode >= 400 {
|
||||
let error = NSError(domain: res!.URL!.URLString, code: res!.statusCode, userInfo: [:])
|
||||
defer.reject(error)
|
||||
return
|
||||
}
|
||||
|
||||
if Empty.instance is T {
|
||||
let response = Response(response: res!, body: Empty.instance as! T)
|
||||
defer.fulfill(response)
|
||||
return
|
||||
}
|
||||
if let json: AnyObject = json {
|
||||
if let body = T.decode(json) {
|
||||
let response = Response(response: res!, body: body)
|
||||
defer.fulfill(response)
|
||||
} else {
|
||||
defer.reject(NSError(domain: "localhost", code: 500, userInfo: ["reason": "cannot convertible: \(json)"]))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
defer.reject(NSError(domain: "localhost", code: 500, userInfo: ["reason": "unreacheable code"]))
|
||||
}
|
||||
return defer.promise
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension Bool: JSONEncodable, JSONDecodable {
|
||||
static func decode(source: AnyObject) -> Bool? {
|
||||
return source as? Bool
|
||||
}
|
||||
extension Bool: JSONEncodable {
|
||||
func encode() -> AnyObject { return self }
|
||||
}
|
||||
|
||||
extension Float: JSONEncodable, JSONDecodable {
|
||||
static func decode(source: AnyObject) -> Float? {
|
||||
return source as? Float
|
||||
}
|
||||
extension Float: JSONEncodable {
|
||||
func encode() -> AnyObject { return self }
|
||||
}
|
||||
|
||||
extension Int: JSONEncodable, JSONDecodable {
|
||||
static func decode(source: AnyObject) -> Int? {
|
||||
return source as? Int
|
||||
}
|
||||
extension Int: JSONEncodable {
|
||||
func encode() -> AnyObject { return self }
|
||||
}
|
||||
|
||||
extension Double: JSONEncodable, JSONDecodable {
|
||||
static func decode(source: AnyObject) -> Double? {
|
||||
return source as? Double
|
||||
}
|
||||
extension Double: JSONEncodable {
|
||||
func encode() -> AnyObject { return self }
|
||||
}
|
||||
|
||||
extension String: JSONEncodable, JSONDecodable {
|
||||
static func decode(source: AnyObject) -> String? {
|
||||
return source as? String
|
||||
}
|
||||
extension String: JSONEncodable {
|
||||
func encode() -> AnyObject { return self }
|
||||
}
|
||||
|
||||
@ -110,12 +63,6 @@ extension JSON {
|
||||
func decode() -> String {
|
||||
return self.stringValue
|
||||
}
|
||||
func decode<T: JSONDecodable>() -> T? {
|
||||
return T.decode(self.object)
|
||||
}
|
||||
func decode<T: JSONDecodable>() -> [T]? {
|
||||
return self.arrayObject?.map(T.decode).filter({ $0 != nil }).map({ $0! })
|
||||
}
|
||||
func decode<T>() -> [T]? {
|
||||
return self.array?.map({ $0 as! T })
|
||||
}
|
||||
|
@ -5,17 +5,12 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftyJSON
|
||||
|
||||
protocol JSONDecodable {
|
||||
static func decode(source: AnyObject) -> Self?
|
||||
}
|
||||
|
||||
protocol JSONEncodable {
|
||||
func encode() -> AnyObject
|
||||
}
|
||||
|
||||
class Response<T: JSONDecodable> {
|
||||
class Response<T> {
|
||||
let statusCode: Int
|
||||
let header: [String: String]
|
||||
let body: T
|
||||
@ -36,12 +31,38 @@ class Response<T: JSONDecodable> {
|
||||
}
|
||||
}
|
||||
|
||||
final class Empty: JSONDecodable {
|
||||
private init() { }
|
||||
class Decoders {
|
||||
static private var decoders = Dictionary<String, ((AnyObject) -> AnyObject)>()
|
||||
|
||||
static let instance = Empty()
|
||||
static func addDecoder<T>(#clazz: T.Type, decoder: ((AnyObject) -> T)) {
|
||||
let key = "\(T.self)"
|
||||
decoders[key] = { decoder($0) as! AnyObject }
|
||||
}
|
||||
|
||||
static func decode(source: AnyObject) -> Empty? {
|
||||
return instance
|
||||
static func decode<T>(#clazz: [T].Type, source: AnyObject) -> [T] {
|
||||
let array = source as! [AnyObject]
|
||||
return array.map { Decoders.decode(clazz: T.self, source: $0) }
|
||||
}
|
||||
|
||||
static func decode<T, Key: Hashable>(#clazz: [Key:T].Type, source: AnyObject) -> [Key:T] {
|
||||
let sourceDictinoary = source as! [Key: AnyObject]
|
||||
var dictionary = [Key:T]()
|
||||
for (key, value) in sourceDictinoary {
|
||||
dictionary[key] = Decoders.decode(clazz: T.self, source: value)
|
||||
}
|
||||
return dictionary
|
||||
}
|
||||
|
||||
static func decode<T>(#clazz: T.Type, source: AnyObject) -> T {
|
||||
if source is T {
|
||||
return source as! T
|
||||
}
|
||||
|
||||
let key = "\(T.self)"
|
||||
if let decoder = decoders[key] {
|
||||
return decoder(source) as! T
|
||||
} else {
|
||||
fatalError("Source \(source) is not convertible to type \(clazz): Maybe swagger file is insufficient")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,9 +28,9 @@ extension {{projectName}}API {
|
||||
{{/hasParams}}{{#allParams}}
|
||||
:param: {{paramName}} ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}}{{/allParams}}
|
||||
|
||||
:returns: Promise<Response<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Empty{{/returnType}}>> {{description}}
|
||||
:returns: Promise<Response<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>> {{description}}
|
||||
*/
|
||||
func {{operationId}}({{#allParams}}{{^secondaryParam}}#{{/secondaryParam}}{{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Empty{{/returnType}}> {
|
||||
func {{operationId}}({{#allParams}}{{^secondaryParam}}#{{/secondaryParam}}{{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
|
||||
{{^pathParams}}let{{/pathParams}}{{#pathParams}}{{^secondaryParam}}var{{/secondaryParam}}{{/pathParams}} path = "{{path}}"{{#pathParams}}
|
||||
path = path.stringByReplacingOccurrencesOfString("{{=<% %>=}}{<%paramName%>}<%={{ }}=%>", withString: "\({{paramName}})", options: .LiteralSearch, range: nil){{/pathParams}}
|
||||
let url = {{projectName}}API.basePath + path
|
||||
@ -41,7 +41,7 @@ extension {{projectName}}API {
|
||||
]{{/hasMore}}{{/queryParams}}
|
||||
let parameters = APIHelper.rejectNil(nillableParameters){{/bodyParam}}
|
||||
|
||||
let requestBuilder: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Empty{{/returnType}}>.Type = {{projectName}}API.requestBuilderFactory.getBuilder()
|
||||
let requestBuilder: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>.Type = {{projectName}}API.requestBuilderFactory.getBuilder()
|
||||
|
||||
return requestBuilder(method: "{{httpMethod}}", URLString: url, parameters: parameters, isBody: {{^queryParams}}true{{/queryParams}}{{#queryParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/queryParams}})
|
||||
}
|
||||
|
@ -7,11 +7,14 @@
|
||||
|
||||
import SwiftyJSON
|
||||
|
||||
extension JSON {
|
||||
}
|
||||
|
||||
extension {{projectName}}API {
|
||||
{{#description}}
|
||||
|
||||
/** {{description}} */{{/description}}
|
||||
final class {{classname}}: JSONEncodable, JSONDecodable {
|
||||
final class {{classname}}: JSONEncodable {
|
||||
{{#vars}}{{#isEnum}}
|
||||
enum {{datatypeWithEnum}}: String { {{#allowableValues}}{{#values}}
|
||||
case {{enum}} = "{{raw}}"{{/values}}{{/allowableValues}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user