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
|
/*
* Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.serialization.features
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlin.test.*
class PolymorphicDeserializationErrorMessagesTest : JsonTestBase() {
@Serializable
class DummyData(@Polymorphic val a: Any)
@Serializable
class Holder(val d: DummyData)
// TODO: remove this after #2480 is merged
private fun checkSerializationException(action: () -> Unit, assertions: SerializationException.(String) -> Unit) {
val e = assertFailsWith(SerializationException::class, action)
assertNotNull(e.message)
e.assertions(e.message!!)
}
@Test
fun testNotRegisteredMessage() = parametrizedTest { mode ->
val input = """{"d":{"a":{"type":"my.Class", "value":42}}}"""
checkSerializationException({
default.decodeFromString<Holder>(input, mode)
}, { message ->
// ReaderJsonLexer.peekLeadingMatchingValue is not implemented, so first-key optimization is not working for streaming yet.
if (mode == JsonTestingMode.STREAMING)
assertContains(message, "Unexpected JSON token at offset 10: Serializer for subclass 'my.Class' is not found in the polymorphic scope of 'Any' at path: \$.d.a")
else
assertContains(message, "Serializer for subclass 'my.Class' is not found in the polymorphic scope of 'Any'")
})
}
@Test
fun testDiscriminatorMissingNoDefaultMessage() = parametrizedTest { mode ->
val input = """{"d":{"a":{"value":42}}}"""
checkSerializationException({
default.decodeFromString<Holder>(input, mode)
}, { message ->
// Always slow path when discriminator is missing, so no position and path
assertContains(message, "Class discriminator was missing and no default serializers were registered in the polymorphic scope of 'Any'")
})
}
@Test
fun testClassDiscriminatorIsNull() = parametrizedTest { mode ->
val input = """{"d":{"a":{"type":null, "value":42}}}"""
checkSerializationException({
default.decodeFromString<Holder>(input, mode)
}, { message ->
// Always slow path when discriminator is missing, so no position and path
assertContains(message, "Class discriminator was missing and no default serializers were registered in the polymorphic scope of 'Any'")
})
}
}
|