Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion Core/Core/Common/CommonModels/API/APIURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public struct APIURL: Codable, Equatable {
public init(from decoder: Decoder) throws {
let baseURL = AppEnvironment.shared.currentSession?.baseURL
let container = try decoder.singleValueContainer()
let string = try container.decode(String.self).removingXMLEscaping
let string = try container.decode(String.self)
.removingXMLEscaping
.removingQueryPercentEncoding
if let url = URL(string: string, relativeTo: baseURL) {
rawValue = url
return
Expand Down Expand Up @@ -69,6 +71,28 @@ extension KeyedDecodingContainer {
}
}

private extension String {

/// Keeping this private as it aims to fix a specific issue with URL strings
/// retrieved from BackEnd: Such strings get percent-encoded except for brackets
/// characters `[` & `]` in query part. Thus resulting into double encoding for some
/// other characters when fed to `URL.init(string:)` leading to failure on request.
///
/// This method removes percent-encoding entirely for the query part, so
/// it get encoded properly as a whole when using `URL.init(string:)` with
/// the resulting one. It assumes Backend perform only 1 iteration of
/// percent-encoding on URL strings.
var removingQueryPercentEncoding: String {
if var comps = URLComponents(string: self) {
if let fixedQuery = comps.query?.removingPercentEncoding {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to assume 1 level of percent-encoding on backend.
Since, we don't want for the excessive percent-encoding removal to turn such value as:

"{ \"prop\": \"%5BSome%20Value%5D\" }"

To this:

"{ \"prop\": \"[Some Value]\" }"

comps.query = fixedQuery
}
return comps.string ?? self
}
return self
}
}

#if DEBUG
extension APIURL {
public static func make(
Expand Down
27 changes: 27 additions & 0 deletions Core/CoreTests/Common/CommonModels/API/APIURLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,33 @@ class APIURLTests: CoreTestCase {
let model = try decoder.decode(MockAPIResponse.self, from: data)
XCTAssertEqual(model.avatar_url?.rawValue, URL(string: "https://example.com/path%7B%20%7D%60%7C%5C%5E")!)
}

func testURLQueryPercentEncoding() throws {
XCTAssertEqual(
try decoder.decode(APIURL.self, from: try encoder.encode("https://domain.com?q=value")),
.make(rawValue: URL(string: "https://domain.com?q=value")!)
)

XCTAssertEqual(
try decoder.decode(APIURL.self, from: try encoder.encode("https://domain.com?q=some value")),
.make(rawValue: URL(string: "https://domain.com?q=some%20value")!)
)

XCTAssertEqual(
try decoder.decode(APIURL.self, from: try encoder.encode("https://domain.com?q=[some value]")),
.make(rawValue: URL(string: "https://domain.com?q=%5Bsome%20value%5D")!)
)

XCTAssertEqual(
try decoder.decode(APIURL.self, from: try encoder.encode("https://domain.com?q=[some%20value]")),
.make(rawValue: URL(string: "https://domain.com?q=%5Bsome%20value%5D")!)
)

XCTAssertEqual(
try decoder.decode(APIURL.self, from: try encoder.encode("https://domain.com?q=[some value]&a=another%20value")),
.make(rawValue: URL(string: "https://domain.com?q=%5Bsome%20value%5D&a=another%20value")!)
)
}
}

private struct MockAPIResponse: Codable {
Expand Down