All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonTest.kotlinx.serialization.json.polymorphic.JsonPolymorphismExceptionTest.kt Maven / Gradle / Ivy

There is a newer version: 1.7.3
Show newest version
/*
 * 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)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy