Skip to content

Commit

Permalink
[Kotlin-Client][JVM-OkHttp] Override parameter enum's toString() meth…
Browse files Browse the repository at this point in the history
…od 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.
  • Loading branch information
jczerski authored Jul 3, 2024
1 parent 03beb37 commit ff2e173
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,49 +68,56 @@ 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}}
{{/enumUnknownDefaultCase}}
{{#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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

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

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

/**
Expand Down

0 comments on commit ff2e173

Please sign in to comment.