1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
/*
* Copyright 2017-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.serialization
import kotlinx.serialization.json.*
import kotlinx.serialization.test.*
import kotlin.test.*
class JsonPathTest : JsonTestBase() {
@Serializable
class Outer(val a: Int, val i: Inner)
@Serializable
class Inner(val a: Int, val b: String, val c: List<String>, val d: Map<Int, Box>)
@Serializable
class Box(val s: String)
@Test
fun testBasicError() {
expectPath("$.a") { Json.decodeFromString<Outer>("""{"a":foo}""") }
expectPath("$.i") { Json.decodeFromString<Outer>("""{"a":42, "i":[]}""") }
expectPath("$.i.b") { Json.decodeFromString<Outer>("""{"a":42, "i":{"a":43, "b":42}""") }
expectPath("$.i.b") { Json.decodeFromString<Outer>("""{"a":42, "i":{"b":42}""") }
}
@Test
fun testMissingKey() {
expectPath("$.i.d['1']") { Json.decodeFromString<Outer>("""{"a":42, "i":{"d":{1:{}}""") }
}
@Test
fun testUnknownKeyIsProperlyReported() {
expectPath("$.i") { Json.decodeFromString<Outer>("""{"a":42, "i":{"foo":42}""") }
expectPath("$") { Json.decodeFromString<Outer>("""{"x":{}, "a": 42}""") }
// The only place we have misattribution in
// Json.decodeFromString<Outer>("""{"a":42, "x":{}}""")
}
@Test
fun testMalformedRootObject() {
expectPath("$") { Json.decodeFromString<Outer>("""{{""") }
}
@Test
fun testArrayIndex() {
expectPath("$.i.c[1]") { Json.decodeFromString<Outer>("""{"a":42, "i":{ "c": ["a", 2]}""") }
expectPath("$[2]") { Json.decodeFromString<List<String>>("""["a", "2", 3]""") }
}
@Test
fun testArrayIndexMalformedArray() {
// Also zeroes as we cannot distinguish what exactly wen wrong is such cases
expectPath("$.i.c[0]") { Json.decodeFromString<Outer>("""{"a":42, "i":{ "c": [[""") }
expectPath("$[0]") { Json.decodeFromString<List<String>>("""[[""") }
// But we can here
expectPath("$.i.c\n") { Json.decodeFromString<Outer>("""{"a":42, "i":{ "c": {}}}""") }
expectPath("$\n") { Json.decodeFromString<List<String>>("""{""") }
}
@Test
fun testMapKey() {
expectPath("$.i.d\n") { Json.decodeFromString<Outer>("""{"a":42, "i":{ "d": {"foo": {}}""") }
expectPath("$.i.d\n") { Json.decodeFromString<Outer>("""{"a":42, "i":{ "d": {42: {"s":"s"}, 42.0:{}}""") }
expectPath("$\n") { Json.decodeFromString<Map<Int, String>>("""{"foo":"bar"}""") }
expectPath("$\n") { Json.decodeFromString<Map<Int, String>>("""{42:"bar", "foo":"bar"}""") }
expectPath("$['42']['foo']") { Json.decodeFromString<Map<Int, Map<String, Int>>>("""{42: {"foo":"bar"}""") }
}
@Test
fun testMalformedMap() {
expectPath("$.i.d\n") { Json.decodeFromString<Outer>("""{"a":42, "i":{ "d": []""") }
expectPath("$\n") { Json.decodeFromString<Map<Int, String>>("""[]""") }
}
@Test
fun testMapValue() {
expectPath("$.i.d['42']\n") { Json.decodeFromString<Outer>("""{"a":42, "i":{ "d": {42: {"xx":"bar"}}""") }
expectPath("$.i.d['43']\n") { Json.decodeFromString<Outer>("""{"a":42, "i":{ "d": {42: {"s":"s"}, 43: {"xx":"bar"}}}""") }
expectPath("$['239']") { Json.decodeFromString<Map<Int, String>>("""{239:bar}""") }
}
@Serializable
class Fp(val d: Double)
@Test
fun testInvalidFp() {
expectPath("$.d") { Json.decodeFromString<Fp>("""{"d": NaN}""") }
}
@Serializable
class EH(val e: E)
enum class E
@Test
fun testUnknownEnum() {
expectPath("$.e") { Json.decodeFromString<EH>("""{"e": "foo"}""") }
}
@Serializable
@SerialName("f")
sealed class Sealed {
@Serializable
@SerialName("n")
class Nesting(val f: Sealed) : Sealed()
@Serializable
@SerialName("b")
class Box(val s: String) : Sealed()
@Serializable
@SerialName("d")
class DoubleNesting(val f: Sealed, val f2: Sealed) : Sealed()
}
// TODO use non-array polymorphism when https://github.com/Kotlin/kotlinx.serialization/issues/1839 is fixed
@Test
fun testHugeNestingToCheckResize() = jvmOnly {
val json = Json { useArrayPolymorphism = true }
var outer = Sealed.Nesting(Sealed.Box("value"))
repeat(100) {
outer = Sealed.Nesting(outer)
}
val str = json.encodeToString(Sealed.serializer(), outer)
// throw-away data
json.decodeFromString(Sealed.serializer(), str)
val malformed = str.replace("\"value\"", "42")
val expectedPath = "$" + ".value.f".repeat(101) + ".value.s"
expectPath(expectedPath) { json.decodeFromString(Sealed.serializer(), malformed) }
}
@Test
fun testDoubleNesting() = jvmOnly {
val json = Json { useArrayPolymorphism = true }
var outer1 = Sealed.Nesting(Sealed.Box("correct"))
repeat(64) {
outer1 = Sealed.Nesting(outer1)
}
var outer2 = Sealed.Nesting(Sealed.Box("incorrect"))
repeat(33) {
outer2 = Sealed.Nesting(outer2)
}
val str = json.encodeToString(Sealed.serializer(), Sealed.DoubleNesting(outer1, outer2))
// throw-away data
json.decodeFromString(Sealed.serializer(), str)
val malformed = str.replace("\"incorrect\"", "42")
val expectedPath = "$.value.f2" + ".value.f".repeat(34) + ".value.s"
expectPath(expectedPath) { json.decodeFromString(Sealed.serializer(), malformed) }
}
private inline fun expectPath(path: String, block: () -> Unit) {
val message = runCatching { block() }
.exceptionOrNull()!!.message!!
assertContains(message, path)
}
}
|