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

jvm/1.8.20 returns KSerializer<List<String>> instead of KSerializer<List<String?>> #2265

Closed
simonegiacomelli opened this issue Apr 8, 2023 · 1 comment

Comments

@simonegiacomelli
Copy link

Describe the bug
The serializer returned should be KSerializer<List> but the expected type is KSerializer<List<String?>>.
In other words, it doesn't honor the nullability of the first generic type of the list.
It happens only on jvm/1.8.20.
Workaround attached.

To Reproduce
This repository reproduces the issue.
Btw all the code needed is this:

import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.elementDescriptors
import kotlinx.serialization.serializer
import kotlin.reflect.typeOf
import kotlin.test.Test
import kotlin.test.assertTrue

class IssueTest {

    /** This fails with jvm/1.8.20 */
    @Test
    fun test_issue() {
        assertTrue(listOfNullable<String>().descriptor.elementDescriptors.first().isNullable)
    }

    /** This works with all the combinations mentioned, i.e., js/jvm/1.8.20 and js/jvm/1.8.10 */
    @Test
    fun test_workaround() {
        assertTrue(workaround<String>().descriptor.elementDescriptors.first().isNullable)
    }
}

inline fun <reified T> listOfNullable(): KSerializer<List<Any?>> = serializer<List<T?>>() as KSerializer<List<Any?>>
inline fun <reified T> workaround(): KSerializer<List<Any?>> = serializer(typeOf<List<T?>>()) as KSerializer<List<Any?>>

Expected behavior
It should return a KSerializer<List<String?>>

Environment

  • Kotlin version: 1.8.20
  • Library version: org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0
  • Kotlin platforms: 1.8.20/jvm affected
  • Gradle version: 7.4.2
  • jvm: 1.8.0_312 (Amazon.com Inc. 25.312-b07)
@sandwwraith sandwwraith self-assigned this Apr 11, 2023
sandwwraith added a commit to JetBrains/kotlin that referenced this issue Apr 19, 2023
…ntrinsic.

Nullability info should be added to TYPE_OF operation marker.

Fixes Kotlin/kotlinx.serialization#2265
KotlinBuild pushed a commit to JetBrains/kotlin that referenced this issue Apr 19, 2023
…ntrinsic.

Nullability info should be added to TYPE_OF operation marker.

Fixes Kotlin/kotlinx.serialization#2265
sandwwraith added a commit to JetBrains/kotlin that referenced this issue Apr 19, 2023
…ntrinsic.

Nullability info should be added to TYPE_OF operation marker.

Fixes Kotlin/kotlinx.serialization#2265

(cherry picked from commit ef9074e)
KotlinBuild pushed a commit to JetBrains/kotlin that referenced this issue Apr 19, 2023
…ntrinsic.

Nullability info should be added to TYPE_OF operation marker.

Fixes Kotlin/kotlinx.serialization#2265

(cherry picked from commit ef9074e)
@sandwwraith
Copy link
Member

Fixed in Kotlin 1.8.21

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