From 8d34af5db44be4825749259dbb38e5e11a4c48da Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Thu, 7 Sep 2023 14:54:29 +0200 Subject: [PATCH] Fix double encoding of path parameters (#14) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix double encoding of path parameters ### Motivation Fixes https://github.com/apple/swift-openapi-generator/issues/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. https://github.com/apple/swift-openapi-urlsession/pull/14 --- Sources/OpenAPIURLSession/URLSessionTransport.swift | 2 +- Tests/OpenAPIURLSessionTests/URLSessionTransportTests.swift | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/OpenAPIURLSession/URLSessionTransport.swift b/Sources/OpenAPIURLSession/URLSessionTransport.swift index 7f73ab4..1561969 100644 --- a/Sources/OpenAPIURLSession/URLSessionTransport.swift +++ b/Sources/OpenAPIURLSession/URLSessionTransport.swift @@ -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) diff --git a/Tests/OpenAPIURLSessionTests/URLSessionTransportTests.swift b/Tests/OpenAPIURLSessionTests/URLSessionTransportTests.swift index 6e9d02b..25411b8 100644 --- a/Tests/OpenAPIURLSessionTests/URLSessionTransportTests.swift +++ b/Tests/OpenAPIURLSessionTests/URLSessionTransportTests.swift @@ -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: [ @@ -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))