Skip to content

Commit 70b9c43

Browse files
authored
Merge pull request #273 from mattpolzin/bugfix/2x/path-trailing-slash
support trailing slashes in paths (which are sometimes required by APIs)
2 parents d3c27d8 + b5f5c3a commit 70b9c43

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Sources/OpenAPIKit/Path Item/PathItem.swift

+10-2
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,28 @@ extension OpenAPI {
1212
/// and [OpenAPI Patterned Fields](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#patterned-fields).
1313
public struct Path: RawRepresentable, Equatable, Hashable {
1414
public let components: [String]
15+
public let trailingSlash: Bool
1516

16-
public init(_ components: [String]) {
17+
public init(_ components: [String], trailingSlash: Bool = false) {
1718
self.components = components
19+
self.trailingSlash = trailingSlash
1820
}
1921

2022
public init(rawValue: String) {
2123
let pathComponents = rawValue.split(separator: "/").map(String.init)
2224
components = pathComponents.count > 0 && pathComponents[0].isEmpty
2325
? Array(pathComponents.dropFirst())
2426
: pathComponents
27+
trailingSlash = rawValue.hasSuffix("/")
2528
}
2629

2730
public var rawValue: String {
28-
return "/\(components.joined(separator: "/"))"
31+
let path =
32+
"/\(components.joined(separator: "/"))"
33+
34+
let suffix = trailingSlash ? "/" : ""
35+
36+
return path + suffix
2937
}
3038
}
3139
}

Tests/OpenAPIKitTests/Path Item/PathItemTests.swift

+5
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,22 @@ final class PathItemTests: XCTestCase {
1515
let t3 = OpenAPI.Path(rawValue: "hello/world")
1616
let t4: OpenAPI.Path = "/hello/world"
1717
let t5: OpenAPI.Path = "hello/world"
18+
let t6: OpenAPI.Path = "hello/world/"
19+
let t7 = OpenAPI.Path(["hello", "world"], trailingSlash: true)
1820

1921
XCTAssertEqual(t1, t2)
2022
XCTAssertEqual(t2, t3)
2123
XCTAssertEqual(t3, t4)
2224
XCTAssertEqual(t4, t5)
25+
XCTAssertNotEqual(t5,t6)
26+
XCTAssertEqual(t6, t7)
2327

2428
XCTAssertEqual(t1.rawValue, "/hello/world")
2529
XCTAssertEqual(t2.rawValue, "/hello/world")
2630
XCTAssertEqual(t3.rawValue, "/hello/world")
2731
XCTAssertEqual(t4.rawValue, "/hello/world")
2832
XCTAssertEqual(t5.rawValue, "/hello/world")
33+
XCTAssertEqual(t6.rawValue, "/hello/world/")
2934
}
3035

3136
func test_initializePathItem() {

0 commit comments

Comments
 (0)