kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Descriptor of external serializer is incomplete

Open TheMrMilchmann opened this issue 7 months ago • 0 comments

Describe the bug An external serializer's descriptor seems to lack element descriptors despite having elements. This leads to unexpected errors and currently prevents me from implementing some complex custom serializers, including a shape-agnostic solution for #292.

To Reproduce Consider the following simple test program:

@Serializer(forClass = Foo::class)
object GeneratedFooSerializer : KSerializer<Foo>

@Serializable
data class Foo(val bar: String)

fun main() {
    println(GeneratedFooSerializer.descriptor)
}

This will throw an AIOOB:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
	at kotlinx.serialization.internal.PluginGeneratedSerialDescriptor.getElementDescriptor(PluginGeneratedSerialDescriptor.kt:135)
	at kotlinx.serialization.internal.PluginGeneratedSerialDescriptor$toString$1.invoke(PluginGeneratedSerialDescriptor.kt:101)
	at kotlinx.serialization.internal.PluginGeneratedSerialDescriptor$toString$1.invoke(PluginGeneratedSerialDescriptor.kt:100)
	at kotlin.text.StringsKt__AppendableKt.appendElement(Appendable.kt:85)
	at kotlin.collections.CollectionsKt___CollectionsKt.joinTo(_Collections.kt:3490)
	at kotlin.collections.CollectionsKt___CollectionsKt.joinToString(_Collections.kt:3507)
	at kotlin.collections.CollectionsKt___CollectionsKt.joinToString$default(_Collections.kt:3506)
	at kotlinx.serialization.internal.PluginGeneratedSerialDescriptor.toString(PluginGeneratedSerialDescriptor.kt:100)
	at java.base/java.lang.String.valueOf(String.java:4461)
	at java.base/java.io.PrintStream.println(PrintStream.java:1187)
	at TestKt.main(Test.kt:33)
	at TestKt.main(Test.kt)

Expected behavior Checking elementsCountconfirms that there is one element. getElementName and getElementAnnotations work fine but getElementDescriptor throws. I expect this to work fine as all required information should be available.

Environment

  • Kotlin version: 1.9.22
  • Library version: 1.6.2
  • Kotlin platforms: any

TheMrMilchmann avatar Jan 22 '24 14:01 TheMrMilchmann