Skip to content

Commit

Permalink
Fix explicit KSerializer's name & generation when used in container (#…
Browse files Browse the repository at this point in the history
…20093)

* [kotlin][client] properly reference the same custom serializer name

* [kotlin][client] properly use correct type when enum in container
  • Loading branch information
hrach authored Nov 14, 2024
1 parent d442fc9 commit 96adf2f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ import {{packageName}}.infrastructure.ITransformForStorage
*/
{{^multiplatform}}
{{#kotlinx_serialization}}
{{#serializableModel}}@KSerializable{{/serializableModel}}{{^serializableModel}}@Serializable{{#enumUnknownDefaultCase}}(with = {{classname}}Serializer::class){{/enumUnknownDefaultCase}}{{/serializableModel}}
{{#serializableModel}}@KSerializable{{/serializableModel}}{{^serializableModel}}@Serializable{{#enumUnknownDefaultCase}}(with = {{{nameInPascalCase}}}Serializer::class){{/enumUnknownDefaultCase}}{{/serializableModel}}
{{/kotlinx_serialization}}
{{#moshi}}
@JsonClass(generateAdapter = false)
Expand Down Expand Up @@ -147,16 +147,16 @@ import {{packageName}}.infrastructure.ITransformForStorage
{{#enumUnknownDefaultCase}}

internal object {{nameInPascalCase}}Serializer : KSerializer<{{nameInPascalCase}}> {
override val descriptor = {{{dataType}}}.serializer().descriptor
override val descriptor = {{^isContainer}}{{dataType}}{{/isContainer}}{{#isContainer}}kotlin.String{{/isContainer}}.serializer().descriptor

override fun deserialize(decoder: Decoder): {{nameInPascalCase}} {
val value = decoder.decodeSerializableValue({{{dataType}}}.serializer())
val value = decoder.decodeSerializableValue({{^isContainer}}{{dataType}}{{/isContainer}}{{#isContainer}}kotlin.String{{/isContainer}}.serializer())
return {{nameInPascalCase}}.values().firstOrNull { it.value == value }
?: {{nameInPascalCase}}.{{#allowableValues}}{{#enumVars}}{{#-last}}{{&name}}{{/-last}}{{/enumVars}}{{/allowableValues}}
}

override fun serialize(encoder: Encoder, value: {{nameInPascalCase}}) {
encoder.encodeSerializableValue({{{dataType}}}.serializer(), value.value)
encoder.encodeSerializableValue({{^isContainer}}{{dataType}}{{/isContainer}}{{#isContainer}}kotlin.String{{/isContainer}}.serializer(), value.value)
}
}
{{/enumUnknownDefaultCase}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ data class Order (
*
* Values: placed,approved,delivered,unknown_default_open_api
*/
@Serializable(with = OrderSerializer::class)
@Serializable(with = StatusSerializer::class)
enum class Status(val value: kotlin.String) {
@SerialName(value = "placed") placed("placed"),
@SerialName(value = "approved") approved("approved"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ data class Pet (
*
* Values: available,pending,sold,unknown_default_open_api
*/
@Serializable(with = PetSerializer::class)
@Serializable(with = StatusSerializer::class)
enum class Status(val value: kotlin.String) {
@SerialName(value = "available") available("available"),
@SerialName(value = "pending") pending("pending"),
Expand Down

0 comments on commit 96adf2f

Please sign in to comment.