From 5dfa753f5cf4328f423ec829b11a945212563e66 Mon Sep 17 00:00:00 2001 From: kubo_takaichi Date: Sun, 17 May 2015 19:26:11 +0900 Subject: [PATCH] Do refactor and add extension methods --- .../codegen/languages/SwiftGenerator.java | 2 +- .../src/main/resources/swift/APIs.mustache | 6 ++-- .../swift/AlamofireImplementations.mustache | 2 +- .../main/resources/swift/Cartfile.mustache | 3 +- .../main/resources/swift/Extensions.mustache | 33 +++++++++++++++++++ .../src/main/resources/swift/api.mustache | 7 ++-- 6 files changed, 44 insertions(+), 9 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/SwiftGenerator.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/SwiftGenerator.java index 39a71449d5e..53097c35736 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/SwiftGenerator.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/SwiftGenerator.java @@ -57,7 +57,7 @@ public class SwiftGenerator extends DefaultCodegen implements CodegenConfig { sourceFolder = appName + "/" + sourceFolder; - supportingFiles.add(new SupportingFile("Cartfile.mustache", appName, "Cartfile")); + supportingFiles.add(new SupportingFile("Cartfile.mustache", "", "Cartfile")); supportingFiles.add(new SupportingFile("APIHelper.mustache", sourceFolder, "APIHelper.swift")); supportingFiles.add(new SupportingFile("AlamofireImplementations.mustache", sourceFolder, "AlamofireImplementations.swift")); supportingFiles.add(new SupportingFile("Extensions.mustache", sourceFolder, "Extensions.swift")); diff --git a/modules/swagger-codegen/src/main/resources/swift/APIs.mustache b/modules/swagger-codegen/src/main/resources/swift/APIs.mustache index 073fbd93be7..8e8764514af 100644 --- a/modules/swagger-codegen/src/main/resources/swift/APIs.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/APIs.mustache @@ -21,9 +21,9 @@ class RequestBuilder { let parameters: [String:AnyObject]? let isBody: Bool let method: String - let URLString: URLStringConvertible + let URLString: String - required init(method: String, URLString: URLStringConvertible, parameters: [String:AnyObject]?, isBody: Bool) { + required init(method: String, URLString: String, parameters: [String:AnyObject]?, isBody: Bool) { self.method = method self.URLString = URLString self.parameters = parameters @@ -40,7 +40,7 @@ class RequestBuilder { } func addCredential() -> Self { - self.credential = OneteamAPI.credential + self.credential = {{projectName}}API.credential return self } } diff --git a/modules/swagger-codegen/src/main/resources/swift/AlamofireImplementations.mustache b/modules/swagger-codegen/src/main/resources/swift/AlamofireImplementations.mustache index 8b34587717a..71f47ad67ca 100644 --- a/modules/swagger-codegen/src/main/resources/swift/AlamofireImplementations.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/AlamofireImplementations.mustache @@ -14,7 +14,7 @@ class AlamofireRequestBuilderFactory: RequestBuilderFactory { } class AlamofireRequestBuilder: RequestBuilder { - required init(method: String, URLString: URLStringConvertible, parameters: [String : AnyObject]?, isBody: Bool) { + required init(method: String, URLString: String, parameters: [String : AnyObject]?, isBody: Bool) { super.init(method: method, URLString: URLString, parameters: parameters, isBody: isBody) } diff --git a/modules/swagger-codegen/src/main/resources/swift/Cartfile.mustache b/modules/swagger-codegen/src/main/resources/swift/Cartfile.mustache index 245a690a74b..7259ae3b6d6 100644 --- a/modules/swagger-codegen/src/main/resources/swift/Cartfile.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/Cartfile.mustache @@ -1,2 +1,3 @@ github "Alamofire/Alamofire" >= 1.2 -github "mxcl/PromiseKit" \ No newline at end of file +github "mxcl/PromiseKit" >=1.5.3 +github "SwiftyJSON/SwiftyJSON" >= 2.2 diff --git a/modules/swagger-codegen/src/main/resources/swift/Extensions.mustache b/modules/swagger-codegen/src/main/resources/swift/Extensions.mustache index d0aed716b7e..cdf5b90d216 100644 --- a/modules/swagger-codegen/src/main/resources/swift/Extensions.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/Extensions.mustache @@ -43,6 +43,12 @@ extension Request { } } +extension String: JSONDecodable { + static func decode(source: AnyObject) -> String? { + return source.description + } +} + extension Array: JSONEncodable { func encode() -> AnyObject { if Element.self is JSONEncodable { @@ -53,6 +59,33 @@ extension Array: JSONEncodable { } } +extension Dictionary: JSONEncodable, JSONDecodable { + static func decode(source: AnyObject) -> [Key:Value]? { + return nil + } + + func encode() -> AnyObject { + var dictionary = [NSObject:AnyObject]() + for (key, value) in self { + let keyObject: NSObject + if Key.self is JSONEncodable { + keyObject = (key as! JSONEncodable).encode() as! NSObject + } else { + keyObject = key as! NSObject + } + let valueObject: AnyObject + if Value.self is JSONEncodable { + valueObject = (value as! JSONEncodable).encode() + } else { + valueObject = value as! NSObject + } + + dictionary[keyObject] = valueObject + } + return dictionary + } +} + extension JSON { func decode() -> Bool? { return self.bool diff --git a/modules/swagger-codegen/src/main/resources/swift/api.mustache b/modules/swagger-codegen/src/main/resources/swift/api.mustache index 0277ec8dfa3..28f14853fcd 100644 --- a/modules/swagger-codegen/src/main/resources/swift/api.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/api.mustache @@ -35,14 +35,15 @@ extension {{projectName}}API { path = path.stringByReplacingOccurrencesOfString("{{=<% %>=}}{<%paramName%>}<%={{ }}=%>", withString: "\({{paramName}})", options: .LiteralSearch, range: nil){{/pathParams}} let url = {{projectName}}API.basePath + path {{#bodyParam}} - let parameters = {{paramName}}.encode() as! [String:AnyObject]{{/bodyParam}}{{^bodyParam}} + let parameters = {{paramName}}{{^required}}?{{/required}}.encode() as? [String:AnyObject]{{/bodyParam}}{{^bodyParam}} let nillableParameters: [String:AnyObject?] = {{^queryParams}}[:]{{/queryParams}}{{#queryParams}}{{^secondaryParam}}[{{/secondaryParam}} "{{paramName}}": {{paramName}}{{#hasMore}},{{/hasMore}}{{^hasMore}} ]{{/hasMore}}{{/queryParams}} let parameters = APIHelper.rejectNil(nillableParameters){{/bodyParam}} - return {{projectName}}API.requestBuilderFactory.getBuilder() - (method: "{{httpMethod}}", URLString: url, parameters: parameters, isBody: {{^queryParams}}true{{/queryParams}}{{#queryParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/queryParams}}) + let requestBuilder: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Empty{{/returnType}}>.Type = {{projectName}}API.requestBuilderFactory.getBuilder() + + return requestBuilder(method: "{{httpMethod}}", URLString: url, parameters: parameters, isBody: {{^queryParams}}true{{/queryParams}}{{#queryParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/queryParams}}) } {{/operation}} }