Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #10902
Closed
Open
Issue created Nov 19, 2021 by Administrator@rootContributor4 of 6 checklist items completed4/6 checklist items

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

Created by: 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

Assignee
Assign to
Time tracking