forked from loafle/openapi-generator-original
add Swift 5 codegen
This commit is contained in:
parent
02d9440f69
commit
72d3213bbe
@ -30,3 +30,13 @@ export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
|
|||||||
ags="generate -t modules/openapi-generator/src/main/resources/swift5 -i modules/openapi-generator/src/test/resources/2_0/swift/petstore-with-fake-endpoints-models-for-testing.yaml -g swift5 -c ./bin/swift5-petstore-promisekit.json -o samples/client/petstore/swift5/promisekit --generate-alias-as-model $@"
|
ags="generate -t modules/openapi-generator/src/main/resources/swift5 -i modules/openapi-generator/src/test/resources/2_0/swift/petstore-with-fake-endpoints-models-for-testing.yaml -g swift5 -c ./bin/swift5-petstore-promisekit.json -o samples/client/petstore/swift5/promisekit --generate-alias-as-model $@"
|
||||||
|
|
||||||
java $JAVA_OPTS -jar $executable $ags
|
java $JAVA_OPTS -jar $executable $ags
|
||||||
|
|
||||||
|
if type "xcodegen" > /dev/null 2>&1; then
|
||||||
|
cd samples/client/petstore/swift5/promisekit
|
||||||
|
xcodegen generate
|
||||||
|
fi
|
||||||
|
|
||||||
|
if type "swiftlint" > /dev/null 2>&1; then
|
||||||
|
cd samples/client/petstore/swift5/promisekit
|
||||||
|
swiftlint autocorrect --quiet
|
||||||
|
fi
|
||||||
|
@ -30,3 +30,13 @@ export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
|
|||||||
ags="generate -t modules/openapi-generator/src/main/resources/swift5 -i modules/openapi-generator/src/test/resources/2_0/swift/petstore-with-fake-endpoints-models-for-testing.yaml -g swift5 -c ./bin/swift5-petstore-rxswift.json -o samples/client/petstore/swift5/rxswift --generate-alias-as-model $@"
|
ags="generate -t modules/openapi-generator/src/main/resources/swift5 -i modules/openapi-generator/src/test/resources/2_0/swift/petstore-with-fake-endpoints-models-for-testing.yaml -g swift5 -c ./bin/swift5-petstore-rxswift.json -o samples/client/petstore/swift5/rxswift --generate-alias-as-model $@"
|
||||||
|
|
||||||
java $JAVA_OPTS -jar $executable $ags
|
java $JAVA_OPTS -jar $executable $ags
|
||||||
|
|
||||||
|
if type "xcodegen" > /dev/null 2>&1; then
|
||||||
|
cd samples/client/petstore/swift5/rxswift
|
||||||
|
xcodegen generate
|
||||||
|
fi
|
||||||
|
|
||||||
|
if type "swiftlint" > /dev/null 2>&1; then
|
||||||
|
cd samples/client/petstore/swift5/promisekit
|
||||||
|
swiftlint autocorrect --quiet
|
||||||
|
fi
|
@ -30,3 +30,13 @@ export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
|
|||||||
ags="generate -t modules/openapi-generator/src/main/resources/swift5 -i modules/openapi-generator/src/test/resources/2_0/swift/petstore-with-fake-endpoints-models-for-testing.yaml -g swift5 -c ./bin/swift5-petstore-unwrapRequired.json -o samples/client/petstore/swift5/unwrapRequired --generate-alias-as-model $@"
|
ags="generate -t modules/openapi-generator/src/main/resources/swift5 -i modules/openapi-generator/src/test/resources/2_0/swift/petstore-with-fake-endpoints-models-for-testing.yaml -g swift5 -c ./bin/swift5-petstore-unwrapRequired.json -o samples/client/petstore/swift5/unwrapRequired --generate-alias-as-model $@"
|
||||||
|
|
||||||
java $JAVA_OPTS -jar $executable $ags
|
java $JAVA_OPTS -jar $executable $ags
|
||||||
|
|
||||||
|
if type "xcodegen" > /dev/null 2>&1; then
|
||||||
|
cd samples/client/petstore/swift5/unwrapRequired
|
||||||
|
xcodegen generate
|
||||||
|
fi
|
||||||
|
|
||||||
|
if type "swiftlint" > /dev/null 2>&1; then
|
||||||
|
cd samples/client/petstore/swift5/promisekit
|
||||||
|
swiftlint autocorrect --quiet
|
||||||
|
fi
|
@ -30,3 +30,13 @@ export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
|
|||||||
ags="generate -t modules/openapi-generator/src/main/resources/swift5 -i modules/openapi-generator/src/test/resources/2_0/swift/petstore-with-fake-endpoints-models-for-testing.yaml -g swift5 -c ./bin/swift5-petstore.json -o samples/client/petstore/swift5/default --generate-alias-as-model $@"
|
ags="generate -t modules/openapi-generator/src/main/resources/swift5 -i modules/openapi-generator/src/test/resources/2_0/swift/petstore-with-fake-endpoints-models-for-testing.yaml -g swift5 -c ./bin/swift5-petstore.json -o samples/client/petstore/swift5/default --generate-alias-as-model $@"
|
||||||
|
|
||||||
java $JAVA_OPTS -jar $executable $ags
|
java $JAVA_OPTS -jar $executable $ags
|
||||||
|
|
||||||
|
if type "xcodegen" > /dev/null 2>&1; then
|
||||||
|
cd samples/client/petstore/swift5/default
|
||||||
|
xcodegen generate
|
||||||
|
fi
|
||||||
|
|
||||||
|
if type "swiftlint" > /dev/null 2>&1; then
|
||||||
|
cd samples/client/petstore/swift5/promisekit
|
||||||
|
swiftlint autocorrect --quiet
|
||||||
|
fi
|
@ -27,6 +27,16 @@ fi
|
|||||||
|
|
||||||
# if you've executed sbt assembly previously it will use that instead.
|
# if you've executed sbt assembly previously it will use that instead.
|
||||||
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
|
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
|
||||||
ags="generate -t modules/openapi-generator/src/main/resources/swift5 -i modules/openapi-generator/src/test/resources/2_0/swift5Test.json -g swift5 -c ./bin/swift5-test.json -o samples/client/test/swift5default $@"
|
ags="generate -t modules/openapi-generator/src/main/resources/swift5 -i modules/openapi-generator/src/test/resources/2_0/swift4Test.json -g swift5 -c ./bin/swift5-test.json -o samples/client/test/swift5default $@"
|
||||||
|
|
||||||
java $JAVA_OPTS -jar $executable $ags
|
java $JAVA_OPTS -jar $executable $ags
|
||||||
|
|
||||||
|
if type "xcodegen" > /dev/null 2>&1; then
|
||||||
|
cd samples/client/test/swift5default
|
||||||
|
xcodegen generate
|
||||||
|
fi
|
||||||
|
|
||||||
|
if type "swiftlint" > /dev/null 2>&1; then
|
||||||
|
cd samples/client/petstore/swift5/promisekit
|
||||||
|
swiftlint autocorrect --quiet
|
||||||
|
fi
|
@ -324,6 +324,11 @@ public class Swift5Codegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
}
|
}
|
||||||
additionalProperties.put(UNWRAP_REQUIRED, unwrapRequired);
|
additionalProperties.put(UNWRAP_REQUIRED, unwrapRequired);
|
||||||
|
|
||||||
|
if (additionalProperties.containsKey(ONLY_PLAIN_INTERFACE)) {
|
||||||
|
setOnlyPlainInterface(convertPropertyToBooleanAndWriteBack(ONLY_PLAIN_INTERFACE));
|
||||||
|
}
|
||||||
|
additionalProperties.put(ONLY_PLAIN_INTERFACE, onlyPlainInterface);
|
||||||
|
|
||||||
// Setup unwrapRequired option, which makes all the properties with "required" non-optional
|
// Setup unwrapRequired option, which makes all the properties with "required" non-optional
|
||||||
if (additionalProperties.containsKey(RESPONSE_AS)) {
|
if (additionalProperties.containsKey(RESPONSE_AS)) {
|
||||||
Object responseAsObject = additionalProperties.get(RESPONSE_AS);
|
Object responseAsObject = additionalProperties.get(RESPONSE_AS);
|
||||||
@ -334,6 +339,7 @@ public class Swift5Codegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
additionalProperties.put(RESPONSE_AS, responseAs);
|
additionalProperties.put(RESPONSE_AS, responseAs);
|
||||||
|
|
||||||
if (Boolean.FALSE.equals(onlyPlainInterface)) {
|
if (Boolean.FALSE.equals(onlyPlainInterface)) {
|
||||||
if (ArrayUtils.contains(responseAs, LIBRARY_PROMISE_KIT)) {
|
if (ArrayUtils.contains(responseAs, LIBRARY_PROMISE_KIT)) {
|
||||||
additionalProperties.put("usePromiseKit", true);
|
additionalProperties.put("usePromiseKit", true);
|
||||||
@ -363,7 +369,7 @@ public class Swift5Codegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
"",
|
"",
|
||||||
projectName + ".podspec"));
|
projectName + ".podspec"));
|
||||||
supportingFiles.add(new SupportingFile("XcodeGen.mustache",
|
supportingFiles.add(new SupportingFile("XcodeGen.mustache",
|
||||||
sourceFolder,
|
"",
|
||||||
"project.yml"));
|
"project.yml"));
|
||||||
if (Boolean.FALSE.equals(onlyPlainInterface)) {
|
if (Boolean.FALSE.equals(onlyPlainInterface)) {
|
||||||
supportingFiles.add(new SupportingFile("Cartfile.mustache",
|
supportingFiles.add(new SupportingFile("Cartfile.mustache",
|
||||||
@ -691,6 +697,10 @@ public class Swift5Codegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
this.projectName = projectName;
|
this.projectName = projectName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setOnlyPlainInterface(boolean onlyPlainInterface) {
|
||||||
|
this.onlyPlainInterface = onlyPlainInterface;
|
||||||
|
}
|
||||||
|
|
||||||
public void setUnwrapRequired(boolean unwrapRequired) {
|
public void setUnwrapRequired(boolean unwrapRequired) {
|
||||||
this.unwrapRequired = unwrapRequired;
|
this.unwrapRequired = unwrapRequired;
|
||||||
}
|
}
|
||||||
|
@ -6,56 +6,49 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
open class {{projectName}}API {
|
public struct RequestBuilder<Response> {
|
||||||
public static var basePath = "{{{basePath}}}"
|
public let endpoint: String
|
||||||
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]?
|
|
||||||
public let isBody: Bool
|
|
||||||
public let method: String
|
public let method: String
|
||||||
public let URLString: String
|
public let parameters: [Parameters]
|
||||||
|
public let headers: [String : String]
|
||||||
|
|
||||||
/// Optional block to obtain a reference to the request's progress instance when available.
|
public init(endpoint: String, method: String, parameters: [Parameters] = [], headers: [String : String] = [:]) {
|
||||||
public var onProgressReady: ((Progress) -> ())?
|
self.endpoint = endpoint
|
||||||
|
|
||||||
required public init(method: String, URLString: String, parameters: [String:Any]?, isBody: Bool, headers: [String:String] = [:]) {
|
|
||||||
self.method = method
|
self.method = method
|
||||||
self.URLString = URLString
|
|
||||||
self.parameters = parameters
|
self.parameters = parameters
|
||||||
self.isBody = isBody
|
|
||||||
self.headers = headers
|
self.headers = headers
|
||||||
|
|
||||||
addHeaders({{projectName}}API.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 = {{projectName}}API.credential
|
|
||||||
return self
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public protocol RequestBuilderFactory {
|
public enum Parameters {
|
||||||
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type
|
case query([String: Any?])
|
||||||
func getBuilder<T:Decodable>() -> RequestBuilder<T>.Type
|
case form([String: String?])
|
||||||
|
case json(AnyEncodable)
|
||||||
|
|
||||||
|
public init(_ raw: [String: Any?]) {
|
||||||
|
self = .query(raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(_ raw: [String: String?]) {
|
||||||
|
self = .form(raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
public init<T: Encodable>(_ raw: T) {
|
||||||
|
self = .json(AnyEncodable(raw))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct AnyEncodable: Encodable {
|
||||||
|
var encode: (Encoder) throws -> Void
|
||||||
|
|
||||||
|
init(_ encodable: Encodable) {
|
||||||
|
func encode(to encoder: Encoder) throws {
|
||||||
|
try encodable.encode(to: encoder)
|
||||||
|
}
|
||||||
|
self.encode = encode
|
||||||
|
}
|
||||||
|
|
||||||
|
public func encode(to encoder: Encoder) throws {
|
||||||
|
try encode(encoder)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
github "Alamofire/Alamofire" ~> 4.5.0{{#usePromiseKit}}
|
github "Alamofire/Alamofire"{{#usePromiseKit}}
|
||||||
github "mxcl/PromiseKit" ~> 4.4{{/usePromiseKit}}{{#useRxSwift}}
|
github "mxcl/PromiseKit"{{/usePromiseKit}}{{#useRxSwift}}
|
||||||
github "ReactiveX/RxSwift" ~> 4.0{{/useRxSwift}}
|
github "ReactiveX/RxSwift"{{/useRxSwift}}
|
||||||
|
@ -7,6 +7,13 @@ targets:
|
|||||||
sources: [{{projectName}}]
|
sources: [{{projectName}}]
|
||||||
info:
|
info:
|
||||||
path: ./Info.plist
|
path: ./Info.plist
|
||||||
|
version: {{#podVersion}}{{podVersion}}{{/podVersion}}{{^podVersion}}{{#apiInfo}}{{version}}{{/apiInfo}}{{^apiInfo}}}0.0.1{{/apiInfo}}{{/podVersion}}
|
||||||
settings:
|
settings:
|
||||||
APPLICATION_EXTENSION_API_ONLY: true
|
APPLICATION_EXTENSION_API_ONLY: true
|
||||||
SWIFT_VERSION: "5.0"
|
SWIFT_VERSION: "5.0"
|
||||||
|
scheme: {}
|
||||||
|
{{^onlyPlainInterface}}
|
||||||
|
dependencies:
|
||||||
|
- carthage: Alamofire{{#useRxSwift}}
|
||||||
|
- carthage: RxSwift{{/useRxSwift}}{{#usePromiseKit}}
|
||||||
|
- carthage: PromiseKit{{/usePromiseKit}}{{/onlyPlainInterface}}
|
||||||
|
@ -6,17 +6,13 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import Alamofire{{#usePromiseKit}}
|
|
||||||
import PromiseKit{{/usePromiseKit}}{{#useRxSwift}}
|
|
||||||
import RxSwift{{/useRxSwift}}
|
|
||||||
|
|
||||||
{{#swiftUseApiNamespace}}
|
{{#swiftUseApiNamespace}}
|
||||||
extension {{projectName}}API {
|
extension {{projectName}}API {
|
||||||
{{/swiftUseApiNamespace}}
|
{{/swiftUseApiNamespace}}
|
||||||
|
|
||||||
{{#description}}
|
{{#description}}
|
||||||
/** {{description}} */{{/description}}
|
/** {{description}} */{{/description}}
|
||||||
open class {{classname}} {
|
public struct {{classname}} {
|
||||||
{{#operation}}
|
{{#operation}}
|
||||||
{{#allParams}}
|
{{#allParams}}
|
||||||
{{#isEnum}}
|
{{#isEnum}}
|
||||||
@ -33,90 +29,6 @@ open class {{classname}} {
|
|||||||
|
|
||||||
{{/isEnum}}
|
{{/isEnum}}
|
||||||
{{/allParams}}
|
{{/allParams}}
|
||||||
{{^usePromiseKit}}
|
|
||||||
{{^useRxSwift}}
|
|
||||||
/**
|
|
||||||
{{#summary}}
|
|
||||||
{{{summary}}}
|
|
||||||
{{/summary}}{{#allParams}}
|
|
||||||
- parameter {{paramName}}: ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/allParams}}
|
|
||||||
- parameter completion: completion handler to receive the data and the error objects
|
|
||||||
*/
|
|
||||||
open class func {{operationId}}({{#allParams}}{{paramName}}: {{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}{{{datatypeWithEnum}}}_{{operationId}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}? = nil{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#hasParams}}, {{/hasParams}}completion: @escaping ((_ data: {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}?,_ error: Error?) -> Void)) {
|
|
||||||
{{operationId}}WithRequestBuilder({{#allParams}}{{paramName}}: {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}).execute { (response, error) -> Void in
|
|
||||||
{{#returnType}}
|
|
||||||
completion(response?.body, error)
|
|
||||||
{{/returnType}}
|
|
||||||
{{^returnType}}
|
|
||||||
if error == nil {
|
|
||||||
completion((), error)
|
|
||||||
} else {
|
|
||||||
completion(nil, error)
|
|
||||||
}
|
|
||||||
{{/returnType}}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{{/useRxSwift}}
|
|
||||||
{{/usePromiseKit}}
|
|
||||||
{{#usePromiseKit}}
|
|
||||||
/**
|
|
||||||
{{#summary}}
|
|
||||||
{{{summary}}}
|
|
||||||
{{/summary}}{{#allParams}}
|
|
||||||
- parameter {{paramName}}: ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/allParams}}
|
|
||||||
- returns: Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>
|
|
||||||
*/
|
|
||||||
open class func {{operationId}}({{#allParams}} {{paramName}}: {{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}{{{datatypeWithEnum}}}_{{operationId}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}? = nil{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
|
|
||||||
let deferred = Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>.pending()
|
|
||||||
{{operationId}}WithRequestBuilder({{#allParams}}{{paramName}}: {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}).execute { (response, error) -> Void in
|
|
||||||
if let error = error {
|
|
||||||
deferred.reject(error)
|
|
||||||
{{#returnType}}
|
|
||||||
} else if let response = response {
|
|
||||||
deferred.fulfill(response.body!)
|
|
||||||
} else {
|
|
||||||
fatalError()
|
|
||||||
{{/returnType}}
|
|
||||||
{{^returnType}}
|
|
||||||
} else {
|
|
||||||
deferred.fulfill(())
|
|
||||||
{{/returnType}}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return deferred.promise
|
|
||||||
}
|
|
||||||
{{/usePromiseKit}}
|
|
||||||
{{#useRxSwift}}
|
|
||||||
/**
|
|
||||||
{{#summary}}
|
|
||||||
{{{summary}}}
|
|
||||||
{{/summary}}{{#allParams}}
|
|
||||||
- parameter {{paramName}}: ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/allParams}}
|
|
||||||
- returns: Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>
|
|
||||||
*/
|
|
||||||
open class func {{operationId}}({{#allParams}}{{paramName}}: {{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}{{{datatypeWithEnum}}}_{{operationId}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}? = nil{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
|
|
||||||
return Observable.create { observer -> Disposable in
|
|
||||||
{{operationId}}WithRequestBuilder({{#allParams}}{{paramName}}: {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}).execute { (response, error) -> Void in
|
|
||||||
if let error = error {
|
|
||||||
observer.onError(error)
|
|
||||||
{{#returnType}}
|
|
||||||
} else if let response = response {
|
|
||||||
observer.onNext(response.body!)
|
|
||||||
} else {
|
|
||||||
fatalError()
|
|
||||||
{{/returnType}}
|
|
||||||
{{^returnType}}
|
|
||||||
} else {
|
|
||||||
observer.onNext(())
|
|
||||||
{{/returnType}}
|
|
||||||
}
|
|
||||||
observer.onCompleted()
|
|
||||||
}
|
|
||||||
return Disposables.create()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{{/useRxSwift}}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
{{#summary}}
|
{{#summary}}
|
||||||
{{{summary}}}
|
{{{summary}}}
|
||||||
@ -141,45 +53,37 @@ open class {{classname}} {
|
|||||||
{{/allParams}}
|
{{/allParams}}
|
||||||
- returns: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{description}}
|
- returns: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{description}}
|
||||||
*/
|
*/
|
||||||
open class func {{operationId}}WithRequestBuilder({{#allParams}}{{paramName}}: {{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}{{{datatypeWithEnum}}}_{{operationId}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}? = nil{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
|
public func {{operationId}}({{#allParams}}{{paramName}}: {{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}{{{datatypeWithEnum}}}_{{operationId}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}? = nil{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
|
||||||
{{^pathParams}}let{{/pathParams}}{{#pathParams}}{{^secondaryParam}}var{{/secondaryParam}}{{/pathParams}} path = "{{{path}}}"{{#pathParams}}
|
{{^pathParams}}let{{/pathParams}}{{#pathParams}}{{^secondaryParam}}var{{/secondaryParam}}{{/pathParams}} path = "{{{path}}}"{{#pathParams}}
|
||||||
let {{paramName}}PreEscape = "\({{#isEnum}}{{paramName}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}.rawValue{{/isContainer}}{{/isEnum}}{{^isEnum}}APIHelper.mapValueToPathItem({{paramName}}){{/isEnum}})"
|
let {{paramName}}Escaped = "\({{#isEnum}}{{paramName}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}.rawValue{{/isContainer}}{{/isEnum}}{{^isEnum}}{{paramName}})".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""{{/isEnum}}
|
||||||
let {{paramName}}PostEscape = {{paramName}}PreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""
|
path = path.replacingOccurrences(of: "{{=<% %>=}}{<%baseName%>}<%={{ }}=%>", with: {{paramName}}Escaped, options: .literal, range: nil){{/pathParams}}
|
||||||
path = path.replacingOccurrences(of: "{{=<% %>=}}{<%baseName%>}<%={{ }}=%>", with: {{paramName}}PostEscape, options: .literal, range: nil){{/pathParams}}
|
{{#hasParams}}
|
||||||
let URLString = {{projectName}}API.basePath + path
|
{{#hasFormParams}}var parameters: [Parameters] = []{{/hasFormParams}}
|
||||||
|
{{^hasFormParams}}{{#hasQueryParams}}var parameters: [Parameters] = []{{/hasQueryParams}}{{/hasFormParams}}
|
||||||
|
{{^hasQueryParams}}{{#hasBodyParam}}var parameters: [Parameters] = []{{/hasBodyParam}}{{/hasQueryParams}}
|
||||||
|
|
||||||
{{#bodyParam}}
|
{{#bodyParam}}
|
||||||
let parameters = JSONEncodingHelper.encodingParameters(forEncodableObject: {{paramName}})
|
parameters.append(Parameters({{paramName}}))
|
||||||
{{/bodyParam}}
|
{{/bodyParam}}
|
||||||
{{^bodyParam}}
|
{{^bodyParam}}
|
||||||
{{#hasFormParams}}
|
{{#hasFormParams}}
|
||||||
let formParams: [String:Any?] = [
|
let formParams: [String: String?] = [
|
||||||
{{#formParams}}
|
{{#formParams}}
|
||||||
{{> _param}}{{#hasMore}},{{/hasMore}}
|
"{{baseName}}": {{paramName}}{{#isContainer}}{{^required}}?{{/required}}.description{{/isContainer}}{{^isContainer}}{{#isEnum}}{{^required}}?{{/required}}.rawValue{{/isEnum}}{{^isString}}{{^required}}?{{/required}}.description{{/isString}}{{/isContainer}}{{#hasMore}}, {{/hasMore}}
|
||||||
{{/formParams}}
|
{{/formParams}}
|
||||||
]
|
]
|
||||||
|
parameters.append(Parameters(formParams))
|
||||||
let nonNullParameters = APIHelper.rejectNil(formParams)
|
{{/hasFormParams}}
|
||||||
let parameters = APIHelper.convertBoolToString(nonNullParameters)
|
{{/bodyParam}}
|
||||||
{{/hasFormParams}}
|
|
||||||
{{^hasFormParams}}
|
{{#hasQueryParams}}
|
||||||
let parameters: [String:Any]? = nil
|
let queryParams: [String: Any?] = [
|
||||||
{{/hasFormParams}}
|
|
||||||
{{/bodyParam}}{{#hasQueryParams}}
|
|
||||||
var url = URLComponents(string: URLString)
|
|
||||||
url?.queryItems = APIHelper.mapValuesToQueryItems([{{^queryParams}}:{{/queryParams}}
|
|
||||||
{{#queryParams}}
|
{{#queryParams}}
|
||||||
{{> _param}}{{#hasMore}}, {{/hasMore}}
|
"{{baseName}}": {{paramName}}{{#hasMore}}, {{/hasMore}}
|
||||||
{{/queryParams}}
|
{{/queryParams}}
|
||||||
]){{/hasQueryParams}}{{^hasQueryParams}}
|
|
||||||
let url = URLComponents(string: URLString){{/hasQueryParams}}{{#headerParams}}{{^secondaryParam}}
|
|
||||||
let nillableHeaders: [String: Any?] = [{{/secondaryParam}}
|
|
||||||
{{> _param}}{{#hasMore}},{{/hasMore}}{{^hasMore}}
|
|
||||||
]
|
]
|
||||||
let headerParameters = APIHelper.rejectNilHeaders(nillableHeaders){{/hasMore}}{{/headerParams}}
|
parameters.append(Parameters(queryParams)){{/hasQueryParams}}{{/hasParams}}
|
||||||
|
return RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>(endpoint: path, method: "{{httpMethod}}"{{#hasParams}}{{#hasFormParams}}, parameters: parameters{{/hasFormParams}}{{^hasFormParams}}{{#hasQueryParams}}, parameters: parameters{{/hasQueryParams}}{{/hasFormParams}}{{^hasQueryParams}}{{#hasBodyParam}}, parameters: parameters{{/hasBodyParam}}{{/hasQueryParams}}{{/hasParams}})
|
||||||
let requestBuilder: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>.Type = {{projectName}}API.requestBuilderFactory.{{#returnType}}getBuilder(){{/returnType}}{{^returnType}}getNonDecodableBuilder(){{/returnType}}
|
|
||||||
|
|
||||||
return requestBuilder.init(method: "{{httpMethod}}", URLString: (url?.string ?? URLString), parameters: parameters, isBody: {{hasBodyParam}}{{#headerParams}}{{^secondaryParam}}, headers: headerParameters{{/secondaryParam}}{{/headerParams}})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{{/operation}}
|
{{/operation}}
|
||||||
|
@ -21,7 +21,6 @@ public struct {{classname}}: Codable {
|
|||||||
}{{/vendorExtensions.x-swift-optional-scalar}}{{/objcCompatible}}
|
}{{/vendorExtensions.x-swift-optional-scalar}}{{/objcCompatible}}
|
||||||
{{/isEnum}}
|
{{/isEnum}}
|
||||||
{{/allVars}}
|
{{/allVars}}
|
||||||
|
|
||||||
{{#hasVars}}
|
{{#hasVars}}
|
||||||
public init({{#allVars}}{{name}}: {{{datatypeWithEnum}}}{{#unwrapRequired}}?{{/unwrapRequired}}{{^unwrapRequired}}{{^required}}?{{/required}}{{/unwrapRequired}}{{#hasMore}}, {{/hasMore}}{{/allVars}}) {
|
public init({{#allVars}}{{name}}: {{{datatypeWithEnum}}}{{#unwrapRequired}}?{{/unwrapRequired}}{{^unwrapRequired}}{{^required}}?{{/required}}{{/unwrapRequired}}{{#hasMore}}, {{/hasMore}}{{/allVars}}) {
|
||||||
{{#allVars}}
|
{{#allVars}}
|
||||||
@ -30,47 +29,7 @@ public struct {{classname}}: Codable {
|
|||||||
}
|
}
|
||||||
{{/hasVars}}
|
{{/hasVars}}
|
||||||
{{#additionalPropertiesType}}
|
{{#additionalPropertiesType}}
|
||||||
public var additionalProperties: [String:{{{additionalPropertiesType}}}] = [:]
|
public var additionalProperties: [String: {{{additionalPropertiesType}}}] = [:]
|
||||||
|
|
||||||
public subscript(key: String) -> {{{additionalPropertiesType}}}? {
|
|
||||||
get {
|
|
||||||
if let value = additionalProperties[key] {
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
set {
|
|
||||||
additionalProperties[key] = newValue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encodable protocol methods
|
|
||||||
|
|
||||||
public func encode(to encoder: Encoder) throws {
|
|
||||||
|
|
||||||
var container = encoder.container(keyedBy: String.self)
|
|
||||||
|
|
||||||
{{#allVars}}
|
|
||||||
try container.encode{{#unwrapRequired}}IfPresent{{/unwrapRequired}}{{^unwrapRequired}}{{^required}}IfPresent{{/required}}{{/unwrapRequired}}({{{name}}}, forKey: "{{{baseName}}}")
|
|
||||||
{{/allVars}}
|
|
||||||
try container.encodeMap(additionalProperties)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decodable protocol methods
|
|
||||||
|
|
||||||
public init(from decoder: Decoder) throws {
|
|
||||||
let container = try decoder.container(keyedBy: String.self)
|
|
||||||
|
|
||||||
{{#allVars}}
|
|
||||||
{{name}} = try container.decode{{#unwrapRequired}}IfPresent{{/unwrapRequired}}{{^unwrapRequired}}{{^required}}IfPresent{{/required}}{{/unwrapRequired}}({{{datatypeWithEnum}}}.self, forKey: "{{{baseName}}}")
|
|
||||||
{{/allVars}}
|
|
||||||
var nonAdditionalPropertyKeys = Set<String>()
|
|
||||||
{{#allVars}}
|
|
||||||
nonAdditionalPropertyKeys.insert("{{{baseName}}}")
|
|
||||||
{{/allVars}}
|
|
||||||
additionalProperties = try container.decodeMap({{{additionalPropertiesType}}}.self, excludedKeys: nonAdditionalPropertyKeys)
|
|
||||||
}
|
|
||||||
|
|
||||||
{{/additionalPropertiesType}}
|
{{/additionalPropertiesType}}
|
||||||
{{^additionalPropertiesType}}{{#vendorExtensions.x-codegen-has-escaped-property-names}}
|
{{^additionalPropertiesType}}{{#vendorExtensions.x-codegen-has-escaped-property-names}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user