Kotlinx.serialization: `Json.parse` fails on JS for types defined in `commonTest` which extend from types defined in `commonMain`.

Created on 23 Apr 2019  路  5Comments  路  Source: Kotlin/kotlinx.serialization

I am trying to upgrade my multiplatform project from Kotlin 1.3.21 and serialization 0.10.0 to Kotlin 1.3.30 and serialization 0.11.0.

After doing so, all my JVM tests pass, but many of my JS tests fail. I get errors along the lines of:

TypeError: SomeBaseClass_init is not a function

I have managed to create a small repro:

class AbstractBaseTest
{
    @Test
    fun concreteClass_test()
    {
        val concrete = ConcreteClass()
        val serialized: String = Json.stringify( ConcreteClass.serializer(), concrete )
        val parsed: ConcreteClass = Json.parse( ConcreteClass.serializer(), serialized )
    }

    @Test
    fun stubConcreteClass_test()
    {
        val concrete = StubConcreteClass()
        val serialized: String = Json.stringify( StubConcreteClass.serializer(), concrete )
        val parsed: StubConcreteClass = Json.parse( StubConcreteClass.serializer(), serialized )
    }
}

concreteClass_test() passes, but stubConcreteClass_test() fails on Json.parse:

TypeError: AbstractBase_init is not a function

The difference is ConcreteClass is defined in commonMain, whereas StubConcreteClass is defined in commonTest. The classes are just empty dummy classes.

@Serializable
abstract class AbstractBase

@Serializable
class ConcreteClass : AbstractBase()

@Serializable
class StubConcreteClass : AbstractBase()

Potentially related/relevant, since this seems a dependency issue. I copy JS test dependencies in build.gradle as follows, and this worked for the old version of Kotlin/serialization:

// JS test configuration.
// This is adapted from kotlinx-io's build file; I do not fully understand this configuration:
// https://stackoverflow.com/a/55244288/590790
apply plugin: 'com.moowork.node'
task copyJsDependencies(type: Copy, dependsOn: compileTestKotlinJs) {
    from compileKotlinJs.destinationDir
    into "${buildDir}/node_modules"

    def configuration = configurations.jsTestRuntimeClasspath
    from(files {
        configuration.collect { File file ->
            file.name.endsWith(".jar")
                    ? zipTree(file.absolutePath).matching {
                include '*.js'
                include '*.js.map' }
                    : files()
        }
    }.builtBy(configuration))
}
node {
    version = '11.12.0'
    download = true
}
task installMocha(type: NpmTask) {
    args = ['install', 'mocha']
}
task runMocha(type: NodeTask, dependsOn: [installMocha, compileTestKotlinJs, copyJsDependencies]) {
    script = file('node_modules/mocha/bin/mocha')
    args = [compileTestKotlinJs.outputFile]
}
jsTest.dependsOn runMocha

Full code is available in the repro.

bug js

Most helpful comment

@Whathecode Fix will be available in Kotlin 1.3.41

All 5 comments

Still happens in Kotlin 1.3.31 and serialization 0.11.0.

I've been looking a bit more into what fails exactly.

The JavaScript tests try to import name mangled _init functions from the dependent module.

var AbstractBase_init = $module$bleh.AbstractBase_init_a66qd8$;

Within the dependent module, however, the base class is packaged, but not the _init function.

package$bleh.AbstractBase = AbstractBase;
// The following is NOT defined!
package$bleh.AbstractBase_init = AbstractBase_init;

Furthermore, AbstractBase_init is not mangled in the module definition.

This seems like a compiler error? After modifying the generated JS files to export all _init definitions, and matching them up correctly where they deviate, all tests pass!

Any plans on fixing this?

This is currently keeping me locked on Kotlin 1.3.21, which prevents me from upgrading to Gradle > 5.3 due to the following bug, further complicating the publication of my multiplatform library.

@Whathecode I've reproduced the issue and currently investigating it

@Whathecode Fix will be available in Kotlin 1.3.41

Was this page helpful?
0 / 5 - 0 ratings

Related issues

elizarov picture elizarov  路  3Comments

sandwwraith picture sandwwraith  路  4Comments

SaifurRahmanMohsin picture SaifurRahmanMohsin  路  3Comments

kastork picture kastork  路  3Comments

ersin-ertan picture ersin-ertan  路  3Comments