[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.
This commit is contained in:
Jacek Czerski 2024-07-03 10:57:46 +02:00 committed by GitHub
parent 03beb3732e
commit ff2e173de8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 159 additions and 25 deletions

View File

@ -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}}

View File

@ -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"
}
/**

View File

@ -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"
}
/**

View File

@ -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"
}
/**

View File

@ -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"
}
/**

View File

@ -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"
}
/**

View File

@ -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"
}
/**

View File

@ -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"
}
/**

View File

@ -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"
}
/**

View File

@ -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"
}
/**

View File

@ -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"
}
/**

View File

@ -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"
}
/**

View File

@ -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"
}
/**