Skip to content

Commit b184fb1

Browse files
xiaochowwing328
authored andcommitted
[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
1 parent 52f606b commit b184fb1

File tree

11 files changed

+51
-24
lines changed

11 files changed

+51
-24
lines changed

modules/swagger-codegen/src/main/resources/swift/AlamofireImplementations.mustache

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,12 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
6666
switch v {
6767
case let fileURL as NSURL:
6868
mpForm.appendBodyPart(fileURL: fileURL, name: k)
69-
break
7069
case let string as NSString:
7170
mpForm.appendBodyPart(data: string.dataUsingEncoding(NSUTF8StringEncoding)!, name: k)
72-
break
7371
case let number as NSNumber:
7472
mpForm.appendBodyPart(data: number.stringValue.dataUsingEncoding(NSUTF8StringEncoding)!, name: k)
75-
break
7673
default:
7774
fatalError("Unprocessable value \(v) with key \(k)")
78-
break
7975
}
8076
}
8177
},

modules/swagger-codegen/src/main/resources/swift4/APIs.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ open class {{projectName}}API {
1616
open class RequestBuilder<T> {
1717
var credential: URLCredential?
1818
var headers: [String:String]
19-
let parameters: [String:Any]?
20-
let isBody: Bool
21-
let method: String
22-
let URLString: String
19+
public let parameters: [String:Any]?
20+
public let isBody: Bool
21+
public let method: String
22+
public let URLString: String
2323
2424
/// Optional block to obtain a reference to the request's progress instance when available.
2525
public var onProgressReady: ((Progress) -> ())?

modules/swagger-codegen/src/main/resources/swift4/api.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ open class {{classname}} {
106106
*/
107107
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}}> {
108108
{{^pathParams}}let{{/pathParams}}{{#pathParams}}{{^secondaryParam}}var{{/secondaryParam}}{{/pathParams}} path = "{{{path}}}"{{#pathParams}}
109-
path = path.replacingOccurrences(of: "{{=<% %>=}}{<%baseName%>}<%={{ }}=%>", with: "\({{paramName}}{{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}.rawValue{{/isContainer}}{{/isEnum}})", options: .literal, range: nil){{/pathParams}}
109+
let {{paramName}}PreEscape = "\({{paramName}}{{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}.rawValue{{/isContainer}}{{/isEnum}})"
110+
let {{paramName}}PostEscape = {{paramName}}PreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""
111+
path = path.replacingOccurrences(of: "{{=<% %>=}}{<%baseName%>}<%={{ }}=%>", with: {{paramName}}PostEscape, options: .literal, range: nil){{/pathParams}}
110112
let URLString = {{projectName}}API.basePath + path
111113
{{#bodyParam}}
112114
let parameters = JSONEncodingHelper.encodingParameters(forEncodableObject: {{paramName}})
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.3.0-SNAPSHOT
1+
2.4.0-SNAPSHOT

samples/client/petstore-security-test/swift/SwaggerClient.podspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Pod::Spec.new do |s|
22
s.name = 'SwaggerClient'
33
s.ios.deployment_target = '8.0'
44
s.osx.deployment_target = '10.9'
5+
s.tvos.deployment_target = '9.0'
56
s.version = '0.0.1'
67
s.source = { :git => '[email protected]:swagger-api/swagger-mustache.git', :tag => 'v1.0.0' }
78
s.authors = 'Swagger Codegen'

samples/client/petstore-security-test/swift/git_push.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ git_remote=`git remote`
3636
if [ "$git_remote" = "" ]; then # git remote not defined
3737

3838
if [ "$GIT_TOKEN" = "" ]; then
39-
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git crediential in your environment."
39+
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
4040
git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git
4141
else
4242
git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git

samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ open class PetstoreClientAPI {
1616
open class RequestBuilder<T> {
1717
var credential: URLCredential?
1818
var headers: [String:String]
19-
let parameters: [String:Any]?
20-
let isBody: Bool
21-
let method: String
22-
let URLString: String
19+
public let parameters: [String:Any]?
20+
public let isBody: Bool
21+
public let method: String
22+
public let URLString: String
2323

2424
/// Optional block to obtain a reference to the request's progress instance when available.
2525
public var onProgressReady: ((Progress) -> ())?

samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/PetAPI.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ open class PetAPI {
7878
*/
7979
open class func deletePetWithRequestBuilder(petId: Int64, apiKey: String? = nil) -> RequestBuilder<Void> {
8080
var path = "/pet/{petId}"
81-
path = path.replacingOccurrences(of: "{petId}", with: "\(petId)", options: .literal, range: nil)
81+
let petIdPreEscape = "\(petId)"
82+
let petIdPostEscape = petIdPreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""
83+
path = path.replacingOccurrences(of: "{petId}", with: petIdPostEscape, options: .literal, range: nil)
8284
let URLString = PetstoreClientAPI.basePath + path
8385
let parameters: [String:Any]? = nil
8486

@@ -432,7 +434,9 @@ open class PetAPI {
432434
*/
433435
open class func getPetByIdWithRequestBuilder(petId: Int64) -> RequestBuilder<Pet> {
434436
var path = "/pet/{petId}"
435-
path = path.replacingOccurrences(of: "{petId}", with: "\(petId)", options: .literal, range: nil)
437+
let petIdPreEscape = "\(petId)"
438+
let petIdPostEscape = petIdPreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""
439+
path = path.replacingOccurrences(of: "{petId}", with: petIdPostEscape, options: .literal, range: nil)
436440
let URLString = PetstoreClientAPI.basePath + path
437441
let parameters: [String:Any]? = nil
438442

@@ -513,7 +517,9 @@ open class PetAPI {
513517
*/
514518
open class func updatePetWithFormWithRequestBuilder(petId: Int64, name: String? = nil, status: String? = nil) -> RequestBuilder<Void> {
515519
var path = "/pet/{petId}"
516-
path = path.replacingOccurrences(of: "{petId}", with: "\(petId)", options: .literal, range: nil)
520+
let petIdPreEscape = "\(petId)"
521+
let petIdPostEscape = petIdPreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""
522+
path = path.replacingOccurrences(of: "{petId}", with: petIdPostEscape, options: .literal, range: nil)
517523
let URLString = PetstoreClientAPI.basePath + path
518524
let formParams: [String:Any?] = [
519525
"name": name,
@@ -567,7 +573,9 @@ open class PetAPI {
567573
*/
568574
open class func uploadFileWithRequestBuilder(petId: Int64, additionalMetadata: String? = nil, file: URL? = nil) -> RequestBuilder<ApiResponse> {
569575
var path = "/pet/{petId}/uploadImage"
570-
path = path.replacingOccurrences(of: "{petId}", with: "\(petId)", options: .literal, range: nil)
576+
let petIdPreEscape = "\(petId)"
577+
let petIdPostEscape = petIdPreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""
578+
path = path.replacingOccurrences(of: "{petId}", with: petIdPostEscape, options: .literal, range: nil)
571579
let URLString = PetstoreClientAPI.basePath + path
572580
let formParams: [String:Any?] = [
573581
"additionalMetadata": additionalMetadata,

samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/StoreAPI.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ open class StoreAPI {
3535
*/
3636
open class func deleteOrderWithRequestBuilder(orderId: String) -> RequestBuilder<Void> {
3737
var path = "/store/order/{order_id}"
38-
path = path.replacingOccurrences(of: "{order_id}", with: "\(orderId)", options: .literal, range: nil)
38+
let orderIdPreEscape = "\(orderId)"
39+
let orderIdPostEscape = orderIdPreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""
40+
path = path.replacingOccurrences(of: "{order_id}", with: orderIdPostEscape, options: .literal, range: nil)
3941
let URLString = PetstoreClientAPI.basePath + path
4042
let parameters: [String:Any]? = nil
4143

@@ -139,7 +141,9 @@ open class StoreAPI {
139141
*/
140142
open class func getOrderByIdWithRequestBuilder(orderId: Int64) -> RequestBuilder<Order> {
141143
var path = "/store/order/{order_id}"
142-
path = path.replacingOccurrences(of: "{order_id}", with: "\(orderId)", options: .literal, range: nil)
144+
let orderIdPreEscape = "\(orderId)"
145+
let orderIdPostEscape = orderIdPreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""
146+
path = path.replacingOccurrences(of: "{order_id}", with: orderIdPostEscape, options: .literal, range: nil)
143147
let URLString = PetstoreClientAPI.basePath + path
144148
let parameters: [String:Any]? = nil
145149

samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/UserAPI.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ open class UserAPI {
140140
*/
141141
open class func deleteUserWithRequestBuilder(username: String) -> RequestBuilder<Void> {
142142
var path = "/user/{username}"
143-
path = path.replacingOccurrences(of: "{username}", with: "\(username)", options: .literal, range: nil)
143+
let usernamePreEscape = "\(username)"
144+
let usernamePostEscape = usernamePreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""
145+
path = path.replacingOccurrences(of: "{username}", with: usernamePostEscape, options: .literal, range: nil)
144146
let URLString = PetstoreClientAPI.basePath + path
145147
let parameters: [String:Any]? = nil
146148

@@ -214,7 +216,9 @@ open class UserAPI {
214216
*/
215217
open class func getUserByNameWithRequestBuilder(username: String) -> RequestBuilder<User> {
216218
var path = "/user/{username}"
217-
path = path.replacingOccurrences(of: "{username}", with: "\(username)", options: .literal, range: nil)
219+
let usernamePreEscape = "\(username)"
220+
let usernamePostEscape = usernamePreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""
221+
path = path.replacingOccurrences(of: "{username}", with: usernamePostEscape, options: .literal, range: nil)
218222
let URLString = PetstoreClientAPI.basePath + path
219223
let parameters: [String:Any]? = nil
220224

@@ -329,7 +333,9 @@ open class UserAPI {
329333
*/
330334
open class func updateUserWithRequestBuilder(username: String, body: User) -> RequestBuilder<Void> {
331335
var path = "/user/{username}"
332-
path = path.replacingOccurrences(of: "{username}", with: "\(username)", options: .literal, range: nil)
336+
let usernamePreEscape = "\(username)"
337+
let usernamePostEscape = usernamePreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""
338+
path = path.replacingOccurrences(of: "{username}", with: usernamePostEscape, options: .literal, range: nil)
333339
let URLString = PetstoreClientAPI.basePath + path
334340
let parameters = JSONEncodingHelper.encodingParameters(forEncodableObject: body)
335341

0 commit comments

Comments
 (0)