OpenAPITools / OpenAPITools/openapi-generator

[BUG] [multiplatform] Support for x-enum-varnames

Open
#10,466 2 comments 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

With this setup:

AS Artic Fox 2020.3.1 Patch 2

object Versions {
    const val iosTestTask = false
    const val foo = "8.6.8"
    const val kotlin = "1.5.31"

    // Gradle
    const val androidGradlePlugin = "7.0.2"
    const val gradle = "gradle-7.2-bin"

    // Android
    const val compileSdk = 30
    const val minSdk = 23
    const val targetSdk = compileSdk

    // You can also use this to specify versions for dependencies. Having consistent
    // versions between modules can avoid behavior conflicts.
    const val sqldelight = "1.5.1"
    const val settings = "0.8"
    const val serialization = "1.2.2"
    // https://kotlinlang.org/docs/mobile/concurrency-and-coroutines.html#multithreaded-coroutines
    const val coroutines = "1.5.1-native-mt"
    const val ktor = "1.6.3"
    const val openapigen = "5.2.1"
}

For enum declaration in the openapi specification like that:

"softwareTiers" : {
            "type" : "array",
            "items" : {
              "type" : "integer",
              "format" : "int32",
              "enum" : [ 200, 250, 300, 310, 350, 400, 401, 701, 503, 600, 900, 410, 800, 8888, 9999, 1000, 6666, 7777, 7778, 1100, 5555, 11111, 21111, 22111 ],
              "x-enum-varnames" : [ "CASISTICA_GUASTI", "CASISTICA_GUASTI_EDIT", "OFFICINA", "ODL", "WORKSHOP_NET", "REVISIONI", "REGISTRO_REVISIONI", "KROMEDA", "DATA_FULL", "FATTURAZIONE_ITALIA", "COMUNICAZIONI", "SMART", "YAP_LITE", "ADMIN", "LANG", "MAGAZZINO", "MEZZI_SPECIALI", "GOMME", "CONTESTO_GOMME", "CED", "PRIMA_NOTA", "E_TICKET", "FCA", "FIRMA" ]
            }
          },

I've found this error:

/**
     * 
     *
     * Values: CASISTICA_GUASTI,CASISTICA_GUASTI_EDIT,OFFICINA,ODL,WORKSHOP_NET,REVISIONI,REGISTRO_REVISIONI,KROMEDA,DATA_FULL,FATTURAZIONE_ITALIA,COMUNICAZIONI,SMART,YAP_LITE,ADMIN,LANG,MAGAZZINO,MEZZI_SPECIALI,GOMME,CONTESTO_GOMME,CED,PRIMA_NOTA,E_TICKET,FCA,FIRMA
     */
    @Serializable
    enum class SoftwareTiers(val value: kotlin.String) {
        @SerialName(value = "200") CASISTICA_GUASTI(200),
        @SerialName(value = "250") CASISTICA_GUASTI_EDIT(250),
        @SerialName(value = "300") OFFICINA(300),
        @SerialName(value = "310") ODL(310),
        @SerialName(value = "350") WORKSHOP_NET(350),
        @SerialName(value = "400") REVISIONI(400),
        @SerialName(value = "401") REGISTRO_REVISIONI(401),
        @SerialName(value = "701") KROMEDA(701),
        @SerialName(value = "503") DATA_FULL(503),
        @SerialName(value = "600") FATTURAZIONE_ITALIA(600),
        @SerialName(value = "900") COMUNICAZIONI(900),
        @SerialName(value = "410") SMART(410),
        @SerialName(value = "800") YAP_LITE(800),
        @SerialName(value = "8888") ADMIN(8888),
        @SerialName(value = "9999") LANG(9999),
        @SerialName(value = "1000") MAGAZZINO(1000),
        @SerialName(value = "6666") MEZZI_SPECIALI(6666),
        @SerialName(value = "7777") GOMME(7777),
        @SerialName(value = "7778") CONTESTO_GOMME(7778),
        @SerialName(value = "1100") CED(1100),
        @SerialName(value = "5555") PRIMA_NOTA(5555),
        @SerialName(value = "11111") E_TICKET(11111),
        @SerialName(value = "21111") FCA(21111),
        @SerialName(value = "22111") FIRMA(22111);
    }

The integer literal does not conform to the expected type String

I've found a solution changing this file: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/kotlin-client/data_class.mustache

By changing this part:

{{#multiplatform}}
        @SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{^isString}}{{#isContainer}}"{{/isContainer}}{{/isString}}{{{value}}}{{^isString}}{{#isContainer}}"{{/isContainer}}{{/isString}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
        {{/multiplatform}}

with this:

{{#multiplatform}}
        @SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
        {{/multiplatform}}

By wrapping with double quotes " the values of the enum in the value constructor.

@Serializable
    enum class SoftwareTiers(val value: kotlin.String) {
        @SerialName(value = "200") CASISTICA_GUASTI("200"),
        @SerialName(value = "250") CASISTICA_GUASTI_EDIT("250"),
        @SerialName(value = "300") OFFICINA("300"),
        @SerialName(value = "310") ODL("310"),
        @SerialName(value = "350") WORKSHOP_NET("350"),
        @SerialName(value = "400") REVISIONI("400"),
        @SerialName(value = "401") REGISTRO_REVISIONI("401"),
        @SerialName(value = "701") KROMEDA("701"),
        @SerialName(value = "503") DATA_FULL("503"),
        @SerialName(value = "600") FATTURAZIONE_ITALIA("600"),
        @SerialName(value = "900") COMUNICAZIONI("900"),
        @SerialName(value = "410") SMART("410"),
        @SerialName(value = "800") YAP_LITE("800"),
        @SerialName(value = "8888") ADMIN("8888"),
        @SerialName(value = "9999") LANG("9999"),
        @SerialName(value = "1000") MAGAZZINO("1000"),
        @SerialName(value = "6666") MEZZI_SPECIALI("6666"),
        @SerialName(value = "7777") GOMME("7777"),
        @SerialName(value = "7778") CONTESTO_GOMME("7778"),
        @SerialName(value = "1100") CED("1100"),
        @SerialName(value = "5555") PRIMA_NOTA("5555"),
        @SerialName(value = "11111") E_TICKET("11111"),
        @SerialName(value = "21111") FCA("21111"),
        @SerialName(value = "22111") FIRMA("22111");
    }

But I'm not sure if my solution

{{^isString}}{{#isContainer}}"{{/isContainer}}{{/isString}}{{{value}}}{{^isString}}{{#isContainer}}"{{/isContainer}

addresses all the cases or if is semantically right.

It also seems that the above enum doesn't match the specifications, because I'd expect to see an Int value constructor, and not a String one, so a better solution should be to add an Int constructor and all should work fine.

I'm not a mustache template file expert, so I need help choosing the right solution in this case.

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 modules/openapi-generator/src/main/resources/kotlin-client/data_class.mustache and reproduce the reported multiplatform enum using the OpenAPI example with integer values and x-enum-varnames. Compare the generated Kotlin enum's value type and constructor arguments with the specification, then verify that the corrected template handles the relevant string, numeric, and container cases and produces compiling output.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.