From ff2e173de8c68c304fa1cc1162a09aef44a34899 Mon Sep 17 00:00:00 2001 From: Jacek Czerski Date: Wed, 3 Jul 2024 10:57:46 +0200 Subject: [PATCH] [Kotlin-Client][JVM-OkHttp] Override parameter enum's toString() method to use its value (#19053) When using the JVM implementation of OkHttp with a Kotlin client, you may encounter issues with the toString() method of enum parameters. By default, the toString() method of an enum returns the name of the enum value, not its value. To fix this issue, you can override the toString() method of your enum to return its value instead of its name. --- .../libraries/jvm-okhttp/api.mustache | 31 ++++++++++------- .../openapitools/client/apis/DefaultApi.kt | 33 +++++++++++++++++-- .../org/openapitools/client/apis/PetApi.kt | 11 ++++++- .../org/openapitools/client/apis/PetApi.kt | 10 ++++++ .../org/openapitools/client/apis/PetApi.kt | 11 ++++++- .../org/openapitools/client/apis/PetApi.kt | 11 ++++++- .../org/openapitools/client/apis/PetApi.kt | 11 ++++++- .../org/openapitools/client/apis/PetApi.kt | 11 ++++++- .../org/openapitools/client/apis/PetApi.kt | 11 ++++++- .../org/openapitools/client/apis/PetApi.kt | 11 ++++++- .../org/openapitools/client/apis/PetApi.kt | 11 ++++++- .../org/openapitools/client/apis/PetApi.kt | 11 ++++++- .../org/openapitools/client/apis/PetApi.kt | 11 ++++++- 13 files changed, 159 insertions(+), 25 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/api.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/api.mustache index 6aa28579d28..9dd4f1664c4 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/api.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/api.mustache @@ -68,20 +68,20 @@ import {{packageName}}.infrastructure.toMultiValue {{#enumVars}} {{^multiplatform}} {{#moshi}} - @Json(name = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}} + @Json(name = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} {{/moshi}} {{#gson}} - @SerializedName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}} + @SerializedName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} {{/gson}} {{#jackson}} - @JsonProperty(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}} + @JsonProperty(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} {{/jackson}} {{#kotlinx_serialization}} - @SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}} + @SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} {{/kotlinx_serialization}} {{/multiplatform}} {{#multiplatform}} - @SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}} + @SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} {{/multiplatform}} {{/enumVars}} {{/allowableValues}} @@ -89,28 +89,35 @@ import {{packageName}}.infrastructure.toMultiValue {{#enumUnknownDefaultCase}} {{#allowableValues}} {{#enumVars}} - {{^-last}} {{^multiplatform}} {{#moshi}} - @Json(name = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}), + @Json(name = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} {{/moshi}} {{#gson}} - @SerializedName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}), + @SerializedName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} {{/gson}} {{#jackson}} - @JsonProperty(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}), + @JsonProperty(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} {{/jackson}} {{#kotlinx_serialization}} - @SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}), + @SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} {{/kotlinx_serialization}} {{/multiplatform}} {{#multiplatform}} - @SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}), + @SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} {{/multiplatform}} - {{/-last}} {{/enumVars}} {{/allowableValues}} {{/enumUnknownDefaultCase}} + + /** + * Override [toString()] to avoid using the enum variable name as the value, and instead use + * the actual value defined in the API spec file. + * + * This solves a problem when the variable name and its value are different, and ensures that + * the client sends the correct enum values to the server always. + */ + override fun toString(): kotlin.String = "$value" } {{/isEnum}} diff --git a/samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt b/samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt index 138966eedaf..54863fe75c6 100644 --- a/samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt +++ b/samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt @@ -50,7 +50,16 @@ class DefaultApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient enum class QueryDefaultEnumFindPetsByStatus(val value: kotlin.String) { @Json(name = "A") A("A"), @Json(name = "B") B("B"), - @Json(name = "C") C("C") + @Json(name = "C") C("C"); + + /** + * Override [toString()] to avoid using the enum variable name as the value, and instead use + * the actual value defined in the API spec file. + * + * This solves a problem when the variable name and its value are different, and ensures that + * the client sends the correct enum values to the server always. + */ + override fun toString(): kotlin.String = "$value" } /** @@ -59,7 +68,16 @@ class DefaultApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient enum class HeaderDefaultEnumFindPetsByStatus(val value: kotlin.String) { @Json(name = "A") A("A"), @Json(name = "B") B("B"), - @Json(name = "C") C("C") + @Json(name = "C") C("C"); + + /** + * Override [toString()] to avoid using the enum variable name as the value, and instead use + * the actual value defined in the API spec file. + * + * This solves a problem when the variable name and its value are different, and ensures that + * the client sends the correct enum values to the server always. + */ + override fun toString(): kotlin.String = "$value" } /** @@ -68,7 +86,16 @@ class DefaultApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient enum class CookieDefaultEnumFindPetsByStatus(val value: kotlin.String) { @Json(name = "A") A("A"), @Json(name = "B") B("B"), - @Json(name = "C") C("C") + @Json(name = "C") C("C"); + + /** + * Override [toString()] to avoid using the enum variable name as the value, and instead use + * the actual value defined in the API spec file. + * + * This solves a problem when the variable name and its value are different, and ensures that + * the client sends the correct enum values to the server always. + */ + override fun toString(): kotlin.String = "$value" } /** diff --git a/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index af0f359940b..7fb0c39b94f 100644 --- a/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -193,7 +193,16 @@ class PetApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient = A enum class StatusFindPetsByStatus(val value: kotlin.String) { @SerializedName(value = "available") available("available"), @SerializedName(value = "pending") pending("pending"), - @SerializedName(value = "sold") sold("sold") + @SerializedName(value = "sold") sold("sold"); + + /** + * Override [toString()] to avoid using the enum variable name as the value, and instead use + * the actual value defined in the API spec file. + * + * This solves a problem when the variable name and its value are different, and ensures that + * the client sends the correct enum values to the server always. + */ + override fun toString(): kotlin.String = "$value" } /** diff --git a/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index bcafbbef17c..9f2ac885ebf 100644 --- a/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -194,6 +194,16 @@ class PetApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient = A @JsonProperty(value = "available") AVAILABLE("available"), @JsonProperty(value = "pending") PENDING("pending"), @JsonProperty(value = "sold") SOLD("sold"), + @JsonProperty(value = "unknown_default_open_api") UNKNOWN_DEFAULT_OPEN_API("unknown_default_open_api"); + + /** + * Override [toString()] to avoid using the enum variable name as the value, and instead use + * the actual value defined in the API spec file. + * + * This solves a problem when the variable name and its value are different, and ensures that + * the client sends the correct enum values to the server always. + */ + override fun toString(): kotlin.String = "$value" } /** diff --git a/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index ac031cb2d6a..5ddf39f4321 100644 --- a/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -195,7 +195,16 @@ class PetApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient = A enum class StatusFindPetsByStatus(val value: kotlin.String) { @SerializedName(value = "available") available("available"), @SerializedName(value = "pending") pending("pending"), - @SerializedName(value = "sold") sold("sold") + @SerializedName(value = "sold") sold("sold"); + + /** + * Override [toString()] to avoid using the enum variable name as the value, and instead use + * the actual value defined in the API spec file. + * + * This solves a problem when the variable name and its value are different, and ensures that + * the client sends the correct enum values to the server always. + */ + override fun toString(): kotlin.String = "$value" } /** diff --git a/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 3263d52ffeb..d4ec2ae6e4c 100644 --- a/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -193,7 +193,16 @@ class PetApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient = A enum class StatusFindPetsByStatus(val value: kotlin.String) { @Json(name = "available") available("available"), @Json(name = "pending") pending("pending"), - @Json(name = "sold") sold("sold") + @Json(name = "sold") sold("sold"); + + /** + * Override [toString()] to avoid using the enum variable name as the value, and instead use + * the actual value defined in the API spec file. + * + * This solves a problem when the variable name and its value are different, and ensures that + * the client sends the correct enum values to the server always. + */ + override fun toString(): kotlin.String = "$value" } /** diff --git a/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index d1e8467efe5..69bcd7ae233 100644 --- a/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -193,7 +193,16 @@ class PetApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient = A enum class StatusFindPetsByStatus(val value: kotlin.String) { @Json(name = "available") available("available"), @Json(name = "pending") pending("pending"), - @Json(name = "sold") sold("sold") + @Json(name = "sold") sold("sold"); + + /** + * Override [toString()] to avoid using the enum variable name as the value, and instead use + * the actual value defined in the API spec file. + * + * This solves a problem when the variable name and its value are different, and ensures that + * the client sends the correct enum values to the server always. + */ + override fun toString(): kotlin.String = "$value" } /** diff --git a/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 3263d52ffeb..d4ec2ae6e4c 100644 --- a/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -193,7 +193,16 @@ class PetApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient = A enum class StatusFindPetsByStatus(val value: kotlin.String) { @Json(name = "available") available("available"), @Json(name = "pending") pending("pending"), - @Json(name = "sold") sold("sold") + @Json(name = "sold") sold("sold"); + + /** + * Override [toString()] to avoid using the enum variable name as the value, and instead use + * the actual value defined in the API spec file. + * + * This solves a problem when the variable name and its value are different, and ensures that + * the client sends the correct enum values to the server always. + */ + override fun toString(): kotlin.String = "$value" } /** diff --git a/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 2dbf87ecb0f..b391779f68e 100644 --- a/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -193,7 +193,16 @@ internal class PetApi(basePath: kotlin.String = defaultBasePath, client: OkHttpC internal enum class StatusFindPetsByStatus(val value: kotlin.String) { @Json(name = "available") available("available"), @Json(name = "pending") pending("pending"), - @Json(name = "sold") sold("sold") + @Json(name = "sold") sold("sold"); + + /** + * Override [toString()] to avoid using the enum variable name as the value, and instead use + * the actual value defined in the API spec file. + * + * This solves a problem when the variable name and its value are different, and ensures that + * the client sends the correct enum values to the server always. + */ + override fun toString(): kotlin.String = "$value" } /** diff --git a/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 7a748692370..cf5beeb71f8 100644 --- a/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -193,7 +193,16 @@ class PetApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient = A enum class StatusFindPetsByStatus(val value: kotlin.String) { @Json(name = "available") available("available"), @Json(name = "pending") pending("pending"), - @Json(name = "sold") sold("sold") + @Json(name = "sold") sold("sold"); + + /** + * Override [toString()] to avoid using the enum variable name as the value, and instead use + * the actual value defined in the API spec file. + * + * This solves a problem when the variable name and its value are different, and ensures that + * the client sends the correct enum values to the server always. + */ + override fun toString(): kotlin.String = "$value" } /** diff --git a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 44f24344ce7..601b71b143d 100644 --- a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -193,7 +193,16 @@ class PetApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient = A enum class StatusFindPetsByStatus(val value: kotlin.String) { @Json(name = "available") available("available"), @Json(name = "pending") pending("pending"), - @Json(name = "sold") sold("sold") + @Json(name = "sold") sold("sold"); + + /** + * Override [toString()] to avoid using the enum variable name as the value, and instead use + * the actual value defined in the API spec file. + * + * This solves a problem when the variable name and its value are different, and ensures that + * the client sends the correct enum values to the server always. + */ + override fun toString(): kotlin.String = "$value" } /** diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 3263d52ffeb..d4ec2ae6e4c 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -193,7 +193,16 @@ class PetApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient = A enum class StatusFindPetsByStatus(val value: kotlin.String) { @Json(name = "available") available("available"), @Json(name = "pending") pending("pending"), - @Json(name = "sold") sold("sold") + @Json(name = "sold") sold("sold"); + + /** + * Override [toString()] to avoid using the enum variable name as the value, and instead use + * the actual value defined in the API spec file. + * + * This solves a problem when the variable name and its value are different, and ensures that + * the client sends the correct enum values to the server always. + */ + override fun toString(): kotlin.String = "$value" } /** diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 3263d52ffeb..d4ec2ae6e4c 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -193,7 +193,16 @@ class PetApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient = A enum class StatusFindPetsByStatus(val value: kotlin.String) { @Json(name = "available") available("available"), @Json(name = "pending") pending("pending"), - @Json(name = "sold") sold("sold") + @Json(name = "sold") sold("sold"); + + /** + * Override [toString()] to avoid using the enum variable name as the value, and instead use + * the actual value defined in the API spec file. + * + * This solves a problem when the variable name and its value are different, and ensures that + * the client sends the correct enum values to the server always. + */ + override fun toString(): kotlin.String = "$value" } /**