-
Notifications
You must be signed in to change notification settings - Fork 626
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix incorrect behavior while deserializing maps to sealed classes (#2052
) Fixes #2035
- Loading branch information
1 parent
dc9983a
commit 0f35682
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
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
50 changes: 50 additions & 0 deletions
50
...onTest/src/kotlinx/serialization/properties/SealedClassSerializationFromPropertiesTest.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,50 @@ | ||
package kotlinx.serialization.properties | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertIs | ||
|
||
class SealedClassSerializationFromPropertiesTest { | ||
@Serializable | ||
sealed class BaseClass { | ||
abstract val firstProperty: Long | ||
abstract val secondProperty: String | ||
} | ||
|
||
@SerialName("FIRSTCHILD") | ||
@Serializable | ||
data class FirstChild(override val firstProperty: Long, override val secondProperty: String) : BaseClass() | ||
|
||
@SerialName("SECONDCHILD") | ||
@Serializable | ||
data class SecondChild(override val firstProperty: Long, override val secondProperty: String) : BaseClass() | ||
|
||
@Test | ||
fun testPropertiesDeserialization() { | ||
val props = mapOf( | ||
"type" to "FIRSTCHILD", | ||
"firstProperty" to 1L, | ||
"secondProperty" to "one" | ||
) | ||
|
||
val instance: BaseClass = Properties.decodeFromMap(props) | ||
|
||
assertIs<FirstChild>(instance) | ||
assertEquals(instance.firstProperty, 1) | ||
assertEquals(instance.secondProperty, "one") | ||
} | ||
|
||
@Test | ||
fun testPropertiesSerialization() { | ||
val instance: BaseClass = FirstChild( | ||
firstProperty = 1L, secondProperty = "one" | ||
) | ||
|
||
val instanceProperties = Properties.encodeToMap(instance) | ||
|
||
assertEquals(1L, instanceProperties["firstProperty"]) | ||
assertEquals("one", instanceProperties["secondProperty"]) | ||
} | ||
} |