Thread
#serialization
    Paulius Ruminas

    Paulius Ruminas

    3 years ago
    Hi, I have encountered an error because of discriminator field name clash:
    sealed class A {
    
        @Serializable
        data class B(val type: String) : A()
    }
    
    @Test
    fun main() {
        val json = Json(
            configuration = JsonConfiguration.Common,
            context = SerializersModule {
                polymorphic<A> {
                    addSubclass<A.B>()
                }
            }
        )
    
        val s = PolymorphicSerializer(A::class)
        val j = json.stringify(s, A.B(type = "Test"))
    
        json.parse(s, j)
    }
    The code will produce error:
    Test is not registered for polymorphic serialization in the scope of class A
    The error occurs because there are two
    type
    fields. If it is possible it would be nice that this name clash issue would be reported at compile time or if it is not possible I think there should be a better error message that states that one has two
    type
    fields and it is impossible to resolve the polymorphic type. What do you think?
    s

    sandwwraith

    3 years ago