commonTest.kotlinx.serialization.json.polymorphic.JsonPolymorphismExceptionTest.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlinx-serialization-json
Show all versions of kotlinx-serialization-json
Kotlin multiplatform serialization runtime library
/*
* Copyright 2017-2020 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.*
import kotlinx.serialization.json.*
import kotlinx.serialization.json.internal.*
import kotlinx.serialization.modules.*
import kotlin.test.*
class JsonPolymorphismExceptionTest : JsonTestBase() {
@Serializable
abstract class Base
@Serializable
@SerialName("derived")
class Derived(val nested: Nested = Nested()) : Base()
@Serializable
class Nested
@Test
fun testDecodingException() = parametrizedTest { jsonTestingMode ->
val serialModule = SerializersModule {
polymorphic(Base::class) {
subclass(Derived::class)
}
}
assertFailsWith {
Json { serializersModule = serialModule }.decodeFromString(Base.serializer(), """{"type":"derived","nested":null}""", jsonTestingMode)
}
}
@Test
fun testMissingDiscriminator() = parametrizedTest { jsonTestingMode ->
val serialModule = SerializersModule {
polymorphic(Base::class) {
subclass(Derived::class)
}
}
assertFailsWith {
Json { serializersModule = serialModule }.decodeFromString(Base.serializer(), """{"nested":{}}""", jsonTestingMode)
}
}
}