Skip to content

[kotlin-client] Support for integer enums for multiplatform with custom serializer#23211

Merged
wing328 merged 4 commits intomasterfrom
rlnt-kotlin-client/integer-enums-multiplatform
Mar 12, 2026
Merged

[kotlin-client] Support for integer enums for multiplatform with custom serializer#23211
wing328 merged 4 commits intomasterfrom
rlnt-kotlin-client/integer-enums-multiplatform

Conversation

@wing328
Copy link
Copy Markdown
Member

@wing328 wing328 commented Mar 12, 2026

based on #21983 and added a test to cover integer enums

cc @karismann (2019/03) @Zomzog (2019/04) @andrewemery (2019/10) @4brunu (2019/11) @yutaka0m (2020/03) @stefankoppier (2022/06) @e5l (2024/10) @dennisameling (2026/02)

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Adds a custom kotlinx.serialization serializer for non-string enums in the Kotlin Multiplatform client so integer/number enums encode and decode correctly. This fixes incorrect handling of numeric enums in multiplatform builds.

  • New Features
    • Generate {{classname}}Serializer for non-string enums that uses the underlying {{dataType}}.serializer() and maps by value.
    • Annotate multiplatform non-string enums with @Serializable(with = ...); apply @SerialName only for string enums.
    • Add missing kotlinx.serialization imports (Decoder, Encoder, builtins.serializer).

Written for commit 96c5385. Summary will update on new commits.

@wing328 wing328 added this to the 7.21.0 milestone Mar 12, 2026
@wing328 wing328 changed the title Rlnt kotlin client/integer enums multiplatform [kotlin-client] Support for integer enums for multiplatform with custom serializer Mar 12, 2026
@wing328 wing328 marked this pull request as ready for review March 12, 2026 01:56
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 issues found across 36 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/resources/kotlin-client/enum_class.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/kotlin-client/enum_class.mustache:187">
P3: Use `entries` instead of `values()` in enum deserialization to avoid per-call array allocation.</violation>
</file>

<file name="samples/client/petstore/kotlin-enum-integers-multiplatform/src/test/kotlin/org/openapitools/client/models/CodeTest.kt">

<violation number="1" location="samples/client/petstore/kotlin-enum-integers-multiplatform/src/test/kotlin/org/openapitools/client/models/CodeTest.kt:24">
P2: This test file currently has no actual test case, so it does not validate integer enum behavior.</violation>
</file>

<file name="samples/client/petstore/kotlin-enum-integers-multiplatform/src/commonMain/kotlin/org/openapitools/client/infrastructure/ApiClient.kt">

<violation number="1" location="samples/client/petstore/kotlin-enum-integers-multiplatform/src/commonMain/kotlin/org/openapitools/client/infrastructure/ApiClient.kt:23">
P1: The public primary constructor allows creating `ApiClient` without initializing `client`, which can crash at runtime with `UninitializedPropertyAccessException`.</violation>
</file>

<file name="samples/client/petstore/kotlin-enum-integers-multiplatform/src/test/kotlin/org/openapitools/client/apis/DefaultApiTest.kt">

<violation number="1" location="samples/client/petstore/kotlin-enum-integers-multiplatform/src/test/kotlin/org/openapitools/client/apis/DefaultApiTest.kt:30">
P2: This test case is empty, so it always passes and provides no coverage for the behavior it is named to validate.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.


import org.openapitools.client.auth.*

open class ApiClient(
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: The public primary constructor allows creating ApiClient without initializing client, which can crash at runtime with UninitializedPropertyAccessException.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/kotlin-enum-integers-multiplatform/src/commonMain/kotlin/org/openapitools/client/infrastructure/ApiClient.kt, line 23:

<comment>The public primary constructor allows creating `ApiClient` without initializing `client`, which can crash at runtime with `UninitializedPropertyAccessException`.</comment>

<file context>
@@ -0,0 +1,188 @@
+
+import org.openapitools.client.auth.*
+
+open class ApiClient(
+        private val baseUrl: String
+) {
</file context>
Fix with Cubic

import org.openapitools.client.models.Code

class CodeTest : ShouldSpec() {
init {
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This test file currently has no actual test case, so it does not validate integer enum behavior.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/kotlin-enum-integers-multiplatform/src/test/kotlin/org/openapitools/client/models/CodeTest.kt, line 24:

<comment>This test file currently has no actual test case, so it does not validate integer enum behavior.</comment>

<file context>
@@ -0,0 +1,29 @@
+import org.openapitools.client.models.Code
+
+class CodeTest : ShouldSpec() {
+    init {
+        // uncomment below to create an instance of Code
+        //val modelInstance = Code()
</file context>
Fix with Cubic

//val apiInstance = DefaultApi()

// to test doGet
should("test doGet") {
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This test case is empty, so it always passes and provides no coverage for the behavior it is named to validate.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/kotlin-enum-integers-multiplatform/src/test/kotlin/org/openapitools/client/apis/DefaultApiTest.kt, line 30:

<comment>This test case is empty, so it always passes and provides no coverage for the behavior it is named to validate.</comment>

<file context>
@@ -0,0 +1,37 @@
+        //val apiInstance = DefaultApi()
+
+        // to test doGet
+        should("test doGet") {
+            // uncomment below to test doGet
+            //val result : DoGet200Response = apiInstance.doGet()
</file context>
Fix with Cubic


override fun deserialize(decoder: Decoder): {{classname}} {
val value = decoder.decodeSerializableValue({{{dataType}}}.serializer())
return {{classname}}.values().firstOrNull { it.value == value }
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: Use entries instead of values() in enum deserialization to avoid per-call array allocation.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/resources/kotlin-client/enum_class.mustache, line 187:

<comment>Use `entries` instead of `values()` in enum deserialization to avoid per-call array allocation.</comment>

<file context>
@@ -172,3 +177,20 @@ internal object {{classname}}Serializer : KSerializer<{{classname}}> {
+
+    override fun deserialize(decoder: Decoder): {{classname}} {
+        val value = decoder.decodeSerializableValue({{{dataType}}}.serializer())
+        return {{classname}}.values().firstOrNull { it.value == value }
+            ?: throw IllegalArgumentException("Unknown enum value: $value")
+    }
</file context>
Suggested change
return {{classname}}.values().firstOrNull { it.value == value }
return {{classname}}.entries.firstOrNull { it.value == value }
Fix with Cubic

@wing328 wing328 merged commit 54cedcb into master Mar 12, 2026
150 of 165 checks passed
@wing328 wing328 deleted the rlnt-kotlin-client/integer-enums-multiplatform branch March 12, 2026 02:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants