kotlinx.serialization
kotlinx.serialization copied to clipboard
Compiler Exception when defining a serializer for a data class in another gradle project
I am trying to define an external serializer for a data class (Data) which is defined in another gradle module (subproject) and the result is the following exception.
Caused by: java.lang.IllegalStateException: Class Data have constructor parameters which are not properties and therefore it is not serializable automatically
at org.jetbrains.kotlinx.serialization.compiler.backend.common.SerializerCodegen.checkSerializability(SerializerCodegen.kt:38)
at org.jetbrains.kotlinx.serialization.compiler.backend.common.SerializerCodegen.generateSerializableClassPropertyIfNeeded(SerializerCodegen.kt:106)
at org.jetbrains.kotlinx.serialization.compiler.backend.common.SerializerCodegen.generate(SerializerCodegen.kt:44)
at org.jetbrains.kotlinx.serialization.compiler.backend.jvm.SerializerCodegenImpl$Companion.generateSerializerExtensions(SerializerCodegenImpl.kt:49)
at org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationCodegenExtension.generateClassSyntheticParts(SerializationCodegenExtension.kt:30)
at org.jetbrains.kotlin.codegen.ImplementationBodyCodegen.generateSyntheticPartsAfterBody(ImplementationBodyCodegen.java:438)
at org.jetbrains.kotlin.codegen.MemberCodegen.generate(MemberCodegen.java:132)
at org.jetbrains.kotlin.codegen.MemberCodegen.genClassOrObject(MemberCodegen.java:302)
at org.jetbrains.kotlin.codegen.MemberCodegen.genClassOrObject(MemberCodegen.java:286)
at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generateClassesAndObjectsInFile(PackageCodegenImpl.java:118)
at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generateFile(PackageCodegenImpl.java:137)
at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generate(PackageCodegenImpl.java:68)
... 44 more
It works if the data class is defined in the same gradle module as the serializer.
To Reproduce
Check out https://github.com/mdietrichstein/bug_repro_kotlinx.serialization_gradle_multiproject_ext_serializer and run ./gradlew clean test
Expected behavior The compiler executes successfully
Environment
- Kotlin version: 1.3.31 and 1.3.41
- Library version: 0.11.0 and 0.11.1
- Kotlin platforms: Android
- Gradle version: 5.4.1
- Other relevant context: Ubuntu 18.04 and MacOSX
I get exactly this error even if the external serializer is in the same Gradle module 🤔 Kotlin version: 1.3.50 kotlinx.serialization: 0.13.0
Same here. with Kotlin 1.3.71. Any workarounds?
@sandwwraith Any news on this ?
Here's another project that demonstrate that this issue still exists in version 1.0-M1-1.4.0-rc: https://github.com/SalomonBrys/test-kx-serialization-bug
This feels critical to me as this prevents automatic serializer generation from other project, which is documented here: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/custom_serializers.md#external-serializers-for-library-classes
Important: this is not linked to Gradle projects. In essence, automatic external serializers generation is broken, whether the class to be serialized comes from another gradle project or from any external library.
Right now, we have no other choice than writing manually our own serializers for any class that is not in our sources.
External automatic serializers generation is in many ways experimental feature for now, and due to its complexity, it has many bugs beside this one. I'll take a look at it, but after 1.0 release
Thanks for the update @sandwwraith.
I believe the documentation should reflect the fact that external automatic serializers generation is not working, especially for the 1.0 release.
We made architecture decisions based on the content of the documentation, and lost some time trying to understand what we did wrong before understanding that the library was not working as the documentation described. While this is perfectly normal for a beta version (we knew it was in beta, and we still decided to use it at our own risks), when the lib hits stable, this may hurt ;)
This still happens with 1.4.10 and serialization 1.0.1
I think it is necessary to at least improve the error message. At the moment the message is very misleading.
Still happens with kotlin 1.4.30 and serialization 1.1.0.
Still Kotlin 1.6.20 and serialization 1.3.2.
(do you see the pattern here?:)
Another one reproduce
https://github.com/y9Kap/types-serialization-reproduce/blob/ec56c40f59ec44bfaf8d0c679d3c8d725358ab60/src/main/kotlin/Main.kt#L13