OpenAPITools / OpenAPITools/openapi-generator

[BUG] [KOTLIN-CLIENT] enums with empty entries to generate invalid kotlin-client code

Open
#18,660 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

A spec with an empty enum entry no longer generates a valid enum in kotlin code.

openapi-generator version

7.4.0, also 7.5.0

OpenAPI declaration file content or url
openapi: 3.0.2
info:
  title: API
  version: 1.0.0
paths:
  /api/v1/foo/:
    get:
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Foo'
components:
  schemas:
    Foo:
      type: object
      description: foo
      properties:
        field_name:
          nullable: true
          enum:
            - ''
            - stringvalue1
          type: string
Generation Details

will generate this kotlin code:

/**
 *
 * Please note:
 * This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * Do not edit this file manually.
 *
 */

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


import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
 * foo
 *
 * @param fieldName 
 */
@JsonClass(generateAdapter = true)

data class ApiFoo (

    @Json(name = "field_name")
    val fieldName: ApiFoo.FieldName? = null

) {

    /**
     * 
     *
     * Values: ,stringvalue1
     */
    @JsonClass(generateAdapter = false)
    enum class FieldName(val value: kotlin.String) {
        @Json(name = "") (""),
        @Json(name = "stringvalue1") stringvalue1("stringvalue1");
    }
}

which has the invalid first entry. Previously this (in 7.3.0) this would generate the following:

/**
 *
 * Please note:
 * This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * Do not edit this file manually.
 *
 */

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


import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
 * foo
 *
 * @param fieldName 
 */
@JsonClass(generateAdapter = true)

data class ApiFoo (

    @Json(name = "field_name")
    val fieldName: ApiFoo.FieldName? = null

) {

    /**
     * 
     *
     * Values: eMPTY,stringvalue1
     */
    @JsonClass(generateAdapter = false)
    enum class FieldName(val value: kotlin.String) {
        @Json(name = "") eMPTY(""),
        @Json(name = "stringvalue1") stringvalue1("stringvalue1");
    }
}

with the eMPTY entry.

here is my openApiGenerate task

openApiGenerate {
  generatorName = "kotlin"
  inputSpec = specFile
  outputDir = generationDir
  templateDir = "$templatesDir"
  apiPackage = "com.giftster.network.api"
  packageName = "com.giftster.network"
  cleanupOutput = true
  modelNamePrefix = "Api"
  library = "jvm-retrofit2"
  generateApiTests = false
  generateModelTests = false
  configOptions.set([
    artifactId              : "client",
    dateLibrary             : "java8",
    groupId                 : "com.foo,
    moshiCodeGen            : "true",
    omitGradlePluginVersions: "true",
    omitGradleWrapper       : "true",
    useCoroutines           : "true"
  ])
}
Steps to reproduce

Run the generation on the yaml.

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/pull/18062

Suggest a fix

Should revert the enum generation changes or provide guidance on fixing the breaking change.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied OpenAPI YAML and reproduce the output using the Kotlin generator at version 7.4.0 or 7.5.0. Review the enum-generation changes in related PR #18062 and compare them with the 7.3.0 output. Done means an empty enum value produces valid Kotlin code while preserving its empty serialized value.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.