From 610b0bfcf0fccb4ffa59f69d48287bdfbb43f2e5 Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Sun, 18 Feb 2024 13:59:44 -0800 Subject: [PATCH] Implement support for KotlinX Serialization This change adds support for a new code-gen argument, `implementKSerializable`, which results in the annotation `kotlinx.serialization.Serializable` being added to `data` classes during codegen. Relates to discussion apple/pkl#185 - feat(codegen): add support for kotlin `Serializable` annotation - feat(gradle): add `implementKSerializable` argument - test(codegen): add test for KotlinX serialization support - test(codegen): add test for both Java and KotlinX serialization - test(gradle): add test for compiling with KotlinX serialization Signed-off-by: Sam Gammon --- buildSrc/build.gradle.kts | 1 + .../main/kotlin/pklKotlinLibrary.gradle.kts | 1 + gradle/libs.versions.toml | 2 + pkl-cli/gradle.lockfile | 6 + pkl-codegen-java/gradle.lockfile | 6 + pkl-codegen-kotlin/gradle.lockfile | 9 ++ .../pkl-codegen-kotlin.gradle.kts | 4 + .../kotlin/CliKotlinCodeGeneratorOptions.kt | 6 +- .../pkl/codegen/kotlin/KotlinCodeGenerator.kt | 27 +++- .../codegen/kotlin/KotlinCodeGeneratorTest.kt | 152 +++++++++++++++++- pkl-commons-cli/gradle.lockfile | 6 + pkl-commons-test/gradle.lockfile | 6 + pkl-commons/gradle.lockfile | 6 + pkl-config-kotlin/gradle.lockfile | 9 ++ pkl-doc/pkl-doc.gradle.kts | 2 +- .../main/java/org/pkl/gradle/PklPlugin.java | 4 + .../pkl/gradle/spec/KotlinCodeGenSpec.java | 4 + .../pkl/gradle/task/KotlinCodeGenTask.java | 6 +- .../pkl/gradle/KotlinCodeGeneratorsTest.kt | 30 +++- pkl-server/gradle.lockfile | 6 + 20 files changed, 283 insertions(+), 10 deletions(-) diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 73eb36728..7d3305611 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -8,6 +8,7 @@ dependencies { implementation(libs.kotlinPlugin) { exclude(module = "kotlin-android-extensions") } + implementation(libs.kotlinPluginSerialization) implementation(libs.shadowPlugin) } diff --git a/buildSrc/src/main/kotlin/pklKotlinLibrary.gradle.kts b/buildSrc/src/main/kotlin/pklKotlinLibrary.gradle.kts index 72a829cff..0f7405cfe 100644 --- a/buildSrc/src/main/kotlin/pklKotlinLibrary.gradle.kts +++ b/buildSrc/src/main/kotlin/pklKotlinLibrary.gradle.kts @@ -2,6 +2,7 @@ plugins { id("pklJavaLibrary") kotlin("jvm") + kotlin("plugin.serialization") } val buildInfo = project.extensions.getByType() diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index a6c635cc5..2ab00ed72 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -70,10 +70,12 @@ kotlinCompilerEmbeddable = { group = "org.jetbrains.kotlin", name = "kotlin-comp kotlinScriptingCompilerEmbeddable = { group = "org.jetbrains.kotlin", name = "kotlin-scripting-compiler-embeddable", version.ref = "kotlin" } kotlinScriptUtil = { group = "org.jetbrains.kotlin", name = "kotlin-script-util", version.ref = "kotlin" } kotlinPlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" } +kotlinPluginSerialization = { group = "org.jetbrains.kotlin", name = "kotlin-serialization", version.ref = "kotlin" } kotlinPoet = { group = "com.squareup", name = "kotlinpoet", version.ref = "kotlinPoet" } kotlinReflect = { group = "org.jetbrains.kotlin", name = "kotlin-reflect", version.ref = "kotlin" } kotlinStdLib = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib-jdk8", version.ref = "kotlin" } kotlinxHtml = { group = "org.jetbrains.kotlinx", name = "kotlinx-html-jvm", version.ref = "kotlinxHtml" } +kotlinxSerializationCore = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-core", version.ref = "kotlinxSerialization" } kotlinxSerializationJson = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinxSerialization" } log4j12Api = { group = "org.apache.logging.log4j", name = "log4j-1.2-api", version.ref = "log4j" } nuValidator = { group = "nu.validator", name = "validator", version.ref = "nuValidator" } diff --git a/pkl-cli/gradle.lockfile b/pkl-cli/gradle.lockfile index 328dbbcc6..6bd4f175b 100644 --- a/pkl-cli/gradle.lockfile +++ b/pkl-cli/gradle.lockfile @@ -35,7 +35,10 @@ org.jetbrains.kotlin:kotlin-compiler-embeddable:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-compiler-embeddable:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath org.jetbrains.kotlin:kotlin-daemon-embeddable:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-daemon-embeddable:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath +org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-klib-commonizer-embeddable:1.7.10=kotlinKlibCommonizerClasspath +org.jetbrains.kotlin:kotlin-native-utils:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-project-model:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-reflect:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-reflect:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath org.jetbrains.kotlin:kotlin-script-runtime:1.6.10=spotless1316845978 @@ -44,6 +47,7 @@ org.jetbrains.kotlin:kotlin-scripting-common:1.7.10=kotlinCompilerPluginClasspat org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-scripting-compiler-impl-embeddable:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-scripting-jvm:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-serialization:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-stdlib-common:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-stdlib-common:1.7.10=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10=spotless1316845978 @@ -52,6 +56,8 @@ org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10=apiDependenciesMetadata,compileCl org.jetbrains.kotlin:kotlin-stdlib:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-stdlib:1.7.10=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains.kotlin:kotlin-test:1.6.10=spotless1316845978 +org.jetbrains.kotlin:kotlin-tooling-core:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-util-io:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains:annotations:13.0=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,spotless1316845978,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jline:jline-native:3.25.1=compileClasspath,default,implementationDependenciesMetadata,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jline:jline-reader:3.25.1=compileClasspath,default,implementationDependenciesMetadata,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath diff --git a/pkl-codegen-java/gradle.lockfile b/pkl-codegen-java/gradle.lockfile index 8d529cfee..396f04d61 100644 --- a/pkl-codegen-java/gradle.lockfile +++ b/pkl-codegen-java/gradle.lockfile @@ -31,7 +31,10 @@ org.jetbrains.kotlin:kotlin-compiler-embeddable:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-compiler-embeddable:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath org.jetbrains.kotlin:kotlin-daemon-embeddable:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-daemon-embeddable:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath +org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-klib-commonizer-embeddable:1.7.10=kotlinKlibCommonizerClasspath +org.jetbrains.kotlin:kotlin-native-utils:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-project-model:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-reflect:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-reflect:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath org.jetbrains.kotlin:kotlin-script-runtime:1.6.10=spotless1316845978 @@ -40,6 +43,7 @@ org.jetbrains.kotlin:kotlin-scripting-common:1.7.10=kotlinCompilerPluginClasspat org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-scripting-compiler-impl-embeddable:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-scripting-jvm:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-serialization:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-stdlib-common:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-stdlib-common:1.7.10=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10=spotless1316845978 @@ -48,6 +52,8 @@ org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10=apiDependenciesMetadata,compileCl org.jetbrains.kotlin:kotlin-stdlib:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-stdlib:1.7.10=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains.kotlin:kotlin-test:1.6.10=spotless1316845978 +org.jetbrains.kotlin:kotlin-tooling-core:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-util-io:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains:annotations:13.0=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,spotless1316845978,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.junit.jupiter:junit-jupiter-api:5.10.2=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata org.junit.jupiter:junit-jupiter-engine:5.10.2=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata diff --git a/pkl-codegen-kotlin/gradle.lockfile b/pkl-codegen-kotlin/gradle.lockfile index 319a481a1..72d4f74d3 100644 --- a/pkl-codegen-kotlin/gradle.lockfile +++ b/pkl-codegen-kotlin/gradle.lockfile @@ -32,7 +32,10 @@ org.jetbrains.kotlin:kotlin-compiler-embeddable:1.7.10=kotlinCompilerClasspath,k org.jetbrains.kotlin:kotlin-daemon-client:1.7.10=testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata org.jetbrains.kotlin:kotlin-daemon-embeddable:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-daemon-embeddable:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath,testRuntimeClasspath +org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-klib-commonizer-embeddable:1.7.10=kotlinKlibCommonizerClasspath +org.jetbrains.kotlin:kotlin-native-utils:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-project-model:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-reflect:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-reflect:1.7.10=compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinKlibCommonizerClasspath,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains.kotlin:kotlin-script-runtime:1.6.10=spotless1316845978 @@ -42,6 +45,7 @@ org.jetbrains.kotlin:kotlin-scripting-common:1.7.10=kotlinCompilerPluginClasspat org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata org.jetbrains.kotlin:kotlin-scripting-compiler-impl-embeddable:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,testRuntimeClasspath org.jetbrains.kotlin:kotlin-scripting-jvm:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata +org.jetbrains.kotlin:kotlin-serialization:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-stdlib-common:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-stdlib-common:1.7.10=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10=spotless1316845978 @@ -50,7 +54,12 @@ org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10=apiDependenciesMetadata,compileCl org.jetbrains.kotlin:kotlin-stdlib:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-stdlib:1.7.10=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata org.jetbrains.kotlin:kotlin-test:1.6.10=spotless1316845978 +org.jetbrains.kotlin:kotlin-tooling-core:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-util-io:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.5.0=testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata +org.jetbrains.kotlinx:kotlinx-serialization-bom:1.5.0=compileClasspath,default,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.jetbrains.kotlinx:kotlinx-serialization-core-jvm:1.5.0=compileClasspath,default,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.0=compileClasspath,default,implementationDependenciesMetadata,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains:annotations:13.0=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,spotless1316845978,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata org.junit.jupiter:junit-jupiter-api:5.10.2=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata org.junit.jupiter:junit-jupiter-engine:5.10.2=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata diff --git a/pkl-codegen-kotlin/pkl-codegen-kotlin.gradle.kts b/pkl-codegen-kotlin/pkl-codegen-kotlin.gradle.kts index 1bc2d9d87..b8a206f38 100644 --- a/pkl-codegen-kotlin/pkl-codegen-kotlin.gradle.kts +++ b/pkl-codegen-kotlin/pkl-codegen-kotlin.gradle.kts @@ -31,6 +31,10 @@ dependencies { implementation(libs.kotlinPoet) implementation(libs.kotlinReflect) + implementation(libs.kotlinxSerializationCore) { + exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib") + exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib-common") + } testImplementation(project(":pkl-config-kotlin")) testImplementation(project(":pkl-commons-test")) diff --git a/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/CliKotlinCodeGeneratorOptions.kt b/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/CliKotlinCodeGeneratorOptions.kt index bf56f90d0..6c05774df 100644 --- a/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/CliKotlinCodeGeneratorOptions.kt +++ b/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/CliKotlinCodeGeneratorOptions.kt @@ -39,7 +39,10 @@ data class CliKotlinCodeGeneratorOptions( val generateSpringBootConfig: Boolean = false, /** Whether to make generated classes implement [java.io.Serializable] */ - val implementSerializable: Boolean = false + val implementSerializable: Boolean = false, + + /** Whether to annotate generated data classes with [kotlinx.serialization.Serializable] */ + val implementKSerializable: Boolean = false ) { fun toKotlinCodegenOptions(): KotlinCodegenOptions = KotlinCodegenOptions( @@ -48,5 +51,6 @@ data class CliKotlinCodeGeneratorOptions( generateSpringBootConfig = generateSpringBootConfig, implementSerializable = implementSerializable, kotlinPackage = kotlinPackage, + implementKSerializable = implementKSerializable, ) } diff --git a/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt b/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt index d4693ba2f..b5f473621 100644 --- a/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt +++ b/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt @@ -20,6 +20,7 @@ import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import java.io.StringWriter import java.net.URI import java.util.* +import kotlinx.serialization.Serializable import org.pkl.core.* import org.pkl.core.util.CodeGeneratorUtils @@ -37,7 +38,10 @@ data class KotlinCodegenOptions( val generateSpringBootConfig: Boolean = false, /** Whether to make generated classes implement [java.io.Serializable] */ - val implementSerializable: Boolean = false + val implementSerializable: Boolean = false, + + /** Whether to annotate data classes with [kotlinx.serialization.Serializable] */ + val implementKSerializable: Boolean = false, ) class KotlinCodeGeneratorException(message: String) : RuntimeException(message) @@ -138,7 +142,14 @@ class KotlinCodeGenerator( } for (pClass in moduleSchema.classes.values) { - moduleType.addType(generateTypeSpec(pClass, moduleSchema).ensureSerializable().build()) + moduleType.addType( + generateTypeSpec(pClass, moduleSchema) + .apply { + ensureSerializable() + ensureKSerializable() + } + .build() + ) } // generate append method for module classes w/o parent class; reuse in subclasses and nested @@ -522,6 +533,18 @@ class KotlinCodeGenerator( else generateRegularClass() } + private fun TypeSpec.Builder.ensureKSerializable(): TypeSpec.Builder { + if (!options.implementKSerializable) { + return this + } + + val spec = AnnotationSpec.builder(Serializable::class).build() + if (!this.annotationSpecs.contains(spec)) { + addAnnotation(spec) + } + return this + } + private fun TypeSpec.Builder.ensureSerializable(): TypeSpec.Builder { if (!options.implementSerializable) { return this diff --git a/pkl-codegen-kotlin/src/test/kotlin/org/pkl/codegen/kotlin/KotlinCodeGeneratorTest.kt b/pkl-codegen-kotlin/src/test/kotlin/org/pkl/codegen/kotlin/KotlinCodeGeneratorTest.kt index e8fd03198..5b15e45fc 100644 --- a/pkl-codegen-kotlin/src/test/kotlin/org/pkl/codegen/kotlin/KotlinCodeGeneratorTest.kt +++ b/pkl-codegen-kotlin/src/test/kotlin/org/pkl/codegen/kotlin/KotlinCodeGeneratorTest.kt @@ -133,7 +133,8 @@ class KotlinCodeGeneratorTest { pklCode: String, generateKdoc: Boolean = false, generateSpringBootConfig: Boolean = false, - implementSerializable: Boolean = false + implementSerializable: Boolean = false, + implementKSerializable: Boolean = false ): String { val module = Evaluator.preconfigured().evaluateSchema(ModuleSource.text(pklCode)) @@ -144,7 +145,8 @@ class KotlinCodeGeneratorTest { KotlinCodegenOptions( generateKdoc = generateKdoc, generateSpringBootConfig = generateSpringBootConfig, - implementSerializable = implementSerializable + implementSerializable = implementSerializable, + implementKSerializable = implementKSerializable, ) ) return generator.kotlinFile @@ -553,6 +555,152 @@ class KotlinCodeGeneratorTest { assertCompilesSuccessfully(kotlinCode) } + @Test + fun `data class implementing serializable`() { + val kotlinCode = + generateKotlinCode( + /* language=pkl */ + """ + module my.mod + + class Person { + name: String + age: Int + hobbies: List + friends: Map + sibling: Person? + } + """, + implementSerializable = true, + ) + + assertEqualTo( + """ + package my + + import java.io.Serializable + import kotlin.Long + import kotlin.String + import kotlin.collections.List + import kotlin.collections.Map + + object Mod { + data class Person( + val name: String, + val age: Long, + val hobbies: List, + val friends: Map, + val sibling: Person? + ) : Serializable { + companion object { + private const val serialVersionUID: Long = 0L + } + } + } + """, + kotlinCode + ) + + assertCompilesSuccessfully(kotlinCode) + } + + @Test + fun `data class implementing kserializable`() { + val kotlinCode = + generateKotlinCode( + /* language=pkl */ + """ + module my.mod + + class Person { + name: String + age: Int + hobbies: List + friends: Map + sibling: Person? + } + """, + implementKSerializable = true, + ) + + assertEqualTo( + """ + package my + + import kotlin.Long + import kotlin.String + import kotlin.collections.List + import kotlin.collections.Map + import kotlinx.serialization.Serializable + + object Mod { + @Serializable + data class Person( + val name: String, + val age: Long, + val hobbies: List, + val friends: Map, + val sibling: Person? + ) + } + """, + kotlinCode + ) + + assertCompilesSuccessfully(kotlinCode) + } + + @Test + fun `data class implementing all serialization`() { + val kotlinCode = + generateKotlinCode( + /* language=pkl */ + """ + module my.mod + + class Person { + name: String + age: Int + hobbies: List + friends: Map + sibling: Person? + } + """, + implementSerializable = true, + implementKSerializable = true, + ) + + assertEqualTo( + """ + package my + + import kotlin.Long + import kotlin.String + import kotlin.collections.List + import kotlin.collections.Map + import kotlinx.serialization.Serializable + + object Mod { + @Serializable + data class Person( + val name: String, + val age: Long, + val hobbies: List, + val friends: Map, + val sibling: Person? + ) : java.io.Serializable { + companion object { + private const val serialVersionUID: Long = 0L + } + } + } + """, + kotlinCode + ) + + assertCompilesSuccessfully(kotlinCode) + } + @Test fun `recursive types`() { val kotlinCode = diff --git a/pkl-commons-cli/gradle.lockfile b/pkl-commons-cli/gradle.lockfile index b746046db..dd720c447 100644 --- a/pkl-commons-cli/gradle.lockfile +++ b/pkl-commons-cli/gradle.lockfile @@ -29,7 +29,10 @@ org.jetbrains.kotlin:kotlin-compiler-embeddable:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-compiler-embeddable:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath org.jetbrains.kotlin:kotlin-daemon-embeddable:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-daemon-embeddable:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath +org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-klib-commonizer-embeddable:1.7.10=kotlinKlibCommonizerClasspath +org.jetbrains.kotlin:kotlin-native-utils:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-project-model:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-reflect:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-reflect:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath org.jetbrains.kotlin:kotlin-script-runtime:1.6.10=spotless1316845978 @@ -38,6 +41,7 @@ org.jetbrains.kotlin:kotlin-scripting-common:1.7.10=kotlinCompilerPluginClasspat org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-scripting-compiler-impl-embeddable:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-scripting-jvm:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-serialization:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-stdlib-common:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-stdlib-common:1.7.10=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10=spotless1316845978 @@ -46,6 +50,8 @@ org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10=apiDependenciesMetadata,compileCl org.jetbrains.kotlin:kotlin-stdlib:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-stdlib:1.7.10=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains.kotlin:kotlin-test:1.6.10=spotless1316845978 +org.jetbrains.kotlin:kotlin-tooling-core:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-util-io:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains:annotations:13.0=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,spotless1316845978,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.junit.jupiter:junit-jupiter-api:5.10.2=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata org.junit.jupiter:junit-jupiter-engine:5.10.2=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata diff --git a/pkl-commons-test/gradle.lockfile b/pkl-commons-test/gradle.lockfile index d204ee6b2..1c2c9367b 100644 --- a/pkl-commons-test/gradle.lockfile +++ b/pkl-commons-test/gradle.lockfile @@ -24,7 +24,10 @@ org.jetbrains.kotlin:kotlin-compiler-embeddable:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-compiler-embeddable:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath org.jetbrains.kotlin:kotlin-daemon-embeddable:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-daemon-embeddable:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath +org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-klib-commonizer-embeddable:1.7.10=kotlinKlibCommonizerClasspath +org.jetbrains.kotlin:kotlin-native-utils:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-project-model:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-reflect:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-reflect:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath org.jetbrains.kotlin:kotlin-script-runtime:1.6.10=spotless1316845978 @@ -33,6 +36,7 @@ org.jetbrains.kotlin:kotlin-scripting-common:1.7.10=kotlinCompilerPluginClasspat org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-scripting-compiler-impl-embeddable:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-scripting-jvm:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-serialization:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-stdlib-common:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-stdlib-common:1.7.10=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10=spotless1316845978 @@ -41,6 +45,8 @@ org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10=apiDependenciesMetadata,compileCl org.jetbrains.kotlin:kotlin-stdlib:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-stdlib:1.7.10=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains.kotlin:kotlin-test:1.6.10=spotless1316845978 +org.jetbrains.kotlin:kotlin-tooling-core:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-util-io:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains:annotations:13.0=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,spotless1316845978,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.junit.jupiter:junit-jupiter-api:5.10.2=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata org.junit.jupiter:junit-jupiter-engine:5.10.2=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata diff --git a/pkl-commons/gradle.lockfile b/pkl-commons/gradle.lockfile index c040aea2f..65381f029 100644 --- a/pkl-commons/gradle.lockfile +++ b/pkl-commons/gradle.lockfile @@ -24,7 +24,10 @@ org.jetbrains.kotlin:kotlin-compiler-embeddable:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-compiler-embeddable:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath org.jetbrains.kotlin:kotlin-daemon-embeddable:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-daemon-embeddable:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath +org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-klib-commonizer-embeddable:1.7.10=kotlinKlibCommonizerClasspath +org.jetbrains.kotlin:kotlin-native-utils:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-project-model:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-reflect:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-reflect:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath org.jetbrains.kotlin:kotlin-script-runtime:1.6.10=spotless1316845978 @@ -33,6 +36,7 @@ org.jetbrains.kotlin:kotlin-scripting-common:1.7.10=kotlinCompilerPluginClasspat org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-scripting-compiler-impl-embeddable:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-scripting-jvm:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-serialization:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-stdlib-common:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-stdlib-common:1.7.10=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10=spotless1316845978 @@ -41,6 +45,8 @@ org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10=apiDependenciesMetadata,compileCl org.jetbrains.kotlin:kotlin-stdlib:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-stdlib:1.7.10=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains.kotlin:kotlin-test:1.6.10=spotless1316845978 +org.jetbrains.kotlin:kotlin-tooling-core:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-util-io:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains:annotations:13.0=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,spotless1316845978,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.junit.jupiter:junit-jupiter-api:5.10.2=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata org.junit.jupiter:junit-jupiter-engine:5.10.2=testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata diff --git a/pkl-config-kotlin/gradle.lockfile b/pkl-config-kotlin/gradle.lockfile index fa7556150..96de47bcb 100644 --- a/pkl-config-kotlin/gradle.lockfile +++ b/pkl-config-kotlin/gradle.lockfile @@ -31,7 +31,10 @@ org.jetbrains.kotlin:kotlin-compiler-embeddable:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-compiler-embeddable:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath org.jetbrains.kotlin:kotlin-daemon-embeddable:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-daemon-embeddable:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath +org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-klib-commonizer-embeddable:1.7.10=kotlinKlibCommonizerClasspath +org.jetbrains.kotlin:kotlin-native-utils:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-project-model:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-reflect:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-reflect:1.7.10=compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinKlibCommonizerClasspath,pklCodegenKotlin,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains.kotlin:kotlin-script-runtime:1.6.10=spotless1316845978 @@ -40,6 +43,7 @@ org.jetbrains.kotlin:kotlin-scripting-common:1.7.10=kotlinCompilerPluginClasspat org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-scripting-compiler-impl-embeddable:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-scripting-jvm:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-serialization:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-stdlib-common:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-stdlib-common:1.7.10=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,pklCodegenKotlin,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10=spotless1316845978 @@ -48,6 +52,11 @@ org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10=apiDependenciesMetadata,compileCl org.jetbrains.kotlin:kotlin-stdlib:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-stdlib:1.7.10=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,pklCodegenKotlin,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains.kotlin:kotlin-test:1.6.10=spotless1316845978 +org.jetbrains.kotlin:kotlin-tooling-core:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-util-io:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlinx:kotlinx-serialization-bom:1.5.0=pklCodegenKotlin +org.jetbrains.kotlinx:kotlinx-serialization-core-jvm:1.5.0=pklCodegenKotlin +org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.0=pklCodegenKotlin org.jetbrains:annotations:13.0=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,pklCodegenKotlin,runtimeClasspath,spotless1316845978,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.junit.jupiter:junit-jupiter-api:5.10.2=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata org.junit.jupiter:junit-jupiter-engine:5.10.2=testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata diff --git a/pkl-doc/pkl-doc.gradle.kts b/pkl-doc/pkl-doc.gradle.kts index a222b94a3..53ac435d5 100644 --- a/pkl-doc/pkl-doc.gradle.kts +++ b/pkl-doc/pkl-doc.gradle.kts @@ -6,7 +6,7 @@ plugins { pklPublishLibrary pklHtmlValidator @Suppress("DSL_SCOPE_VIOLATION") // https://youtrack.jetbrains.com/issue/KTIJ-19369 - alias(libs.plugins.kotlinxSerialization) + id(libs.plugins.kotlinxSerialization.get().pluginId) } val graalVmBaseDir = buildInfo.graalVm.baseDir diff --git a/pkl-gradle/src/main/java/org/pkl/gradle/PklPlugin.java b/pkl-gradle/src/main/java/org/pkl/gradle/PklPlugin.java index 10ffdf669..1d32d0dc3 100644 --- a/pkl-gradle/src/main/java/org/pkl/gradle/PklPlugin.java +++ b/pkl-gradle/src/main/java/org/pkl/gradle/PklPlugin.java @@ -197,6 +197,8 @@ private void configureKotlinCodeGenTasks(NamedDomainObjectContainer getKotlinPackage(); Property getGenerateKdoc(); + + Property getImplementSerializable(); + + Property getImplementKSerializable(); } diff --git a/pkl-gradle/src/main/java/org/pkl/gradle/task/KotlinCodeGenTask.java b/pkl-gradle/src/main/java/org/pkl/gradle/task/KotlinCodeGenTask.java index 7ed4175e8..a4cc1a95f 100644 --- a/pkl-gradle/src/main/java/org/pkl/gradle/task/KotlinCodeGenTask.java +++ b/pkl-gradle/src/main/java/org/pkl/gradle/task/KotlinCodeGenTask.java @@ -28,6 +28,9 @@ public abstract class KotlinCodeGenTask extends CodeGenTask { @Input public abstract Property getKotlinPackage(); + @Input + public abstract Property getImplementKSerializable(); + @Override protected void doRunTask() { //noinspection ResultOfMethodCallIgnored @@ -41,7 +44,8 @@ protected void doRunTask() { getKotlinPackage().get(), getGenerateKdoc().get(), getGenerateSpringBootConfig().get(), - getImplementSerializable().get())) + getImplementSerializable().get(), + getImplementKSerializable().get())) .run(); } } diff --git a/pkl-gradle/src/test/kotlin/org/pkl/gradle/KotlinCodeGeneratorsTest.kt b/pkl-gradle/src/test/kotlin/org/pkl/gradle/KotlinCodeGeneratorsTest.kt index 3e563ff66..eae484a5a 100644 --- a/pkl-gradle/src/test/kotlin/org/pkl/gradle/KotlinCodeGeneratorsTest.kt +++ b/pkl-gradle/src/test/kotlin/org/pkl/gradle/KotlinCodeGeneratorsTest.kt @@ -65,7 +65,22 @@ class KotlinCodeGeneratorsTest : AbstractTest() { assertThat(personClassFile).exists() assertThat(addressClassFile).exists() } - + + @Test + fun `compile generated code with kserialization support`() { + writeBuildFile(kotlinxSerde = true) + writePklFile() + runTask("compileKotlin") + + val classesDir = testProjectDir.resolve("build/classes/kotlin/main") + val moduleClassFile = classesDir.resolve("org/Mod.class") + val personClassFile = classesDir.resolve("org/Mod\$Person.class") + val addressClassFile = classesDir.resolve("org/Mod\$Address.class") + assertThat(moduleClassFile).exists() + assertThat(personClassFile).exists() + assertThat(addressClassFile).exists() + } + @Test fun `no source modules`() { writeFile( @@ -88,8 +103,14 @@ class KotlinCodeGeneratorsTest : AbstractTest() { assertThat(result.output).contains("No source modules specified.") } - private fun writeBuildFile() { - val kotlinVersion = "1.6.0" + private fun writeBuildFile(kotlinxSerde: Boolean = false) { + val kotlinVersion = "1.7.10" + val kotlinXSerdeVersion = "1.5.0" + val kotlinXRuntimeDependency = StringBuilder().apply { + append("org.jetbrains.kotlinx:kotlinx-serialization-core") + append(":") + append(kotlinXSerdeVersion) + }.toString() writeFile( "build.gradle", """ @@ -102,6 +123,7 @@ class KotlinCodeGeneratorsTest : AbstractTest() { classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion") { exclude module: "kotlin-android-extensions" } + classpath("org.jetbrains.kotlin:kotlin-serialization:$kotlinVersion") } } @@ -110,6 +132,7 @@ class KotlinCodeGeneratorsTest : AbstractTest() { } apply plugin: "kotlin" + ${if (kotlinxSerde) "apply plugin: 'kotlinx-serialization'" else ""} repositories { mavenCentral() @@ -117,6 +140,7 @@ class KotlinCodeGeneratorsTest : AbstractTest() { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion" + ${if (kotlinxSerde) "implementation '$kotlinXRuntimeDependency'" else ""} } pkl { diff --git a/pkl-server/gradle.lockfile b/pkl-server/gradle.lockfile index b3b3cbf48..5d74d2a4b 100644 --- a/pkl-server/gradle.lockfile +++ b/pkl-server/gradle.lockfile @@ -27,7 +27,10 @@ org.jetbrains.kotlin:kotlin-compiler-embeddable:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-compiler-embeddable:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath org.jetbrains.kotlin:kotlin-daemon-embeddable:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-daemon-embeddable:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath +org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-klib-commonizer-embeddable:1.7.10=kotlinKlibCommonizerClasspath +org.jetbrains.kotlin:kotlin-native-utils:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-project-model:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-reflect:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-reflect:1.7.10=kotlinCompilerClasspath,kotlinKlibCommonizerClasspath org.jetbrains.kotlin:kotlin-script-runtime:1.6.10=spotless1316845978 @@ -36,6 +39,7 @@ org.jetbrains.kotlin:kotlin-scripting-common:1.7.10=kotlinCompilerPluginClasspat org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-scripting-compiler-impl-embeddable:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-scripting-jvm:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-serialization:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains.kotlin:kotlin-stdlib-common:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-stdlib-common:1.7.10=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10=spotless1316845978 @@ -44,6 +48,8 @@ org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10=apiDependenciesMetadata,compileCl org.jetbrains.kotlin:kotlin-stdlib:1.6.10=spotless1316845978 org.jetbrains.kotlin:kotlin-stdlib:1.7.10=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.jetbrains.kotlin:kotlin-test:1.6.10=spotless1316845978 +org.jetbrains.kotlin:kotlin-tooling-core:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest +org.jetbrains.kotlin:kotlin-util-io:1.7.10=kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest org.jetbrains:annotations:13.0=apiDependenciesMetadata,compileClasspath,default,implementationDependenciesMetadata,kotlinCompilerClasspath,kotlinCompilerPluginClasspathMain,kotlinCompilerPluginClasspathTest,kotlinKlibCommonizerClasspath,runtimeClasspath,spotless1316845978,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath org.junit.jupiter:junit-jupiter-api:5.10.2=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata org.junit.jupiter:junit-jupiter-engine:5.10.2=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath,testRuntimeOnlyDependenciesMetadata