summaryrefslogtreecommitdiff
path: root/formats/json-tests
diff options
context:
space:
mode:
authorUgljesa Jovanovic <ugljesa.jovanovic@ionspin.com>2023-06-29 13:44:16 +0200
committerGitHub <noreply@github.com>2023-06-29 13:44:16 +0200
commit5bba1083ab8e9ef96a2632e2d0a97cbec8bac637 (patch)
treefb71851d771c2083d92a4b538e88d6afb20a6646 /formats/json-tests
parentfd75d353efd2e4e26f24c1e41e763541daa62cb9 (diff)
downloadkotlinx.serialization-5bba1083ab8e9ef96a2632e2d0a97cbec8bac637.tar.gz
Fix beginStructure in JsonTreeDecoder when inner structure descriptor is same as outer (#2346)
Instead of returning the same instance of decoder (with invalid position) a new instance is created, and polyDiscriminator and polyDescriptor are passed to the new instance. This way check for unknown keys in endStructure can properly filter out polymorphic discriminator (by default "type) from potential unknown keys. Fixes #2343
Diffstat (limited to 'formats/json-tests')
-rw-r--r--formats/json-tests/commonTest/src/kotlinx/serialization/json/polymorphic/JsonTreeDecoderPolymorphicTest.kt43
1 files changed, 43 insertions, 0 deletions
diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/polymorphic/JsonTreeDecoderPolymorphicTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/polymorphic/JsonTreeDecoderPolymorphicTest.kt
new file mode 100644
index 00000000..9d8e861d
--- /dev/null
+++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/polymorphic/JsonTreeDecoderPolymorphicTest.kt
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
+ */
+
+package kotlinx.serialization.json.polymorphic
+
+import kotlinx.serialization.Serializable
+import kotlinx.serialization.json.*
+import kotlin.test.*
+
+class JsonTreeDecoderPolymorphicTest : JsonTestBase() {
+
+ @Serializable
+ sealed class Sealed
+
+ @Serializable
+ data class ClassContainingItself(
+ val a: String,
+ val b: String,
+ val c: ClassContainingItself? = null,
+ val d: String?
+ ) : Sealed()
+
+ val inner = ClassContainingItself(
+ "InnerA",
+ "InnerB",
+ null,
+ "InnerC"
+ )
+ val outer = ClassContainingItself(
+ "OuterA",
+ "OuterB",
+ inner,
+ "OuterC"
+ )
+
+ @Test
+ fun testDecodingWhenClassContainsItself() = parametrizedTest { jsonTestingMode ->
+ val encoded = default.encodeToString(outer as Sealed, jsonTestingMode)
+ val decoded: Sealed = Json.decodeFromString(encoded, jsonTestingMode)
+ assertEquals(outer, decoded)
+ }
+}