I just spent a bit of time updating our build to s...
# k2-adopters
j
I just spent a bit of time updating our build to support k2. I got it working, so that's great. But I had to work around two issues: 1. We have a couple of @Deprecated tags with ReplaceWith patterns that take a string literal. For readability, we were using string concatenation so, ReplaceWith("foo("+ "bar=1"), the compiler doesn't like that. Replacing those with raw strings fixed it. Main issue is that the expressions are longish so breaking them up over several lines was the intention here. Ugly but it used to work. 2. One of our unit tests that uses a kotlinx serializer on a sealed class was complaining it couldn't find the serializer in our kotlin-js build (it's a mulitplatform library). Jvm build works fine. A neighbouring unit test using the same serializer as part of a ListSerializer also works fine in js; so the serializer is there for sure. Including the stacktrace for that. Commenting the test out fixes things. But looks like a weird one. Switching the first test to a ListSerializer doesn't fix the problem. There's something with the WebLink nested class that it just doesn't like?! I hope this helps.
Copy code
@Serializable
sealed class Content {
    // removed a bunch of additional fields and classes    
    @Serializable
    @SerialName("Image")
    data class Image(
        override val id: String,
        val href: String,
        val width: Int,
        val height: Int,
        val key: String,
    ) : Content()

    @Serializable
    @SerialName("WebLink")
    data class WebLink(
        override val id: String,
        val href: String,
        val mimeType: String = "text/html",
    ) : Content()
}

    @Test
    // breaks
    fun shouldSerializeAndDeserializeWeblink() {
        val webLink = Content.WebLink("1", "ddd", "mmm")
        val serializer = Content.serializer()
        val encoded = DEFAULT_PRETTY_JSON.encodeToString(serializer, webLink)
        val decoded = DEFAULT_PRETTY_JSON.decodeFromString<Content>(encoded)
        decoded shouldBe webLink
    }

    @Test
    // works fine
    fun shouldSerializeAndDeserializeImage() {
        val image = Content.Image("1", "ddd", 200, 200, "xyz")
        val serializer = ListSerializer(Content.serializer())
        val encoded = DEFAULT_PRETTY_JSON.encodeToString(serializer, listOf(image))
        println(encoded)
        val decoded = DEFAULT_PRETTY_JSON.decodeFromString(serializer, encoded)
        println(encoded)
        decoded shouldBe listOf(image)
    }
Copy code
SerializationException: Serializer for class 'Content' is not found.
Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.

On Kotlin/JS explicitly declared serializer should be used for interfaces and enums without @Serializable annotation
	at <global>.platformSpecificSerializerNotRegistered(/opt/buildAgent/work/b2fef8360e1bcf3d/core/jsMain/src/kotlinx/serialization/internal/Platform.kt:42)
	at <global>.serializer(/opt/buildAgent/work/b2fef8360e1bcf3d/core/commonMain/src/kotlinx/serialization/Serializers.kt:134)
	at AttachmentTest.protoOf.shouldSerializeAndDeserializeWeblink_98yafc(/opt/buildAgent/work/b2fef8360e1bcf3d/core/commonMain/src/kotlinx/serialization/internal/Platform.common.kt:80)
	at <global>.fn(kotlin/formation-graphqlapi-api-client-test.js:1126)
	at Context.<anonymous>(/Users/jillesvangurp/git/formation/graphqlapi/build/js/packages_imported/kotlin-test-js-runner/src/KotlinTestTeamCityConsoleAdapter.ts:70)
	at <global>.processImmediate(node:internal/timers:471)
👍 1
the exception for the replace with is
Copy code
@Deprecated(
        "refactor with new calls",
        ReplaceWith(
            "restPost(" +
                    "  body = jsonBody(serializationStrategy, body)," +
                    "  params = params," +
                    "  pathElements = pathElements," +
                    ")"
        )
    )
Copy code
org.jetbrains.kotlin.util.SourceCodeAnalysisException: java.lang.IllegalStateException: Cannot convert expression String(restPost().R|kotlin/String.plus|(String(  body = jsonBody(serializationStrategy, body),)).R|kotlin/String.plus|(String(  params = params,)).R|kotlin/String.plus|(String(  pathElements = pathElements,)).R|kotlin/String.plus|(String())) to constant
        at org.jetbrains.kotlin.util.AnalysisExceptionsKt.wrapIntoSourceCodeAnalysisExceptionIfNeeded(AnalysisExceptions.kt:27)
        at org.jetbrains.kotlin.fir.FirCliExceptionHandler.handleExceptionOnElementAnalysis(Utils.kt:169)
        at org.jetbrains.kotlin.fir.serialization.FirElementSerializer.functionProto(FirElementSerializer.kt:1351)
        at org.jetbrains.kotlin.fir.serialization.FirElementSerializer.classProto(FirElementSerializer.kt:182)
        at org.jetbrains.kotlin.fir.serialization.FirKlibSerializationKt.serializeSingleFirFile$makeClassesProtoWithNested(firKlibSerialization.kt:54)
        at org.jetbrains.kotlin.fir.serialization.FirKlibSerializationKt.serializeSingleFirFile(firKlibSerialization.kt:60)
        at org.jetbrains.kotlin.cli.metadata.FirMetadataSerializer.serialize(FirMetadataSerializer.kt:164)
        at org.jetbrains.kotlin.cli.metadata.FirMetadataSerializer.serialize(FirMetadataSerializer.kt:50)
        at org.jetbrains.kotlin.cli.metadata.AbstractMetadataSerializer.analyzeAndSerialize(AbstractMetadataSerializer.kt:38)
        at org.jetbrains.kotlin.cli.metadata.K2MetadataCompiler.doExecute(K2MetadataCompiler.kt:127)
        at org.jetbrains.kotlin.cli.metadata.K2MetadataCompiler.doExecute(K2MetadataCompiler.kt:41)
        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.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1497)
        at jdk.internal.reflect.GeneratedMethodAccessor36.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:360)
        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(AccessController.java:712)
        at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:587)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:705)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:704)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base/java.lang.Thread.run(Thread.java:833)
d
For the first issue there is KT-57618 The second one looks new, could you please report a ticket? cc @sandwwraith
c
When moving Java code over to Kotlin i change the
"bla" +\n"bleh"
to
"""\n  bla\n   bleh\n""".trimIndentation()
j
Done, KT-61562. Sorry this took me a few days to get around to.
d
Thank you!