From a7f2791783325568698c4a9e82abea0aa8a8320c Mon Sep 17 00:00:00 2001 From: Alexander Eggers Date: Wed, 19 Feb 2020 13:30:13 +1100 Subject: [PATCH] [Kotlin][Client] Enum toString handling (#5327) * Added toString to enum_class script This toString avoids using the enum var name and uses the enum's value instead. This will fix cases when enum var name and value are quite different. * Updated enum template Co-Authored-By: Jim Schubert Co-authored-by: Jim Schubert --- .../src/main/resources/kotlin-client/enum_class.mustache | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/enum_class.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/enum_class.mustache index 73113186c51..947f34ea901 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/enum_class.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/enum_class.mustache @@ -41,6 +41,14 @@ import kotlinx.serialization.internal.CommonEnumSerializer {{/enumVars}}{{/allowableValues}} + /** + This override toString avoids using the enum var name and uses the actual api value instead. + In cases the var name and value are different, the client would send incorrect enums to the server. + **/ + override fun toString(): String { + return value{{^isString}}.toString(){{/isString}} + } + {{#multiplatform}} {{#nonPublicApi}}internal {{/nonPublicApi}}object Serializer : CommonEnumSerializer<{{classname}}>("{{classname}}", values(), values().map { it.value.toString() }.toTypedArray()) {{/multiplatform}}