question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
sandwwraithcommented, Jul 1, 2019

@Whathecode Fix will be available in Kotlin 1.3.41

1reaction
sandwwraithcommented, Jun 28, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript type-safety fails with JSON.parse - Stack Overflow
Am I wrong or does type-safety get thrown out in TypeScript when parsing JSON? I should be getting an error here, but I...
Read more >
more general type of `JSON.parse()` · Issue #48907 · microsoft ...
My compilation target is ES2015 and my lib is the default . Missing / Incorrect Definition. JSON.parse 。 Sample Code. In Node.js: import...
Read more >
A quick introduction to “Type Declaration” files and adding ...
json , we have specified the value for the typeRoots compiler-option and it points to the types/ directory relative to this file. I...
Read more >
TypeScript errors and how to fix them
A list of common TypeScript errors and how to fix them.
Read more >
Multiplatform Gradle DSL reference - Kotlin
Designates the Kotlin platform of this target. Available values: jvm , androidJvm , js , native , common . artifactsTaskName.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found