`Json.parse` fails on JS for types defined in `commonTest` which extend from types defined in `commonMain`.
See original GitHub issueI 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:
- Created 4 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
@Whathecode Fix will be available in Kotlin 1.3.41
@Whathecode I’ve reproduced the issue and currently investigating it