Skip to content

Commit

Permalink
Fix double encoding of path parameters (#15)
Browse files Browse the repository at this point in the history
Fix double encoding of path parameters

### Motivation

Fixes apple/swift-openapi-generator#251.

### Modifications

Use the already escaped path setter on `URLComponents` to avoid the second encoding pass.

### Result

Path parameters that needed escaping are only escaped once, not twice.

### Test Plan

Adapted the existing unit test to cover a path item that needs escaping.


Reviewed by: simonjbeaumont

Builds:
     ✔︎ pull request validation (5.8) - Build finished. 
     ✔︎ pull request validation (5.9) - Build finished. 
     ✔︎ pull request validation (nightly) - Build finished. 
     ✔︎ pull request validation (soundness) - Build finished. 

#15
  • Loading branch information
czechboy0 authored Sep 7, 2023
1 parent 902cfc7 commit 7e40bff
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public struct AsyncHTTPClientTransport: ClientTransport {
guard var baseUrlComponents = URLComponents(string: baseURL.absoluteString) else {
throw Error.invalidRequestURL(request: request, baseURL: baseURL)
}
baseUrlComponents.path += request.path
baseUrlComponents.percentEncodedPath += request.path
baseUrlComponents.percentEncodedQuery = request.query
guard let url = baseUrlComponents.url else {
throw Error.invalidRequestURL(request: request, baseURL: baseURL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Test_AsyncHTTPClientTransport: XCTestCase {

func testConvertRequest() throws {
let request: OpenAPIRuntime.Request = .init(
path: "/hello/Maria",
path: "/hello%20world/Maria",
query: "greeting=Howdy",
method: .post,
headerFields: [
Expand All @@ -50,7 +50,7 @@ class Test_AsyncHTTPClientTransport: XCTestCase {
request,
baseURL: try XCTUnwrap(URL(string: "http://example.com/api/v1"))
)
XCTAssertEqual(httpRequest.url, "http://example.com/api/v1/hello/Maria?greeting=Howdy")
XCTAssertEqual(httpRequest.url, "http://example.com/api/v1/hello%20world/Maria?greeting=Howdy")
XCTAssertEqual(httpRequest.method, .POST)
XCTAssertEqual(
httpRequest.headers,
Expand Down

0 comments on commit 7e40bff

Please sign in to comment.