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 $@"
|
||||
|
||||
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 $@"
|
||||
|
||||
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 $@"
|
||||
|
||||
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 $@"
|
||||
|
||||
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.
|
||||
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
|
||||
|
||||
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);
|
||||
|
||||
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
|
||||
if (additionalProperties.containsKey(RESPONSE_AS)) {
|
||||
Object responseAsObject = additionalProperties.get(RESPONSE_AS);
|
||||
@ -334,6 +339,7 @@ public class Swift5Codegen extends DefaultCodegen implements CodegenConfig {
|
||||
}
|
||||
}
|
||||
additionalProperties.put(RESPONSE_AS, responseAs);
|
||||
|
||||
if (Boolean.FALSE.equals(onlyPlainInterface)) {
|
||||
if (ArrayUtils.contains(responseAs, LIBRARY_PROMISE_KIT)) {
|
||||
additionalProperties.put("usePromiseKit", true);
|
||||
@ -363,7 +369,7 @@ public class Swift5Codegen extends DefaultCodegen implements CodegenConfig {
|
||||
"",
|
||||
projectName + ".podspec"));
|
||||
supportingFiles.add(new SupportingFile("XcodeGen.mustache",
|
||||
sourceFolder,
|
||||
"",
|
||||
"project.yml"));
|
||||
if (Boolean.FALSE.equals(onlyPlainInterface)) {
|
||||
supportingFiles.add(new SupportingFile("Cartfile.mustache",
|
||||
@ -691,6 +697,10 @@ public class Swift5Codegen extends DefaultCodegen implements CodegenConfig {
|
||||
this.projectName = projectName;
|
||||
}
|
||||
|
||||
public void setOnlyPlainInterface(boolean onlyPlainInterface) {
|
||||
this.onlyPlainInterface = onlyPlainInterface;
|
||||
}
|
||||
|
||||
public void setUnwrapRequired(boolean unwrapRequired) {
|
||||
this.unwrapRequired = unwrapRequired;
|
||||
}
|
||||
|
@ -6,56 +6,49 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
open class {{projectName}}API {
|
||||
public static var basePath = "{{{basePath}}}"
|
||||
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 struct RequestBuilder<Response> {
|
||||
public let endpoint: 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 var onProgressReady: ((Progress) -> ())?
|
||||
|
||||
required public init(method: String, URLString: String, parameters: [String:Any]?, isBody: Bool, headers: [String:String] = [:]) {
|
||||
public init(endpoint: String, method: String, parameters: [Parameters] = [], headers: [String : String] = [:]) {
|
||||
self.endpoint = endpoint
|
||||
self.method = method
|
||||
self.URLString = URLString
|
||||
self.parameters = parameters
|
||||
self.isBody = isBody
|
||||
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 {
|
||||
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type
|
||||
func getBuilder<T:Decodable>() -> RequestBuilder<T>.Type
|
||||
public enum Parameters {
|
||||
case query([String: Any?])
|
||||
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 "mxcl/PromiseKit" ~> 4.4{{/usePromiseKit}}{{#useRxSwift}}
|
||||
github "ReactiveX/RxSwift" ~> 4.0{{/useRxSwift}}
|
||||
github "Alamofire/Alamofire"{{#usePromiseKit}}
|
||||
github "mxcl/PromiseKit"{{/usePromiseKit}}{{#useRxSwift}}
|
||||
github "ReactiveX/RxSwift"{{/useRxSwift}}
|
||||
|
@ -7,6 +7,13 @@ targets:
|
||||
sources: [{{projectName}}]
|
||||
info:
|
||||
path: ./Info.plist
|
||||
version: {{#podVersion}}{{podVersion}}{{/podVersion}}{{^podVersion}}{{#apiInfo}}{{version}}{{/apiInfo}}{{^apiInfo}}}0.0.1{{/apiInfo}}{{/podVersion}}
|
||||
settings:
|
||||
APPLICATION_EXTENSION_API_ONLY: true
|
||||
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 Alamofire{{#usePromiseKit}}
|
||||
import PromiseKit{{/usePromiseKit}}{{#useRxSwift}}
|
||||
import RxSwift{{/useRxSwift}}
|
||||
|
||||
{{#swiftUseApiNamespace}}
|
||||
extension {{projectName}}API {
|
||||
{{/swiftUseApiNamespace}}
|
||||
|
||||
{{#description}}
|
||||
/** {{description}} */{{/description}}
|
||||
open class {{classname}} {
|
||||
public struct {{classname}} {
|
||||
{{#operation}}
|
||||
{{#allParams}}
|
||||
{{#isEnum}}
|
||||
@ -33,90 +29,6 @@ open class {{classname}} {
|
||||
|
||||
{{/isEnum}}
|
||||
{{/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}}}
|
||||
@ -141,45 +53,37 @@ open class {{classname}} {
|
||||
{{/allParams}}
|
||||
- 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}}
|
||||
let {{paramName}}PreEscape = "\({{#isEnum}}{{paramName}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}.rawValue{{/isContainer}}{{/isEnum}}{{^isEnum}}APIHelper.mapValueToPathItem({{paramName}}){{/isEnum}})"
|
||||
let {{paramName}}PostEscape = {{paramName}}PreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""
|
||||
path = path.replacingOccurrences(of: "{{=<% %>=}}{<%baseName%>}<%={{ }}=%>", with: {{paramName}}PostEscape, options: .literal, range: nil){{/pathParams}}
|
||||
let URLString = {{projectName}}API.basePath + path
|
||||
let {{paramName}}Escaped = "\({{#isEnum}}{{paramName}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}.rawValue{{/isContainer}}{{/isEnum}}{{^isEnum}}{{paramName}})".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""{{/isEnum}}
|
||||
path = path.replacingOccurrences(of: "{{=<% %>=}}{<%baseName%>}<%={{ }}=%>", with: {{paramName}}Escaped, options: .literal, range: nil){{/pathParams}}
|
||||
{{#hasParams}}
|
||||
{{#hasFormParams}}var parameters: [Parameters] = []{{/hasFormParams}}
|
||||
{{^hasFormParams}}{{#hasQueryParams}}var parameters: [Parameters] = []{{/hasQueryParams}}{{/hasFormParams}}
|
||||
{{^hasQueryParams}}{{#hasBodyParam}}var parameters: [Parameters] = []{{/hasBodyParam}}{{/hasQueryParams}}
|
||||
|
||||
{{#bodyParam}}
|
||||
let parameters = JSONEncodingHelper.encodingParameters(forEncodableObject: {{paramName}})
|
||||
parameters.append(Parameters({{paramName}}))
|
||||
{{/bodyParam}}
|
||||
{{^bodyParam}}
|
||||
{{#hasFormParams}}
|
||||
let formParams: [String:Any?] = [
|
||||
let formParams: [String: String?] = [
|
||||
{{#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}}
|
||||
]
|
||||
parameters.append(Parameters(formParams))
|
||||
{{/hasFormParams}}
|
||||
{{/bodyParam}}
|
||||
|
||||
let nonNullParameters = APIHelper.rejectNil(formParams)
|
||||
let parameters = APIHelper.convertBoolToString(nonNullParameters)
|
||||
{{/hasFormParams}}
|
||||
{{^hasFormParams}}
|
||||
let parameters: [String:Any]? = nil
|
||||
{{/hasFormParams}}
|
||||
{{/bodyParam}}{{#hasQueryParams}}
|
||||
var url = URLComponents(string: URLString)
|
||||
url?.queryItems = APIHelper.mapValuesToQueryItems([{{^queryParams}}:{{/queryParams}}
|
||||
{{#hasQueryParams}}
|
||||
let queryParams: [String: Any?] = [
|
||||
{{#queryParams}}
|
||||
{{> _param}}{{#hasMore}}, {{/hasMore}}
|
||||
"{{baseName}}": {{paramName}}{{#hasMore}}, {{/hasMore}}
|
||||
{{/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}}
|
||||
|
||||
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}})
|
||||
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}})
|
||||
}
|
||||
|
||||
{{/operation}}
|
||||
|
@ -21,7 +21,6 @@ public struct {{classname}}: Codable {
|
||||
}{{/vendorExtensions.x-swift-optional-scalar}}{{/objcCompatible}}
|
||||
{{/isEnum}}
|
||||
{{/allVars}}
|
||||
|
||||
{{#hasVars}}
|
||||
public init({{#allVars}}{{name}}: {{{datatypeWithEnum}}}{{#unwrapRequired}}?{{/unwrapRequired}}{{^unwrapRequired}}{{^required}}?{{/required}}{{/unwrapRequired}}{{#hasMore}}, {{/hasMore}}{{/allVars}}) {
|
||||
{{#allVars}}
|
||||
@ -30,47 +29,7 @@ public struct {{classname}}: Codable {
|
||||
}
|
||||
{{/hasVars}}
|
||||
{{#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)
|
||||
}
|
||||
public var additionalProperties: [String: {{{additionalPropertiesType}}}] = [:]
|
||||
|
||||
{{/additionalPropertiesType}}
|
||||
{{^additionalPropertiesType}}{{#vendorExtensions.x-codegen-has-escaped-property-names}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user