Skip to content

Commit

Permalink
Fix double encoding of path parameters (#14)
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. 

#14
  • Loading branch information
czechboy0 committed Sep 7, 2023
1 parent 294b1e9 commit 8d34af5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/OpenAPIURLSession/URLSessionTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ extension URLRequest {
guard var baseUrlComponents = URLComponents(string: baseURL.absoluteString) else {
throw URLSessionTransportError.invalidRequestURL(request: request, baseURL: baseURL)
}
baseUrlComponents.path += request.path
baseUrlComponents.percentEncodedPath += request.path
baseUrlComponents.percentEncodedQuery = request.query
guard let url = baseUrlComponents.url else {
throw URLSessionTransportError.invalidRequestURL(request: request, baseURL: baseURL)
Expand Down
4 changes: 2 additions & 2 deletions Tests/OpenAPIURLSessionTests/URLSessionTransportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class URLSessionTransportTests: XCTestCase {

func testRequestConversion() throws {
let request = OpenAPIRuntime.Request(
path: "/hello/Maria",
path: "/hello%20world/Maria",
query: "greeting=Howdy",
method: .post,
headerFields: [
Expand All @@ -41,7 +41,7 @@ class URLSessionTransportTests: XCTestCase {
body: Data("👋".utf8)
)
let urlRequest = try URLRequest(request, baseURL: URL(string: "http://example.com/api")!)
XCTAssertEqual(urlRequest.url, URL(string: "http://example.com/api/hello/Maria?greeting=Howdy"))
XCTAssertEqual(urlRequest.url, URL(string: "http://example.com/api/hello%20world/Maria?greeting=Howdy"))
XCTAssertEqual(urlRequest.httpMethod, "POST")
XCTAssertEqual(urlRequest.allHTTPHeaderFields, ["X-Mumble": "mumble"])
XCTAssertEqual(urlRequest.httpBody, Data("👋".utf8))
Expand Down

0 comments on commit 8d34af5

Please sign in to comment.