summaryrefslogtreecommitdiff
path: root/formats/json-tests
diff options
context:
space:
mode:
authorLeonid Startsev <sandwwraith@users.noreply.github.com>2023-07-14 14:59:42 +0200
committerGitHub <noreply@github.com>2023-07-14 14:59:42 +0200
commit3191884b106a9737df680b24b7fc0673d8b154f0 (patch)
tree60ce025e03a784f61a3b615efd98df6bfaf12d47 /formats/json-tests
parentb8de86f0e351f1099d2afb03ff92e2ef6256cbc7 (diff)
downloadkotlinx.serialization-3191884b106a9737df680b24b7fc0673d8b154f0.tar.gz
Fix error triggered by 'consume leading class discriminator' polymorphic parsing optimization: (#2362)
In case of an empty object, no exception should be thrown, it should be treated as missing class discriminator. peekString() correctly handles this case. Also fixes misplaced calls in lenient vs non-lenient mode. Fixes #2353
Diffstat (limited to 'formats/json-tests')
-rw-r--r--formats/json-tests/commonTest/src/kotlinx/serialization/features/PolymorphismTest.kt38
1 files changed, 38 insertions, 0 deletions
diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/features/PolymorphismTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/features/PolymorphismTest.kt
index d05403b8..c4938871 100644
--- a/formats/json-tests/commonTest/src/kotlinx/serialization/features/PolymorphismTest.kt
+++ b/formats/json-tests/commonTest/src/kotlinx/serialization/features/PolymorphismTest.kt
@@ -143,4 +143,42 @@ class PolymorphismTest : JsonTestBase() {
val s = json.encodeToString(Wrapper.serializer(), obj, jsonTestingMode)
assertEquals("""{"polyBase1":{"type":"even","parity":"even"},"polyBase2":{"type":"odd","parity":"odd"}}""", s)
}
+
+ @Serializable
+ sealed class Conf {
+ @Serializable
+ @SerialName("empty")
+ object Empty : Conf() // default
+
+ @Serializable
+ @SerialName("simple")
+ data class Simple(val value: String) : Conf()
+ }
+
+ private val jsonForConf = Json {
+ isLenient = false
+ ignoreUnknownKeys = true
+ serializersModule = SerializersModule {
+ polymorphicDefaultDeserializer(Conf::class) { Conf.Empty.serializer() }
+ }
+ }
+
+ @Test
+ fun defaultSerializerWithEmptyBodyTest() = parametrizedTest { mode ->
+ assertEquals(Conf.Simple("123"), jsonForConf.decodeFromString<Conf>("""{"type": "simple", "value": "123"}""", mode))
+ assertEquals(Conf.Empty, jsonForConf.decodeFromString<Conf>("""{"type": "default"}""", mode))
+ assertEquals(Conf.Empty, jsonForConf.decodeFromString<Conf>("""{"unknown": "Meow"}""", mode))
+ assertEquals(Conf.Empty, jsonForConf.decodeFromString<Conf>("""{}""", mode))
+ }
+
+ @Test
+ fun testTypeKeysInLenientMode() = parametrizedTest { mode ->
+ val json = Json(jsonForConf) { isLenient = true }
+
+ assertEquals(Conf.Simple("123"), json.decodeFromString<Conf>("""{type: simple, value: 123}""", mode))
+ assertEquals(Conf.Empty, json.decodeFromString<Conf>("""{type: default}""", mode))
+ assertEquals(Conf.Empty, json.decodeFromString<Conf>("""{unknown: Meow}""", mode))
+ assertEquals(Conf.Empty, json.decodeFromString<Conf>("""{}""", mode))
+
+ }
}