diff --git a/modules/swagger-codegen/src/main/resources/swift/AlamofireImplementations.mustache b/modules/swagger-codegen/src/main/resources/swift/AlamofireImplementations.mustache index 0536080ca72..506770347a7 100644 --- a/modules/swagger-codegen/src/main/resources/swift/AlamofireImplementations.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/AlamofireImplementations.mustache @@ -66,16 +66,12 @@ class AlamofireRequestBuilder: RequestBuilder { switch v { case let fileURL as NSURL: mpForm.appendBodyPart(fileURL: fileURL, name: k) - break case let string as NSString: mpForm.appendBodyPart(data: string.dataUsingEncoding(NSUTF8StringEncoding)!, name: k) - break case let number as NSNumber: mpForm.appendBodyPart(data: number.stringValue.dataUsingEncoding(NSUTF8StringEncoding)!, name: k) - break default: fatalError("Unprocessable value \(v) with key \(k)") - break } } }, diff --git a/modules/swagger-codegen/src/main/resources/swift4/APIs.mustache b/modules/swagger-codegen/src/main/resources/swift4/APIs.mustache index 4337e40cff0..fe88150cc1e 100644 --- a/modules/swagger-codegen/src/main/resources/swift4/APIs.mustache +++ b/modules/swagger-codegen/src/main/resources/swift4/APIs.mustache @@ -16,10 +16,10 @@ open class {{projectName}}API { open class RequestBuilder { var credential: URLCredential? var headers: [String:String] - let parameters: [String:Any]? - let isBody: Bool - let method: String - let URLString: String + public let parameters: [String:Any]? + public let isBody: Bool + public let method: String + public let URLString: String /// Optional block to obtain a reference to the request's progress instance when available. public var onProgressReady: ((Progress) -> ())? diff --git a/modules/swagger-codegen/src/main/resources/swift4/api.mustache b/modules/swagger-codegen/src/main/resources/swift4/api.mustache index cc7684afbde..fcb9ddff8cd 100644 --- a/modules/swagger-codegen/src/main/resources/swift4/api.mustache +++ b/modules/swagger-codegen/src/main/resources/swift4/api.mustache @@ -106,7 +106,9 @@ open class {{classname}} { */ 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}}> { {{^pathParams}}let{{/pathParams}}{{#pathParams}}{{^secondaryParam}}var{{/secondaryParam}}{{/pathParams}} path = "{{{path}}}"{{#pathParams}} - path = path.replacingOccurrences(of: "{{=<% %>=}}{<%baseName%>}<%={{ }}=%>", with: "\({{paramName}}{{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}.rawValue{{/isContainer}}{{/isEnum}})", options: .literal, range: nil){{/pathParams}} + let {{paramName}}PreEscape = "\({{paramName}}{{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}.rawValue{{/isContainer}}{{/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 {{#bodyParam}} let parameters = JSONEncodingHelper.encodingParameters(forEncodableObject: {{paramName}}) diff --git a/samples/client/petstore-security-test/swift/.swagger-codegen/VERSION b/samples/client/petstore-security-test/swift/.swagger-codegen/VERSION index f9f7450d135..855ff9501eb 100644 --- a/samples/client/petstore-security-test/swift/.swagger-codegen/VERSION +++ b/samples/client/petstore-security-test/swift/.swagger-codegen/VERSION @@ -1 +1 @@ -2.3.0-SNAPSHOT \ No newline at end of file +2.4.0-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore-security-test/swift/SwaggerClient.podspec b/samples/client/petstore-security-test/swift/SwaggerClient.podspec index f6faf03a57a..4034f7686ee 100644 --- a/samples/client/petstore-security-test/swift/SwaggerClient.podspec +++ b/samples/client/petstore-security-test/swift/SwaggerClient.podspec @@ -2,6 +2,7 @@ Pod::Spec.new do |s| s.name = 'SwaggerClient' s.ios.deployment_target = '8.0' s.osx.deployment_target = '10.9' + s.tvos.deployment_target = '9.0' s.version = '0.0.1' s.source = { :git => 'git@github.com:swagger-api/swagger-mustache.git', :tag => 'v1.0.0' } s.authors = 'Swagger Codegen' diff --git a/samples/client/petstore-security-test/swift/git_push.sh b/samples/client/petstore-security-test/swift/git_push.sh index ed374619b13..ae01b182ae9 100644 --- a/samples/client/petstore-security-test/swift/git_push.sh +++ b/samples/client/petstore-security-test/swift/git_push.sh @@ -36,7 +36,7 @@ git_remote=`git remote` if [ "$git_remote" = "" ]; then # git remote not defined if [ "$GIT_TOKEN" = "" ]; then - echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git crediential in your environment." + echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment." git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git else git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git diff --git a/samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs.swift b/samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs.swift index 745d640ec1c..2c2fc3157de 100644 --- a/samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs.swift +++ b/samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs.swift @@ -16,10 +16,10 @@ open class PetstoreClientAPI { open class RequestBuilder { var credential: URLCredential? var headers: [String:String] - let parameters: [String:Any]? - let isBody: Bool - let method: String - let URLString: String + public let parameters: [String:Any]? + public let isBody: Bool + public let method: String + public let URLString: String /// Optional block to obtain a reference to the request's progress instance when available. public var onProgressReady: ((Progress) -> ())? diff --git a/samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/PetAPI.swift b/samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/PetAPI.swift index 6a7425564aa..af0ed4d11c7 100644 --- a/samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/PetAPI.swift +++ b/samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/PetAPI.swift @@ -78,7 +78,9 @@ open class PetAPI { */ open class func deletePetWithRequestBuilder(petId: Int64, apiKey: String? = nil) -> RequestBuilder { var path = "/pet/{petId}" - path = path.replacingOccurrences(of: "{petId}", with: "\(petId)", options: .literal, range: nil) + let petIdPreEscape = "\(petId)" + let petIdPostEscape = petIdPreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? "" + path = path.replacingOccurrences(of: "{petId}", with: petIdPostEscape, options: .literal, range: nil) let URLString = PetstoreClientAPI.basePath + path let parameters: [String:Any]? = nil @@ -432,7 +434,9 @@ open class PetAPI { */ open class func getPetByIdWithRequestBuilder(petId: Int64) -> RequestBuilder { var path = "/pet/{petId}" - path = path.replacingOccurrences(of: "{petId}", with: "\(petId)", options: .literal, range: nil) + let petIdPreEscape = "\(petId)" + let petIdPostEscape = petIdPreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? "" + path = path.replacingOccurrences(of: "{petId}", with: petIdPostEscape, options: .literal, range: nil) let URLString = PetstoreClientAPI.basePath + path let parameters: [String:Any]? = nil @@ -513,7 +517,9 @@ open class PetAPI { */ open class func updatePetWithFormWithRequestBuilder(petId: Int64, name: String? = nil, status: String? = nil) -> RequestBuilder { var path = "/pet/{petId}" - path = path.replacingOccurrences(of: "{petId}", with: "\(petId)", options: .literal, range: nil) + let petIdPreEscape = "\(petId)" + let petIdPostEscape = petIdPreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? "" + path = path.replacingOccurrences(of: "{petId}", with: petIdPostEscape, options: .literal, range: nil) let URLString = PetstoreClientAPI.basePath + path let formParams: [String:Any?] = [ "name": name, @@ -567,7 +573,9 @@ open class PetAPI { */ open class func uploadFileWithRequestBuilder(petId: Int64, additionalMetadata: String? = nil, file: URL? = nil) -> RequestBuilder { var path = "/pet/{petId}/uploadImage" - path = path.replacingOccurrences(of: "{petId}", with: "\(petId)", options: .literal, range: nil) + let petIdPreEscape = "\(petId)" + let petIdPostEscape = petIdPreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? "" + path = path.replacingOccurrences(of: "{petId}", with: petIdPostEscape, options: .literal, range: nil) let URLString = PetstoreClientAPI.basePath + path let formParams: [String:Any?] = [ "additionalMetadata": additionalMetadata, diff --git a/samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/StoreAPI.swift b/samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/StoreAPI.swift index 756ada76c42..2e050a3f04a 100644 --- a/samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/StoreAPI.swift +++ b/samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/StoreAPI.swift @@ -35,7 +35,9 @@ open class StoreAPI { */ open class func deleteOrderWithRequestBuilder(orderId: String) -> RequestBuilder { var path = "/store/order/{order_id}" - path = path.replacingOccurrences(of: "{order_id}", with: "\(orderId)", options: .literal, range: nil) + let orderIdPreEscape = "\(orderId)" + let orderIdPostEscape = orderIdPreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? "" + path = path.replacingOccurrences(of: "{order_id}", with: orderIdPostEscape, options: .literal, range: nil) let URLString = PetstoreClientAPI.basePath + path let parameters: [String:Any]? = nil @@ -139,7 +141,9 @@ open class StoreAPI { */ open class func getOrderByIdWithRequestBuilder(orderId: Int64) -> RequestBuilder { var path = "/store/order/{order_id}" - path = path.replacingOccurrences(of: "{order_id}", with: "\(orderId)", options: .literal, range: nil) + let orderIdPreEscape = "\(orderId)" + let orderIdPostEscape = orderIdPreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? "" + path = path.replacingOccurrences(of: "{order_id}", with: orderIdPostEscape, options: .literal, range: nil) let URLString = PetstoreClientAPI.basePath + path let parameters: [String:Any]? = nil diff --git a/samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/UserAPI.swift b/samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/UserAPI.swift index cbc7bd93258..185fdac7977 100644 --- a/samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/UserAPI.swift +++ b/samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/UserAPI.swift @@ -140,7 +140,9 @@ open class UserAPI { */ open class func deleteUserWithRequestBuilder(username: String) -> RequestBuilder { var path = "/user/{username}" - path = path.replacingOccurrences(of: "{username}", with: "\(username)", options: .literal, range: nil) + let usernamePreEscape = "\(username)" + let usernamePostEscape = usernamePreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? "" + path = path.replacingOccurrences(of: "{username}", with: usernamePostEscape, options: .literal, range: nil) let URLString = PetstoreClientAPI.basePath + path let parameters: [String:Any]? = nil @@ -214,7 +216,9 @@ open class UserAPI { */ open class func getUserByNameWithRequestBuilder(username: String) -> RequestBuilder { var path = "/user/{username}" - path = path.replacingOccurrences(of: "{username}", with: "\(username)", options: .literal, range: nil) + let usernamePreEscape = "\(username)" + let usernamePostEscape = usernamePreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? "" + path = path.replacingOccurrences(of: "{username}", with: usernamePostEscape, options: .literal, range: nil) let URLString = PetstoreClientAPI.basePath + path let parameters: [String:Any]? = nil @@ -329,7 +333,9 @@ open class UserAPI { */ open class func updateUserWithRequestBuilder(username: String, body: User) -> RequestBuilder { var path = "/user/{username}" - path = path.replacingOccurrences(of: "{username}", with: "\(username)", options: .literal, range: nil) + let usernamePreEscape = "\(username)" + let usernamePostEscape = usernamePreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? "" + path = path.replacingOccurrences(of: "{username}", with: usernamePostEscape, options: .literal, range: nil) let URLString = PetstoreClientAPI.basePath + path let parameters = JSONEncodingHelper.encodingParameters(forEncodableObject: body) diff --git a/samples/client/petstore/swift4/default/SwaggerClientTests/SwaggerClientTests/UserAPITests.swift b/samples/client/petstore/swift4/default/SwaggerClientTests/SwaggerClientTests/UserAPITests.swift index 103ee1b90d1..51b4bcab89d 100644 --- a/samples/client/petstore/swift4/default/SwaggerClientTests/SwaggerClientTests/UserAPITests.swift +++ b/samples/client/petstore/swift4/default/SwaggerClientTests/SwaggerClientTests/UserAPITests.swift @@ -117,5 +117,15 @@ class UserAPITests: XCTestCase { self.waitForExpectations(timeout: testTimeout, handler: nil) } + + func testPathParamsAreEscaped() { + // The path for this operation is /user/{userId}. In order to make a usable path, + // then we must make sure that {userId} is percent-escaped when it is substituted + // into the path. So we intentionally introduce a path with spaces. + let userRequestBuilder = UserAPI.getUserByNameWithRequestBuilder(username: "User Name With Spaces") + let urlContainsSpace = userRequestBuilder.URLString.contains(" ") + + XCTAssert(!urlContainsSpace, "Expected URL to be escaped, but it was not.") + } }