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

A custom serializer class requires serializers for non-serializable fields with version of kotlin 1.8.20. #2260

Closed
mercuriy94 opened this issue Apr 3, 2023 · 3 comments

Comments

@mercuriy94
Copy link

mercuriy94 commented Apr 3, 2023

Describe the bug
I have a base class (BaseApiResponse) for http responses and custom serializer (BaseApiResponseSerializer) for deserialize responses into this base api class. This class has a few fields and one field of them is not serializable (Throwable).

class BaseApiResponse<T>(
    var result: T? = null,
    var isSuccess: Boolean = false,
    var errorCode: String? = null,
    var errorMessage: String? = null,
    var errorId: String? = null,
    var requestId: String? = null,
) {
    // this field is not serializable
    var cause: Throwable? = null
}
Json {
       serializersModule = SerializersModule {
             contextual(BaseApiResponse::class) { args -> BaseApiResponseSerializer(args[0]) }
        }
}

I have updated kotlin version to 1.8.20 and my project is no longer compiling with an error.

Message of error
Details: kotlinx.serialization compiler plugin internal error: unable to transform declaration, see cause
  at org.jetbrains.kotlinx.serialization.compiler.extensions.SerializerClassLowering.lower(SerializationLoweringExtension.kt:219)
  at org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationLoweringExtensionKt$runOnFileInOrder$1.visitClass(SerializationLoweringExtension.kt:51)
  at org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid$DefaultImpls.visitClass(IrElementVisitorVoid.kt:111)
  at org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationLoweringExtensionKt$runOnFileInOrder$1.visitClass(SerializationLoweringExtension.kt:45)
  at org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationLoweringExtensionKt$runOnFileInOrder$1.visitClass(SerializationLoweringExtension.kt:45)
  at org.jetbrains.kotlin.ir.declarations.IrClass.accept(IrClass.kt:64)
  at org.jetbrains.kotlin.ir.declarations.IrFile.acceptChildren(IrFile.kt:36)
  at org.jetbrains.kotlin.ir.visitors.IrVisitorsKt.acceptChildrenVoid(IrVisitors.kt:15)
  at org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationLoweringExtensionKt$runOnFileInOrder$1.visitElement(SerializationLoweringExtension.kt:47)
  at org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid$DefaultImpls.visitPackageFragment(IrElementVisitorVoid.kt:190)
  at org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationLoweringExtensionKt$runOnFileInOrder$1.visitPackageFragment(SerializationLoweringExtension.kt:45)
  at org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid$DefaultImpls.visitFile(IrElementVisitorVoid.kt:200)
  at org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationLoweringExtensionKt$runOnFileInOrder$1.visitFile(SerializationLoweringExtension.kt:45)
  at org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid$DefaultImpls.visitFile(IrElementVisitorVoid.kt:198)
  at org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationLoweringExtensionKt$runOnFileInOrder$1.visitFile(SerializationLoweringExtension.kt:45)
  at org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationLoweringExtensionKt$runOnFileInOrder$1.visitFile(SerializationLoweringExtension.kt:45)
  at org.jetbrains.kotlin.ir.declarations.IrFile.accept(IrFile.kt:30)
  at org.jetbrains.kotlin.ir.visitors.IrVisitorsKt.acceptVoid(IrVisitors.kt:11)
  at org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationLoweringExtensionKt.runOnFileInOrder(SerializationLoweringExtension.kt:45)
  at org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationLoweringExtension.generate(SerializationLoweringExtension.kt:182)
  at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.convertToIr$lambda$0(JvmIrCodegenFactory.kt:190)
  at org.jetbrains.kotlin.psi2ir.Psi2IrTranslator.generateModuleFragment(Psi2IrTranslator.kt:107)
  at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.convertToIr(JvmIrCodegenFactory.kt:224)
  at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.convertToIr(JvmIrCodegenFactory.kt:57)
  at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.convertToIr(KotlinToJVMBytecodeCompiler.kt:225)
  at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:102)
  at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:47)
  at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:168)
  at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:53)
  at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:100)
  at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:46)
  at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:101)
  at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:495)
  at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:133)
  at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.doCompile(IncrementalCompilerRunner.kt:486)
  at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileImpl(IncrementalCompilerRunner.kt:409)
  at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileNonIncrementally(IncrementalCompilerRunner.kt:290)
  at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compile(IncrementalCompilerRunner.kt:112)
  at org.jetbrains.kotlin.daemon.CompileServiceImplBase.execIncrementalCompiler(CompileServiceImpl.kt:627)
  at org.jetbrains.kotlin.daemon.CompileServiceImplBase.access$execIncrementalCompiler(CompileServiceImpl.kt:101)
  at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1587)
  at jdk.internal.reflect.GeneratedMethodAccessor329.invoke(Unknown Source)
  at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.base/java.lang.reflect.Method.invoke(Method.java:566)
  at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
  at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
  at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
  at java.base/java.security.AccessController.doPrivileged(Native Method)
  at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
  at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
  at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
  at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
  at java.base/java.security.AccessController.doPrivileged(Native Method)
  at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
  at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
  at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
  at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalStateException: Serializer for element of type kotlin.Throwable? has not been found
  at org.jetbrains.kotlinx.serialization.compiler.backend.ir.SerializerSearchUtilKt.findTypeSerializerOrContext(SerializerSearchUtil.kt:84)
  at org.jetbrains.kotlinx.serialization.compiler.backend.ir.SerializerSearchUtilKt.getIrSerialTypeInfo(SerializerSearchUtil.kt:63)
  at org.jetbrains.kotlinx.serialization.compiler.backend.ir.BaseIrGenerator.cacheableChildSerializerInstance(BaseIrGenerator.kt:418)
  at org.jetbrains.kotlinx.serialization.compiler.backend.ir.BaseIrGenerator.createCachedChildSerializers(BaseIrGenerator.kt:413)
  at org.jetbrains.kotlinx.serialization.compiler.backend.ir.SerializerIrGenerator.<init>(SerializerIrGenerator.kt:92)
  at org.jetbrains.kotlinx.serialization.compiler.backend.ir.SerializerIrGenerator$Companion.generate(SerializerIrGenerator.kt:639)
  at org.jetbrains.kotlinx.serialization.compiler.extensions.SerializerClassLowering.lower(SerializationLoweringExtension.kt:135)
  ... 56 more


