Skip to content

Commit 3a2e0d2

Browse files
committed
Add custom type adapter to deserialize UUID
1 parent f183153 commit 3a2e0d2

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package {{packageName}}.infrastructure
22

3+
import com.squareup.moshi.FromJson
4+
import com.squareup.moshi.Moshi
5+
import com.squareup.moshi.ToJson
36
import okhttp3.*
47
import java.io.File
8+
import java.util.*
59

610
open class ApiClient(val baseUrl: String) {
711
companion object {
@@ -51,7 +55,12 @@ open class ApiClient(val baseUrl: String) {
5155
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
5256
if(body == null) return null
5357
return when(mediaType) {
54-
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source())
58+
JsonMediaType -> Moshi.Builder().add(object {
59+
@ToJson
60+
fun toJson(uuid: UUID) = uuid.toString()
61+
@FromJson
62+
fun fromJson(s: String) = UUID.fromString(s)
63+
}).build().adapter(T::class.java).fromJson(body.source())
5564
else -> TODO()
5665
}
5766
}

0 commit comments

Comments
 (0)