Kotlinx.serialization: Equals of SerialDescriptor is not symmetric (in some cases)

Created on 21 Jan 2020  路  2Comments  路  Source: Kotlin/kotlinx.serialization

Describe the bug
In some cases, equals of SerialDesciptor can't be reversed.

To Reproduce
If you run the code, the second test fails. If you change the type of a in both classes to C and run the code, the both tests pass.

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlin.test.Test
import kotlin.test.assertNotEquals


@Serializable
data class ClassWithEnum1(val a: A?)

@Serializable
@SerialName("ClassWithEnum1")
data class ClassWithEnum2(val a: A)

@Serializable
enum class A {

  B,
}

@Serializable
enum class C {

  D,
}

class TestEquals {

  @Test
  fun test() {
    assertNotEquals(ClassWithEnum1.serializer().descriptor, ClassWithEnum2.serializer().descriptor)
  }

  @Test
  fun testReversed() {
    assertNotEquals(ClassWithEnum2.serializer().descriptor, ClassWithEnum1.serializer().descriptor)
  }
}

Expected behavior
I think the test shouldn't crash.

Environment

  • Kotlin version: 1.3.61
  • Library version: 0.14.0
  • Kotlin platforms: tested on JVM
  • Gradle version: 5.2.1
  • Other relevant context: JRE is JBR11, OS is Linux
bug compiler-plugin

Most helpful comment

It seems like a funny compiler plugin bug: it assumes that C means Char (since C, indeed, is code name for char primitive in JVM bytecode) and inserts char serializer

Workaround: don't use name C (rename your enum value with @SerialName into something else)

All 2 comments

If we compare generated bytecode of C and A, we receive different serializers...

image

It seems like a funny compiler plugin bug: it assumes that C means Char (since C, indeed, is code name for char primitive in JVM bytecode) and inserts char serializer

Workaround: don't use name C (rename your enum value with @SerialName into something else)

Was this page helpful?
0 / 5 - 0 ratings