[PR #196] [CLOSED] Implement support for KotlinX Serialization #442

Closed
opened 2025-12-30 01:24:30 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/apple/pkl/pull/196
Author: @sgammon
Created: 2/18/2024
Status: Closed

Base: mainHead: feat/issue-186


📝 Commits (3)

  • a89e910 Fix dropped implementSerializable flag
  • e9638dd Add setting for Kotlin package to codegen
  • a1613a1 Implement support for KotlinX Serialization

📊 Changes

29 files changed (+874 additions, -228 deletions)

View changed files

📝 bench/gradle.lockfile (+22 -14)
📝 buildSrc/build.gradle.kts (+1 -0)
📝 buildSrc/src/main/kotlin/pklKotlinLibrary.gradle.kts (+1 -0)
📝 docs/gradle.lockfile (+10 -10)
📝 gradle/libs.versions.toml (+4 -3)
📝 pkl-cli/gradle.lockfile (+45 -17)
📝 pkl-codegen-java/gradle.lockfile (+41 -13)
📝 pkl-codegen-kotlin/gradle.lockfile (+44 -13)
📝 pkl-codegen-kotlin/pkl-codegen-kotlin.gradle.kts (+4 -0)
📝 pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/CliKotlinCodeGeneratorOptions.kt (+15 -2)
📝 pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt (+42 -6)
📝 pkl-codegen-kotlin/src/test/kotlin/org/pkl/codegen/kotlin/KotlinCodeGeneratorTest.kt (+242 -4)
📝 pkl-commons-cli/gradle.lockfile (+40 -12)
📝 pkl-commons-test/gradle.lockfile (+39 -11)
📝 pkl-commons/gradle.lockfile (+40 -12)
📝 pkl-config-java/gradle.lockfile (+19 -11)
📝 pkl-config-kotlin/gradle.lockfile (+44 -13)
📝 pkl-core/gradle.lockfile (+18 -11)
📝 pkl-doc/gradle.lockfile (+46 -26)
📝 pkl-doc/pkl-doc.gradle.kts (+1 -1)

...and 9 more files

📄 Description

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.

New discussion opened here

Approach

A new argument has been added to the Kotlin code generator and Gradle plugin, at implementKSerializable. When set to true, the code generator annotates data classes with @kotlinx.serialization.Serializable.

If both JVM serialization and KotlinX serialization are enabled, the code generator properly qualifies with java.io.Serializable and import kotlinx.serialization.Serializable. The two are optional and usable independent of each other.

For the moment, this is filed on top of these other PRs to avoid conflicts, since they all touch the code generator; these can be rebased away, though:

Transitive dependency needs

Users of this code will need to include the KotlinX Serialization compiler plugin in their build, with:

plugins {
  kotlin("plugin.serialization")

  // -- or --
  alias(libs.plugins.kotlin.serialization)  // version catalog

  // -- or --
  id(libs.plugins.kotlin.serialization.get().pluginId)  // multi-module build with version catalog
}

And a dependency on KotlinX Serialization Core, for the annotation, at version 1.5.0 or greater:

dependencies {
  api("org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.0")

  // -- or --
  api(libs.kotlinx.serialization.core)
}
[versions]
# ...
kotlinx-serialization = "1.5.0"  # or greater for newer Kotlin metadata versions
# ...

[libraries]
# ...
kotlinx-serialization = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" }
# ...

I stopped short of adding an api dependency to Pkl's Kotlin runtime library to avoid adding it to consumer dependency graphs downstream. This could easily be added, though, if preferable. Gradle will generally select the newest available version in a user's build, but builds that don't include KotlinX Serialization already will have to download it if it is added this way, of course.

IDEA and Kotlin warn appropriately if the compiler is not included.

Changelog

  • 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
  • chore: update lockfiles

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/apple/pkl/pull/196 **Author:** [@sgammon](https://github.com/sgammon) **Created:** 2/18/2024 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/issue-186` --- ### 📝 Commits (3) - [`a89e910`](https://github.com/apple/pkl/commit/a89e9105515721d1a59fadc80a16883183e0b6f2) Fix dropped `implementSerializable` flag - [`e9638dd`](https://github.com/apple/pkl/commit/e9638ddf9821960d0bcac2ee14c51b8c330a0d7e) Add setting for Kotlin package to codegen - [`a1613a1`](https://github.com/apple/pkl/commit/a1613a1c7a9140bebe494d7589bdafd0558a081d) Implement support for KotlinX Serialization ### 📊 Changes **29 files changed** (+874 additions, -228 deletions) <details> <summary>View changed files</summary> 📝 `bench/gradle.lockfile` (+22 -14) 📝 `buildSrc/build.gradle.kts` (+1 -0) 📝 `buildSrc/src/main/kotlin/pklKotlinLibrary.gradle.kts` (+1 -0) 📝 `docs/gradle.lockfile` (+10 -10) 📝 `gradle/libs.versions.toml` (+4 -3) 📝 `pkl-cli/gradle.lockfile` (+45 -17) 📝 `pkl-codegen-java/gradle.lockfile` (+41 -13) 📝 `pkl-codegen-kotlin/gradle.lockfile` (+44 -13) 📝 `pkl-codegen-kotlin/pkl-codegen-kotlin.gradle.kts` (+4 -0) 📝 `pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/CliKotlinCodeGeneratorOptions.kt` (+15 -2) 📝 `pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt` (+42 -6) 📝 `pkl-codegen-kotlin/src/test/kotlin/org/pkl/codegen/kotlin/KotlinCodeGeneratorTest.kt` (+242 -4) 📝 `pkl-commons-cli/gradle.lockfile` (+40 -12) 📝 `pkl-commons-test/gradle.lockfile` (+39 -11) 📝 `pkl-commons/gradle.lockfile` (+40 -12) 📝 `pkl-config-java/gradle.lockfile` (+19 -11) 📝 `pkl-config-kotlin/gradle.lockfile` (+44 -13) 📝 `pkl-core/gradle.lockfile` (+18 -11) 📝 `pkl-doc/gradle.lockfile` (+46 -26) 📝 `pkl-doc/pkl-doc.gradle.kts` (+1 -1) _...and 9 more files_ </details> ### 📄 Description 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. New discussion opened [here](https://github.com/apple/pkl/discussions/186) ## Approach A new argument has been added to the Kotlin code generator and Gradle plugin, at `implementKSerializable`. When set to `true`, the code generator annotates data classes with `@kotlinx.serialization.Serializable`. If both JVM serialization and KotlinX serialization are enabled, the code generator properly qualifies with `java.io.Serializable` and `import kotlinx.serialization.Serializable`. The two are optional and usable independent of each other. For the moment, this is filed on top of these other PRs to avoid conflicts, since they all touch the code generator; these can be rebased away, though: - #194 - #192 ### Transitive dependency needs Users of this code will need to include the KotlinX Serialization compiler plugin in their build, with: ```kotlin plugins { kotlin("plugin.serialization") // -- or -- alias(libs.plugins.kotlin.serialization) // version catalog // -- or -- id(libs.plugins.kotlin.serialization.get().pluginId) // multi-module build with version catalog } ``` And a dependency on KotlinX Serialization Core, for the annotation, at version `1.5.0` or greater: ```kotlin dependencies { api("org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.0") // -- or -- api(libs.kotlinx.serialization.core) } ``` ```toml [versions] # ... kotlinx-serialization = "1.5.0" # or greater for newer Kotlin metadata versions # ... [libraries] # ... kotlinx-serialization = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" } # ... ``` I stopped short of adding an `api` dependency to Pkl's Kotlin runtime library to avoid adding it to consumer dependency graphs downstream. This could easily be added, though, if preferable. Gradle will generally select the newest available version in a user's build, but builds that don't include KotlinX Serialization already will have to download it if it is added this way, of course. IDEA and Kotlin warn appropriately if the compiler is not included. ### Changelog - 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 - chore: update lockfiles --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-30 01:24:30 +01:00
adam closed this issue 2025-12-30 01:24:30 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#442