-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
52 additions
and
6 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...s/commonTest/src/kotlinx/serialization/json/polymorphic/JsonTreeDecoderPolymorphicTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters