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 {
|
switch v {
|
||||||
case let fileURL as NSURL:
|
case let fileURL as NSURL:
|
||||||
mpForm.appendBodyPart(fileURL: fileURL, name: k)
|
mpForm.appendBodyPart(fileURL: fileURL, name: k)
|
||||||
break
|
|
||||||
case let string as NSString:
|
case let string as NSString:
|
||||||
mpForm.appendBodyPart(data: string.dataUsingEncoding(NSUTF8StringEncoding)!, name: k)
|
mpForm.appendBodyPart(data: string.dataUsingEncoding(NSUTF8StringEncoding)!, name: k)
|
||||||
break
|
|
||||||
case let number as NSNumber:
|
case let number as NSNumber:
|
||||||
mpForm.appendBodyPart(data: number.stringValue.dataUsingEncoding(NSUTF8StringEncoding)!, name: k)
|
mpForm.appendBodyPart(data: number.stringValue.dataUsingEncoding(NSUTF8StringEncoding)!, name: k)
|
||||||
break
|
|
||||||
default:
|
default:
|
||||||
fatalError("Unprocessable value \(v) with key \(k)")
|
fatalError("Unprocessable value \(v) with key \(k)")
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -16,10 +16,10 @@ open class {{projectName}}API {
|
|||||||
open class RequestBuilder<T> {
|
open class RequestBuilder<T> {
|
||||||
var credential: URLCredential?
|
var credential: URLCredential?
|
||||||
var headers: [String:String]
|
var headers: [String:String]
|
||||||
let parameters: [String:Any]?
|
public let parameters: [String:Any]?
|
||||||
let isBody: Bool
|
public let isBody: Bool
|
||||||
let method: String
|
public let method: String
|
||||||
let URLString: String
|
public let URLString: String
|
||||||
|
|
||||||
/// Optional block to obtain a reference to the request's progress instance when available.
|
/// Optional block to obtain a reference to the request's progress instance when available.
|
||||||
public var onProgressReady: ((Progress) -> ())?
|
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}}> {
|
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}}
|
{{^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
|
let URLString = {{projectName}}API.basePath + path
|
||||||
{{#bodyParam}}
|
{{#bodyParam}}
|
||||||
let parameters = JSONEncodingHelper.encodingParameters(forEncodableObject: {{paramName}})
|
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.name = 'SwaggerClient'
|
||||||
s.ios.deployment_target = '8.0'
|
s.ios.deployment_target = '8.0'
|
||||||
s.osx.deployment_target = '10.9'
|
s.osx.deployment_target = '10.9'
|
||||||
|
s.tvos.deployment_target = '9.0'
|
||||||
s.version = '0.0.1'
|
s.version = '0.0.1'
|
||||||
s.source = { :git => 'git@github.com:swagger-api/swagger-mustache.git', :tag => 'v1.0.0' }
|
s.source = { :git => 'git@github.com:swagger-api/swagger-mustache.git', :tag => 'v1.0.0' }
|
||||||
s.authors = 'Swagger Codegen'
|
s.authors = 'Swagger Codegen'
|
||||||
|
@ -36,7 +36,7 @@ git_remote=`git remote`
|
|||||||
if [ "$git_remote" = "" ]; then # git remote not defined
|
if [ "$git_remote" = "" ]; then # git remote not defined
|
||||||
|
|
||||||
if [ "$GIT_TOKEN" = "" ]; then
|
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
|
git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git
|
||||||
else
|
else
|
||||||
git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git
|
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> {
|
open class RequestBuilder<T> {
|
||||||
var credential: URLCredential?
|
var credential: URLCredential?
|
||||||
var headers: [String:String]
|
var headers: [String:String]
|
||||||
let parameters: [String:Any]?
|
public let parameters: [String:Any]?
|
||||||
let isBody: Bool
|
public let isBody: Bool
|
||||||
let method: String
|
public let method: String
|
||||||
let URLString: String
|
public let URLString: String
|
||||||
|
|
||||||
/// Optional block to obtain a reference to the request's progress instance when available.
|
/// Optional block to obtain a reference to the request's progress instance when available.
|
||||||
public var onProgressReady: ((Progress) -> ())?
|
public var onProgressReady: ((Progress) -> ())?
|
||||||
|
@ -78,7 +78,9 @@ open class PetAPI {
|
|||||||
*/
|
*/
|
||||||
open class func deletePetWithRequestBuilder(petId: Int64, apiKey: String? = nil) -> RequestBuilder<Void> {
|
open class func deletePetWithRequestBuilder(petId: Int64, apiKey: String? = nil) -> RequestBuilder<Void> {
|
||||||
var path = "/pet/{petId}"
|
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 URLString = PetstoreClientAPI.basePath + path
|
||||||
let parameters: [String:Any]? = nil
|
let parameters: [String:Any]? = nil
|
||||||
|
|
||||||
@ -432,7 +434,9 @@ open class PetAPI {
|
|||||||
*/
|
*/
|
||||||
open class func getPetByIdWithRequestBuilder(petId: Int64) -> RequestBuilder<Pet> {
|
open class func getPetByIdWithRequestBuilder(petId: Int64) -> RequestBuilder<Pet> {
|
||||||
var path = "/pet/{petId}"
|
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 URLString = PetstoreClientAPI.basePath + path
|
||||||
let parameters: [String:Any]? = nil
|
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> {
|
open class func updatePetWithFormWithRequestBuilder(petId: Int64, name: String? = nil, status: String? = nil) -> RequestBuilder<Void> {
|
||||||
var path = "/pet/{petId}"
|
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 URLString = PetstoreClientAPI.basePath + path
|
||||||
let formParams: [String:Any?] = [
|
let formParams: [String:Any?] = [
|
||||||
"name": name,
|
"name": name,
|
||||||
@ -567,7 +573,9 @@ open class PetAPI {
|
|||||||
*/
|
*/
|
||||||
open class func uploadFileWithRequestBuilder(petId: Int64, additionalMetadata: String? = nil, file: URL? = nil) -> RequestBuilder<ApiResponse> {
|
open class func uploadFileWithRequestBuilder(petId: Int64, additionalMetadata: String? = nil, file: URL? = nil) -> RequestBuilder<ApiResponse> {
|
||||||
var path = "/pet/{petId}/uploadImage"
|
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 URLString = PetstoreClientAPI.basePath + path
|
||||||
let formParams: [String:Any?] = [
|
let formParams: [String:Any?] = [
|
||||||
"additionalMetadata": additionalMetadata,
|
"additionalMetadata": additionalMetadata,
|
||||||
|
@ -35,7 +35,9 @@ open class StoreAPI {
|
|||||||
*/
|
*/
|
||||||
open class func deleteOrderWithRequestBuilder(orderId: String) -> RequestBuilder<Void> {
|
open class func deleteOrderWithRequestBuilder(orderId: String) -> RequestBuilder<Void> {
|
||||||
var path = "/store/order/{order_id}"
|
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 URLString = PetstoreClientAPI.basePath + path
|
||||||
let parameters: [String:Any]? = nil
|
let parameters: [String:Any]? = nil
|
||||||
|
|
||||||
@ -139,7 +141,9 @@ open class StoreAPI {
|
|||||||
*/
|
*/
|
||||||
open class func getOrderByIdWithRequestBuilder(orderId: Int64) -> RequestBuilder<Order> {
|
open class func getOrderByIdWithRequestBuilder(orderId: Int64) -> RequestBuilder<Order> {
|
||||||
var path = "/store/order/{order_id}"
|
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 URLString = PetstoreClientAPI.basePath + path
|
||||||
let parameters: [String:Any]? = nil
|
let parameters: [String:Any]? = nil
|
||||||
|
|
||||||
|
@ -140,7 +140,9 @@ open class UserAPI {
|
|||||||
*/
|
*/
|
||||||
open class func deleteUserWithRequestBuilder(username: String) -> RequestBuilder<Void> {
|
open class func deleteUserWithRequestBuilder(username: String) -> RequestBuilder<Void> {
|
||||||
var path = "/user/{username}"
|
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 URLString = PetstoreClientAPI.basePath + path
|
||||||
let parameters: [String:Any]? = nil
|
let parameters: [String:Any]? = nil
|
||||||
|
|
||||||
@ -214,7 +216,9 @@ open class UserAPI {
|
|||||||
*/
|
*/
|
||||||
open class func getUserByNameWithRequestBuilder(username: String) -> RequestBuilder<User> {
|
open class func getUserByNameWithRequestBuilder(username: String) -> RequestBuilder<User> {
|
||||||
var path = "/user/{username}"
|
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 URLString = PetstoreClientAPI.basePath + path
|
||||||
let parameters: [String:Any]? = nil
|
let parameters: [String:Any]? = nil
|
||||||
|
|
||||||
@ -329,7 +333,9 @@ open class UserAPI {
|
|||||||
*/
|
*/
|
||||||
open class func updateUserWithRequestBuilder(username: String, body: User) -> RequestBuilder<Void> {
|
open class func updateUserWithRequestBuilder(username: String, body: User) -> RequestBuilder<Void> {
|
||||||
var path = "/user/{username}"
|
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 URLString = PetstoreClientAPI.basePath + path
|
||||||
let parameters = JSONEncodingHelper.encodingParameters(forEncodableObject: body)
|
let parameters = JSONEncodingHelper.encodingParameters(forEncodableObject: body)
|
||||||
|
|
||||||
|
@ -117,5 +117,15 @@ class UserAPITests: XCTestCase {
|
|||||||
|
|
||||||
self.waitForExpectations(timeout: testTimeout, handler: nil)
|
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