Optional properties are not working on Kotlin 1.5.31
See original GitHub issueI’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:
- Created 2 years ago
- Reactions:2
- Comments:7 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As a workaround, you need to remove the
@Serializable(OptionalPropertySerializer::class)
annotation from theOptional
class and add it to each property of your data class (like in blog post).Related YouTrack ticket: https://youtrack.jetbrains.com/issue/KT-50764