Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@file:UseContextualSerialization doesn't work with actual typealias on Kotlin 2.0.0 #2704

Closed
rjaros opened this issue Jun 5, 2024 · 5 comments

Comments

@rjaros
Copy link

rjaros commented Jun 5, 2024

When using a @file:UseContextualSerialization annotation with an expected type declared in common target and defined as actual typealias on other targets, the compiler reports an error about missing serializer.

To, Reproduce
common code:

expect class LocalDateTime

common code, different file:

@file:UseContextualSerialization(LocalDateTime::class)

@Serializable
data class Person(
    val id: Int? = 0,
    val firstName: String? = null,
    val lastName: String? = null,
    val createdAt: LocalDateTime? = null,
)

jvm code:

actual typealias LocalDateTime = java.time.LocalDateTime

The compiler fails with an error message:

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

Expected behavior
No error. The same code works fine with Kotlin 1.9.24.
Using @Contextual annotation directly on a property works as well.

Environment

  • Kotlin version: 2.0.0
  • Library version: 1.7.0-RC
  • Kotlin platforms: JS, JVM
@sandwwraith
Copy link
Member

Quick question: does it work with 2.0 when typealias LocalDateTime = java.time.LocalDateTime is not actual, just a regular one?

@rjaros
Copy link
Author

rjaros commented Jun 13, 2024

Apparently it doesn't work with regular typealias as well. This is all you need to have a compile error with 2.0.0:

@file:UseContextualSerialization(MyDate::class)
package com.example

import kotlinx.serialization.Serializable
import kotlinx.serialization.UseContextualSerialization

typealias MyDate = java.time.LocalDate

@Serializable
data class MyClass(val d: MyDate?)

fun main() {
}

The same file compiles fine with 1.9.24.

@sandwwraith
Copy link
Member

Thanks, I'll take a look on it.

@sandwwraith sandwwraith self-assigned this Jun 17, 2024
@sandwwraith
Copy link
Member

By the way, your original reproducer (with @file:UseContextualSerialization(LocalDateTime::class) and expect class LocalDateTime in common code) works fine on 2.0.0. So you probably had UseContextualSerialization in the platform sources.

sandwwraith added a commit to JetBrains/kotlin that referenced this issue Jun 20, 2024
…annotation

because they are expanded for properties types

Fixes Kotlin/kotlinx.serialization#2704
@rjaros
Copy link
Author

rjaros commented Jun 20, 2024

You are right. I've simplified my example too much without checking. In my project, the expect class and typealias are defined in a multiplatform library. But the rest of the code is the same and the UseContextualSerialization is used in the common sources. When using a library, it doesn't work on 2.0.0.

sandwwraith added a commit to JetBrains/kotlin that referenced this issue Jun 21, 2024
…annotation

because they are expanded for properties types

Fixes Kotlin/kotlinx.serialization#2704
sandwwraith added a commit to JetBrains/kotlin that referenced this issue Jun 24, 2024
…annotation

because they are expanded for properties types

Fixes Kotlin/kotlinx.serialization#2704
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants