Kotlin / kotlinx.serialization

Kotlin multiplatform / multi-format serialization
Apache License 2.0
5.33k stars 618 forks source link

@file:UseContextualSerialization doesn't work on Kotlin 2.0.20-RC2 #2783

Closed vladimirfx closed 2 weeks ago

vladimirfx commented 4 weeks ago

Describe the bug

@file:UseContextualSerialization annotation simply doesn't work starting from Kotlin 2.0.0 up to 2.0.20-RC2. Compilation fails with:

Serializer has not been found for type 'java.util.UUID'. To use context serializer as fallback, explicitly annotate type or property with @Contextual

To Reproduce

@file:UseContextualSerialization(
    forClasses = [
        UUID::class,
    ]
)

package reproducer

import kotlinx.serialization.Serializable
import kotlinx.serialization.UseContextualSerialization
import java.util.UUID

@Serializable
data class ContextualSerializer(val uuid: UUID)

Reproducer project: use-contextual-serializer-bug.zip

Expected behavior

The code above should compile normally.

Environment

sandwwraith commented 3 weeks ago

Thanks for the report. Apparently @file:UseContextualSerialization(UUID::class) or @file:UseContextualSerialization(UUID::class, Date::class) work as expected but @file:UseContextualSerialization(forClasses = [UUID::class]) does not. That is... not what I expected from compiler plugin API.