Skip to content

[BUG] Kotlin multiplatform, Kotlinx Serialization, Error while generating enum classes #10902

@Qbit982

Description

@Qbit982

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)

Description

Openapi generator produce enum classes with error while using kotlinx.serialization

Version

Gradle plugin "org.openapi.generator"
5.3.0

Gradle version 7.0.0-bin

OpenAPI declaration file content or url

---
openapi: 3.0.3
info:
  title: Generated API
  version: "1.0"
paths: {}
components:
  schemas:
    Foo:
      description: "Enum class Foo"
      enum:
        - Foo
        - Bar
      type: string

Generation Details

tasks.register("generateKotlinClient", org.openapitools.generator.gradle.plugin.tasks.GenerateTask::class) {
    generatorName.set("kotlin")
    library.set("multiplatform")
    inputSpec.set("$rootDir/{NAME_YML_FILE}.yml")
    outputDir.set("$buildDir/generated")
}

Steps to reproduce

./gradlew clean generateKotlinClient

Output vs Expected Output

Output

@file:Suppress(
    "ArrayInDataClass",
    "EnumEntryName",
    "RemoveRedundantQualifierName",
    "UnusedImport"
)

package org.openapitools.client.models

import kotlinx.serialization.*

@Serializable(with = Foo.Serializer::class)
enum class Foo(val value: kotlin.String) {

    @SerialName(value = "Foo")
    foo("Foo"),

    @SerialName(value = "Bar")
    bar("Bar");

    override fun toString(): String = value

    companion object {
        fun encode(data: Any?): kotlin.String? = if (data is Foo) "$data" else null

        fun decode(data: Any?): Foo? = data?.let {
          val normalizedData = "$it".lowercase()
          values().firstOrNull { value ->
            it == value || normalizedData == "$value".lowercase()
          }
        }
    }
}

which results with

@Serializable(with = Foo.Serializer::class)

however, I would expect

@Serializable

When trying to build with the output by generated build.gradle.kts in the $buildDir/generated, I am catching error by task compileKotlinJs

Task :compileKotlinJs FAILED
\build\generated\src\commonMain\kotlin...\models\Foo.kt: (31, 22): An annotation argument must be a compile-time constant
\build\generated\src\commonMain\kotlin...\models\Foo.kt: (31, 26): Unresolved reference: Serializer

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions