question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional properties are not working on Kotlin 1.5.31

See original GitHub issue

I’m dealing with demanding API, that has 3 states: value, null value, no value. It’s a case described in blog post here: https://medium.com/livefront/kotlinx-serialization-de-serializing-jsons-nullable-optional-properties-442c7f0c2614

To Reproduce I have serialiser as in blog post:

open class OptionalPropertySerializer<T>(
    private val valueSerializer: KSerializer<T>
) : KSerializer<OptionalProperty<T>> {
    final override val descriptor: SerialDescriptor = valueSerializer.descriptor

    final override fun deserialize(decoder: Decoder): OptionalProperty<T> =
        OptionalProperty.Present(valueSerializer.deserialize(decoder))

    final override fun serialize(encoder: Encoder, value: OptionalProperty<T>) {
        when (value) {
            OptionalProperty.NotPresent -> throw SerializationException(
                "Tried to serialize an optional property that had no value present." +
                        " Is encodeDefaults false?"
            )
            is OptionalProperty.Present ->
                valueSerializer.serialize(encoder, value.value)
        }
    }
}

on Kotlin 1.5.30 this valueSerializer was injected correctly. However on 1.5.31 I’m always getting there instance of: kotlinx.serialization.internal.UnitSerializer

Expected behavior It should work as in Kotlin 1.5.30. I’m not sure what created that regression (https://github.com/JetBrains/kotlin/releases/tag/v1.5.31) or is it serialisation issue or Kotlin’s.

Environment

  • Kotlin version: 1.5.31
  • Library version: 1.3.0, also tested with the same result on 1.2.2
  • Kotlin platforms: JVM, native
  • Gradle version: 7.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
shanshincommented, Sep 27, 2021

As a workaround, you need to remove the @Serializable(OptionalPropertySerializer::class) annotation from the Optional class and add it to each property of your data class (like in blog post).

0reactions
C2H6Ocommented, Jan 12, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Room kapt error when upgrading kotlin or gradle
In my case, I have faced the same issue while I have upgraded "kotlin-gradle-plugin: from 1.5.31 to 1.6.10".
Read more >
Kotlin Language Documentation 1.7.21
Functional operators example: Long Number problem ... Types in Kotlin are optional in many places due to pervasive use of type-inference, ...
Read more >
Compose UI - Android Developers
In UI tests using a Compose rule, continuations resumed during withFrameNanos callbacks will not be dispatched until after all frame callbacks have finished ......
Read more >
Kotlin Gradle Plugin 1.4.20 undeclared system property reads ...
5 problems were found storing the configuration cache, 3 of which seem unique. - Class `org.jetbrains.kotlin.daemon.client.
Read more >
Building Kotlin Libraries Sample - Gradle User Manual
This guide demonstrates how to create a Kotlin library with Gradle using gradle init . ... If not, the libraries will be downloaded...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found