Skip to content

Commit 3c52f82

Browse files
committed
fix: escape paths on JSONPointer
1 parent 51de910 commit 3c52f82

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Sources/JSONPointer.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ struct JSONPointer {
1717
}
1818

1919
var path: String {
20-
return components.joined(separator: "/")
20+
return components
21+
.map {
22+
$0
23+
.replacingOccurrences(of: "~", with: "~0")
24+
.replacingOccurrences(of: "/", with: "~1")
25+
}
26+
.joined(separator: "/")
2127
}
2228

2329
func resolve(document: Any) -> Any? {

Tests/JSONSchemaTests/JSONPointerTests.swift

+12
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,16 @@ class JSONPointerTests: XCTestCase {
115115

116116
XCTAssertNil(pointer.resolve(document: document))
117117
}
118+
119+
// MARK: - #path
120+
121+
func testPath() {
122+
let pointer = JSONPointer(path: "/foo")
123+
XCTAssertEqual(pointer.path, "/foo")
124+
}
125+
126+
func testPathWithSlash() {
127+
let pointer = JSONPointer(path: "/a~1b")
128+
XCTAssertEqual(pointer.path, "/a~1b")
129+
}
118130
}

0 commit comments

Comments
 (0)