Swift3: properly percent-escape path parameters (#6705)

* Add addiitional files from upstream

* Remove mis-added files

* Swift3: Properly percent-escape path parameters

This change fixes the following issue:

https://github.com/swagger-api/swagger-codegen/issues/6400

The problem was that path parameters were not properly percent-escaped before being placed into the URL path. This leads to creation of an invalid URL, which then fails.

So therefore, in the API template where path parameters are handled, we propertly percent escape them, using the characters which are allowed in URL paths.

In addition to this template change, then this PR includes the following changes:

1. Resulting changes in all generated code due to the above template change.
2. I added the objcCompatible run to the swift3-petstore-all.sh so that I could re-generated all of the generated code with a single script.
3. I added a unit test in UserAPITests.swift which verifies that paths are properly escaped.
4. In order to make the unit test work, then I needed access to RequestBuilder<T>.URLString to verify that the path was properly escaped. However, RequestBuilder<T>.URLString had "internal" access control so it was inaccessible from the unit test. So therefore, I made four contants in RequestBuilder<T> to be public. This should not harm anything, since they are constants ("let's") and cannot be changed from the outside of the class after initialization.
5. There were also some stray changes which look like they were caused by having not run bin/swift3-petstore-all.sh in a while.
This commit is contained in:
ehyche
2017-10-30 03:40:00 -04:00
committed by wing328
parent bc302f7151
commit 95ef1bf62e
84 changed files with 5188 additions and 69 deletions

View File

@@ -32,10 +32,10 @@ open class APIBase {
open class RequestBuilder<T> {
var credential: URLCredential?
var headers: [String:String]
let parameters: Any?
let isBody: Bool
let method: String
let URLString: String
public let parameters: 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) -> ())?

View File

@@ -1,5 +1,5 @@
//
// AnotherFakeAPI.swift
// AnotherfakeAPI.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
@@ -9,7 +9,7 @@ import Foundation
import Alamofire
open class AnotherFakeAPI: APIBase {
open class AnotherfakeAPI: APIBase {
/**
To test special tags
- parameter body: (body) client model

View File

@@ -37,7 +37,7 @@ open class Fake_classname_tags123API: APIBase {
open class func testClassnameWithRequestBuilder(body: Client) -> RequestBuilder<Client> {
let path = "/fake_classname_test"
let URLString = PetstoreClientAPI.basePath + path
let parameters = body.encodeToJSON() as? [String:AnyObject]
let parameters = body.encodeToJSON()
let url = NSURLComponents(string: URLString)

View File

@@ -70,7 +70,9 @@ open class PetAPI: APIBase {
*/
open class func deletePetWithRequestBuilder(petId: Int64, apiKey: String? = nil) -> RequestBuilder<Void> {
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
@@ -412,7 +414,9 @@ open class PetAPI: APIBase {
*/
open class func getPetByIdWithRequestBuilder(petId: Int64) -> RequestBuilder<Pet> {
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
@@ -485,7 +489,9 @@ open class PetAPI: APIBase {
*/
open class func updatePetWithFormWithRequestBuilder(petId: Int64, name: String? = nil, status: String? = nil) -> RequestBuilder<Void> {
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,
@@ -535,7 +541,9 @@ open class PetAPI: APIBase {
*/
open class func uploadFileWithRequestBuilder(petId: Int64, additionalMetadata: String? = nil, file: URL? = nil) -> RequestBuilder<ApiResponse> {
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,

View File

@@ -32,7 +32,9 @@ open class StoreAPI: APIBase {
*/
open class func deleteOrderWithRequestBuilder(orderId: String) -> RequestBuilder<Void> {
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
@@ -130,7 +132,9 @@ open class StoreAPI: APIBase {
*/
open class func getOrderByIdWithRequestBuilder(orderId: Int64) -> RequestBuilder<Order> {
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

View File

@@ -128,7 +128,9 @@ open class UserAPI: APIBase {
*/
open class func deleteUserWithRequestBuilder(username: String) -> RequestBuilder<Void> {
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
@@ -199,7 +201,9 @@ open class UserAPI: APIBase {
*/
open class func getUserByNameWithRequestBuilder(username: String) -> RequestBuilder<User> {
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
@@ -306,7 +310,9 @@ open class UserAPI: APIBase {
*/
open class func updateUserWithRequestBuilder(username: String, body: User) -> RequestBuilder<Void> {
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 = body.encodeToJSON()