forked from loafle/openapi-generator-original
		
	[Swift3] escape URL parameters (#7529)
* Escape URL parameters for Swift4 and update its Petstore sample * Update the code style * Update code style * Update code style * Remove break statements in switch for AlamofireImplementations
This commit is contained in:
		
							parent
							
								
									52f606b8dc
								
							
						
					
					
						commit
						b184fb1d9c
					
				@ -66,16 +66,12 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
 | 
			
		||||
                        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
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                },
 | 
			
		||||
 | 
			
		||||
@ -16,10 +16,10 @@ open class {{projectName}}API {
 | 
			
		||||
open class RequestBuilder<T> {
 | 
			
		||||
    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) -> ())?
 | 
			
		||||
 | 
			
		||||
@ -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}})
 | 
			
		||||
 | 
			
		||||
@ -1 +1 @@
 | 
			
		||||
2.3.0-SNAPSHOT
 | 
			
		||||
2.4.0-SNAPSHOT
 | 
			
		||||
@ -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'
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
@ -16,10 +16,10 @@ open class PetstoreClientAPI {
 | 
			
		||||
open class RequestBuilder<T> {
 | 
			
		||||
    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) -> ())?
 | 
			
		||||
 | 
			
		||||
@ -78,7 +78,9 @@ open class PetAPI {
 | 
			
		||||
     */
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
@ -432,7 +434,9 @@ open class PetAPI {
 | 
			
		||||
     */
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
@ -513,7 +517,9 @@ open class PetAPI {
 | 
			
		||||
     */
 | 
			
		||||
    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,
 | 
			
		||||
@ -567,7 +573,9 @@ open class PetAPI {
 | 
			
		||||
     */
 | 
			
		||||
    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,
 | 
			
		||||
 | 
			
		||||
@ -35,7 +35,9 @@ open class StoreAPI {
 | 
			
		||||
     */
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
@ -139,7 +141,9 @@ open class StoreAPI {
 | 
			
		||||
     */
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -140,7 +140,9 @@ open class UserAPI {
 | 
			
		||||
     */
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
@ -214,7 +216,9 @@ open class UserAPI {
 | 
			
		||||
     */
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
@ -329,7 +333,9 @@ open class UserAPI {
 | 
			
		||||
     */
 | 
			
		||||
    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 = JSONEncodingHelper.encodingParameters(forEncodableObject: body)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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.")
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user