From 72d3213bbeaa79bb7d2f8ee27c552d161544e1f1 Mon Sep 17 00:00:00 2001 From: Daiki Matsudate Date: Mon, 22 Jul 2019 18:40:38 +0900 Subject: [PATCH] add Swift 5 codegen --- bin/swift5-petstore-promisekit.sh | 10 ++ bin/swift5-petstore-rxswift.sh | 10 ++ bin/swift5-petstore-unwrapRequired.sh | 10 ++ bin/swift5-petstore.sh | 10 ++ bin/swift5-test.sh | 12 +- .../codegen/languages/Swift5Codegen.java | 12 +- .../src/main/resources/swift5/APIs.mustache | 81 +++++----- .../main/resources/swift5/Cartfile.mustache | 6 +- .../main/resources/swift5/XcodeGen.mustache | 9 +- .../src/main/resources/swift5/api.mustache | 138 +++--------------- .../resources/swift5/modelObject.mustache | 43 +----- 11 files changed, 132 insertions(+), 209 deletions(-) diff --git a/bin/swift5-petstore-promisekit.sh b/bin/swift5-petstore-promisekit.sh index 729956c04b2..11e785a13ec 100755 --- a/bin/swift5-petstore-promisekit.sh +++ b/bin/swift5-petstore-promisekit.sh @@ -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 diff --git a/bin/swift5-petstore-rxswift.sh b/bin/swift5-petstore-rxswift.sh index ceec25a1558..738026f9c6c 100755 --- a/bin/swift5-petstore-rxswift.sh +++ b/bin/swift5-petstore-rxswift.sh @@ -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 \ No newline at end of file diff --git a/bin/swift5-petstore-unwrapRequired.sh b/bin/swift5-petstore-unwrapRequired.sh index 03d94c54628..3c01d1b2d6c 100755 --- a/bin/swift5-petstore-unwrapRequired.sh +++ b/bin/swift5-petstore-unwrapRequired.sh @@ -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 \ No newline at end of file diff --git a/bin/swift5-petstore.sh b/bin/swift5-petstore.sh index 4320643c54d..67aadc731f2 100755 --- a/bin/swift5-petstore.sh +++ b/bin/swift5-petstore.sh @@ -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 \ No newline at end of file diff --git a/bin/swift5-test.sh b/bin/swift5-test.sh index bc84c27303d..5feee5d0040 100755 --- a/bin/swift5-test.sh +++ b/bin/swift5-test.sh @@ -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 \ No newline at end of file diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5Codegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5Codegen.java index 5c78c0afc88..36abca1b1a2 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5Codegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5Codegen.java @@ -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; } diff --git a/modules/openapi-generator/src/main/resources/swift5/APIs.mustache b/modules/openapi-generator/src/main/resources/swift5/APIs.mustache index 6ed1abe5694..8fb1f23ff68 100644 --- a/modules/openapi-generator/src/main/resources/swift5/APIs.mustache +++ b/modules/openapi-generator/src/main/resources/swift5/APIs.mustache @@ -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 { - var credential: URLCredential? - var headers: [String:String] - public let parameters: [String:Any]? - public let isBody: Bool +public struct RequestBuilder { + 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?, _ 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() -> RequestBuilder.Type - func getBuilder() -> RequestBuilder.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(_ 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) + } } diff --git a/modules/openapi-generator/src/main/resources/swift5/Cartfile.mustache b/modules/openapi-generator/src/main/resources/swift5/Cartfile.mustache index b8548fc548c..33939b73516 100644 --- a/modules/openapi-generator/src/main/resources/swift5/Cartfile.mustache +++ b/modules/openapi-generator/src/main/resources/swift5/Cartfile.mustache @@ -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}} diff --git a/modules/openapi-generator/src/main/resources/swift5/XcodeGen.mustache b/modules/openapi-generator/src/main/resources/swift5/XcodeGen.mustache index 872c7b0f572..d307b9ac179 100644 --- a/modules/openapi-generator/src/main/resources/swift5/XcodeGen.mustache +++ b/modules/openapi-generator/src/main/resources/swift5/XcodeGen.mustache @@ -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" \ No newline at end of file + SWIFT_VERSION: "5.0" + scheme: {} +{{^onlyPlainInterface}} + dependencies: + - carthage: Alamofire{{#useRxSwift}} + - carthage: RxSwift{{/useRxSwift}}{{#usePromiseKit}} + - carthage: PromiseKit{{/usePromiseKit}}{{/onlyPlainInterface}} diff --git a/modules/openapi-generator/src/main/resources/swift5/api.mustache b/modules/openapi-generator/src/main/resources/swift5/api.mustache index c6eeab2d97e..c5ad8d429cd 100644 --- a/modules/openapi-generator/src/main/resources/swift5/api.mustache +++ b/modules/openapi-generator/src/main/resources/swift5/api.mustache @@ -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}} ] - - 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}} + parameters.append(Parameters(formParams)) + {{/hasFormParams}} + {{/bodyParam}} + + {{#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}} diff --git a/modules/openapi-generator/src/main/resources/swift5/modelObject.mustache b/modules/openapi-generator/src/main/resources/swift5/modelObject.mustache index d477ac7880d..ec0ef367aea 100644 --- a/modules/openapi-generator/src/main/resources/swift5/modelObject.mustache +++ b/modules/openapi-generator/src/main/resources/swift5/modelObject.mustache @@ -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() - {{#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}}