Generic custom serializer breaks kotlin-kapt compilation
See original GitHub issueDescribe the bug
When using a generic custom serializer (see example below), adding the kotlin-kapt
plugin with something that uses kapt
(like data binding or Dagger) causes a Kotlin compiler error.
To Reproduce
The following generic custom serializer works as expected in an Android project when kotlin-kapt
is not in use:
data class Wrapped<T>(
val value: T
)
open class WrappedSerializer<T>(private val serializer: KSerializer<T>): KSerializer<Wrapped<T>> {
override val descriptor: SerialDescriptor = serializer.descriptor
override fun deserialize(decoder: Decoder): Wrapped<T> =
Wrapped(serializer.deserialize(decoder))
override fun serialize(encoder: Encoder, obj: Wrapped<T>) =
serializer.serialize(encoder, obj.value)
}
@Serializable
data class A(
@Serializable(with = WrappedSerializer::class)
val value: Wrapped<String>
)
However, adding
apply plugin: 'kotlin-kapt'
and
android {
dataBinding {
enabled = true
}
}
causes building the build to fail with the following compiler exception:
error: incompatible types: Class<WrappedSerializer> cannot be converted to Class<? extends KSerializer<?>> @kotlinx.serialization.Serializable(with = com.alexvanyo.kotlintest.WrappedSerializer.class)
Expected behavior
Applying the kotlin-kapt
plugin shouldn’t cause a compiler error.
As a temporary workaround, specifying the type does allow compilation with kotlin-kapt
, but this isn’t ideal as it requires a separate class for every serialized type:
class StringWrappedSerializer(serializer: KSerializer<String>): WrappedSerializer<String>(serializer)
@Serializable
data class A(
@Serializable(with = StringWrappedSerializer::class)
val value: Wrapped<String>
)
Environment
- Kotlin version: 1.3.61
- Library version: 0.14.0
- Kotlin platform: JVM
- Gradle version: 5.4.1
- IDE version: Android Studio 3.5.3
Issue Analytics
- State:
- Created 4 years ago
- Reactions:11
- Comments:8
Top Results From Across the Web
How to serialize a generic class with kontlinx.serialization?
Probably, a viable workaround can be to move models and serializers to the separate Gradle module, which is not processed by kapt. And...
Read more >Generics in Kotlin Serialization produces compile time error ...
You need to provide a custom serializer for type T. check this guide (BTW, in your code type T shouldn't be Serializable -...
Read more >SerialDescriptor - Kotlin
For generic types, the actual type substitution is omitted from the string ... Class with custom serializer and custom serial descriptor class Data(...
Read more >kotlinx.serialization: (de)serializing JSON's nullable, optional ...
Writing a custom kotlinx.serialization serializer to ... ²: ^ If you are using the kotlin-kapt plugin (used by data binding, Dagger, etc.) ...
Read more >Spring Boot Reference Documentation
A broken “Liveness” state means that the application is in a state that it ... Custom serializers are usually registered with Jackson through...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
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
I have the same issue as @eugenio1590 describes.
I have the following enum with a
JsonNames
attribute with two values:This causes the build to fail:
If i remove the second value of
JsonNames
, the build succeeds:I am getting an issue similar to this. I am using databinding and kotlinx.serialization. I have an attribute with different names, so I included the @JsonNames annotation with more that one name.
But when I tried to build the project, the console printed the following error:
This didn’t happen when I disabled the databinding. To solve this, I changed the implementation of my DocMessage class
or
But none of the solutions solves the original problem completely. @JsonNames cannot be used with more than one value and requires to be used with the @SerialName to work.