As you can see, the main cause is that the serializer is for an element of type kotlin.Throwable? was not found.

But this code compiles successfully with version of kotlin 1.8.10

Expected behavior
I think as long as I provide my custom serializer, I shouldn't have such compilation issues.

Environment

  • Kotlin version: 1.8.20
  • Library version: 1.5.0
  • Kotlin platforms: Android
  • Gradle version: 7.6.1
  • IDE version: Android Studio Electric Eel | 2022.1.1 Patch 2
  • Other relevant context:
    os - macOS Ventura (version 13.2.1),
    jre - java 11.0.13
@sandwwraith
Copy link
Member

@sandwwraith
Copy link
Member

If you use @kotlinx.serialization.Serializer annotation, simply remove it

sandwwraith added a commit to JetBrains/kotlin that referenced this issue Apr 6, 2023
Creation of this property in the SerializerIrGenerator.<init> can lead to
a 'Serializer not found' internal error when generator is applied to a fully-customized external serializer.
In that case, generator is still created, but none of the generateSave/Load functions are called,
so cacheableChildSerializers(Property) is not necessary.

#KT-57730 Fixed
Fixes Kotlin/kotlinx.serialization#2260
sandwwraith added a commit to JetBrains/kotlin that referenced this issue Apr 17, 2023
Creation of this property in the SerializerIrGenerator.<init> can lead to
a 'Serializer not found' internal error when generator is applied to a fully-customized external serializer.
In that case, generator is still created, but none of the generateSave/Load functions are called,
so cacheableChildSerializers(Property) is not necessary.

#KT-57730 Fixed
Fixes Kotlin/kotlinx.serialization#2260
KotlinBuild pushed a commit to JetBrains/kotlin that referenced this issue Apr 18, 2023
Creation of this property in the SerializerIrGenerator.<init> can lead to
a 'Serializer not found' internal error when generator is applied to a fully-customized external serializer.
In that case, generator is still created, but none of the generateSave/Load functions are called,
so cacheableChildSerializers(Property) is not necessary.

#KT-57730 Fixed
Fixes Kotlin/kotlinx.serialization#2260
sandwwraith added a commit to JetBrains/kotlin that referenced this issue Apr 18, 2023
Creation of this property in the SerializerIrGenerator.<init> can lead to
a 'Serializer not found' internal error when generator is applied to a fully-customized external serializer.
In that case, generator is still created, but none of the generateSave/Load functions are called,
so cacheableChildSerializers(Property) is not necessary.

#KT-57730 Fixed
Fixes Kotlin/kotlinx.serialization#2260

(cherry picked from commit d9e16fb)
KotlinBuild pushed a commit to JetBrains/kotlin that referenced this issue Apr 19, 2023
Creation of this property in the SerializerIrGenerator.<init> can lead to
a 'Serializer not found' internal error when generator is applied to a fully-customized external serializer.
In that case, generator is still created, but none of the generateSave/Load functions are called,
so cacheableChildSerializers(Property) is not necessary.

#KT-57730 Fixed
Fixes Kotlin/kotlinx.serialization#2260

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

Fixed in Kotlin 1.8.20

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

3 participants