Skip to content

Commit 4fc77e4

Browse files
committed
proper escaping in json pointers
1 parent 89a0b51 commit 4fc77e4

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

src/main/kotlin/com/github/erosb/jsonsKema/DynamicPath.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ class DynamicPath {
1111
return rval
1212
}
1313

14+
fun <P> inSegmentPath(seg: List<String>, cb: () -> P?): P? {
15+
path.addAll(seg)
16+
val rval = cb()
17+
val count = seg.size
18+
for (i in 1..count) path.removeLast()
19+
return rval
20+
}
21+
22+
1423
fun asPointer(): JsonPointer = JsonPointer(path.toList())
1524

1625

src/main/kotlin/com/github/erosb/jsonsKema/JsonValue.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ data class JsonPointer(val segments: List<String>) {
2525

2626
constructor(vararg segments: String): this(listOf(*segments))
2727

28-
override fun toString() = "#" + (if (segments.isEmpty()) "" else "/") + segments.stream().collect(joining("/"))
28+
override fun toString() = "#" + (if (segments.isEmpty()) "" else "/") + segments.joinToString("/") {
29+
it.replace("~", "~0").replace("/", "~1")
30+
}
2931

3032
operator fun plus(additionalSegment: String): JsonPointer = JsonPointer(segments + additionalSegment)
3133

src/main/kotlin/com/github/erosb/jsonsKema/SchemaVisitor.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ abstract class SchemaVisitor<P> {
8888
return dynamicPath.inSegmentPath(seg, cb)
8989
}
9090

91+
protected fun inPathSegment(seg: List<String>, cb: () -> P?): P? {
92+
return dynamicPath.inSegmentPath(seg, cb)
93+
}
94+
9195
protected fun dynamicPath(): JsonPointer = dynamicPath.asPointer()
9296

9397
protected fun inPathSegment(seg: Keyword, cb: () -> P?): P? = inPathSegment(seg.value, cb)
@@ -105,7 +109,7 @@ abstract class SchemaVisitor<P> {
105109
open fun visitEnumSchema(schema: EnumSchema): P? = visitChildren(schema)
106110
open fun visitTypeSchema(schema: TypeSchema): P? = visitChildren(schema)
107111
open fun visitMultiTypeSchema(schema: MultiTypeSchema): P? = visitChildren(schema)
108-
open fun visitPropertySchema(property: String, schema: Schema): P? = inPathSegment("properties/" + property) { visitChildren(schema) }
112+
open fun visitPropertySchema(property: String, schema: Schema): P? = inPathSegment(listOf("properties", property)) { visitChildren(schema) }
109113
open fun visitPatternPropertySchema(pattern: Regexp, schema: Schema): P? = visitChildren(schema)
110114
open fun visitPatternSchema(schema: PatternSchema): P? = visitChildren(schema)
111115
open fun visitNotSchema(schema: NotSchema): P? = visitChildren(schema)

src/main/kotlin/com/github/erosb/jsonsKema/Validator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ private class DefaultValidator(
306306
}
307307

308308
override fun visitPropertySchema(property: String, schema: Schema): ValidationFailure? =
309-
inPathSegment("properties/" + property) {
309+
inPathSegment(listOf("properties", property)) {
310310
if (instance !is IJsonObject<*, *>) {
311311
return@inPathSegment null
312312
}

src/test/kotlin/com/github/erosb/jsonsKema/JsonValueTest.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ class JsonValueTest {
2828
}
2929
assertEquals("can not remove 3 segment from pointer #/seg1/seg2", thrown.message)
3030
}
31+
32+
@Test
33+
fun `segments are escaped`() {
34+
assertEquals("#/~1seg1/seg2~0", JsonPointer("/seg1", "seg2~").toString())
35+
}
3136
}

0 commit comments

Comments
 (0)