mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-08 12:26:11 +00:00
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:
@@ -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) -> ())?
|
||||
|
||||
@@ -120,7 +120,9 @@ open class {{classname}}: APIBase {
|
||||
*/
|
||||
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 = {{paramName}}{{^required}}?{{/required}}.encodeToJSON()
|
||||
|
||||
Reference in New Issue
Block a user