Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect behavior while deserializing maps to sealed classes #2052

Merged
merged 7 commits into from Oct 13, 2022
@@ -0,0 +1,33 @@
package kotlinx.serialization

import kotlinx.serialization.json.Json
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs

class SealedClassSerializationFromJsonTest {
sandwwraith marked this conversation as resolved.
Show resolved Hide resolved
@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 testJsonSerialization() {
val json = """{"type": "FIRSTCHILD", "firstProperty": 1, "secondProperty": "one"}"""

val instance: BaseClass = Json.decodeFromString(json)

assertIs<FirstChild>(instance)
assertEquals(instance.firstProperty, 1)
assertEquals(instance.secondProperty, "one")
}
}
Expand Up @@ -89,6 +89,22 @@ public sealed class Properties(
return structure(descriptor).also { copyTagsTo(it) }
}

final override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T {
val type = map["type"]?.toString()
var actualSerializer: DeserializationStrategy<out Any>? = null

if (type != null && deserializer is AbstractPolymorphicSerializer<*>) {
sandwwraith marked this conversation as resolved.
Show resolved Hide resolved
actualSerializer = deserializer.findPolymorphicSerializerOrNull(this, type)
}

if (actualSerializer == null) {
sandwwraith marked this conversation as resolved.
Show resolved Hide resolved
throw RuntimeException()
sandwwraith marked this conversation as resolved.
Show resolved Hide resolved
}

@Suppress("UNCHECKED_CAST")
return actualSerializer.deserialize(this) as T
}

final override fun decodeTaggedValue(tag: String): Value {
return map.getValue(tag)
}
Expand Down
@@ -0,0 +1,38 @@
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 testPropertiesSerialization() {
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")
}
}