[kotlin][client] bytearray conversion (#2166)

* use kotlin.String for ByteArray fields (type: string, format: byte)

* revert "use kotlin.String for ByteArray fields (type: string, format: byte)"

* add ByteArrayAdapter for string <-> ByteArray conversion with moshi
This commit is contained in:
Alexander Navratil 2019-02-18 11:46:44 +01:00 committed by William Cheng
parent b4b8c28b2a
commit aace459217
5 changed files with 31 additions and 2 deletions

View File

@ -164,5 +164,6 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
supportingFiles.add(new SupportingFile("infrastructure/ResponseExtensions.kt.mustache", infrastructureFolder, "ResponseExtensions.kt"));
supportingFiles.add(new SupportingFile("infrastructure/Serializer.kt.mustache", infrastructureFolder, "Serializer.kt"));
supportingFiles.add(new SupportingFile("infrastructure/Errors.kt.mustache", infrastructureFolder, "Errors.kt"));
supportingFiles.add(new SupportingFile("infrastructure/ByteArrayAdapter.kt.mustache", infrastructureFolder, "ByteArrayAdapter.kt"));
}
}

View File

@ -60,7 +60,9 @@ open class ApiClient(val baseUrl: String) {
fun toJson(uuid: UUID) = uuid.toString()
@FromJson
fun fromJson(s: String) = UUID.fromString(s)
}).build().adapter(T::class.java).fromJson(body.source())
})
.add(ByteArrayAdapter())
.build().adapter(T::class.java).fromJson(body.source())
else -> TODO()
}
}

View File

@ -0,0 +1,12 @@
package {{packageName}}.infrastructure
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
class ByteArrayAdapter {
@ToJson
fun toJson(data: ByteArray): String = String(data)
@FromJson
fun fromJson(data: String): ByteArray = data.toByteArray()
}

View File

@ -60,7 +60,9 @@ open class ApiClient(val baseUrl: String) {
fun toJson(uuid: UUID) = uuid.toString()
@FromJson
fun fromJson(s: String) = UUID.fromString(s)
}).build().adapter(T::class.java).fromJson(body.source())
})
.add(ByteArrayAdapter())
.build().adapter(T::class.java).fromJson(body.source())
else -> TODO()
}
}

View File

@ -0,0 +1,12 @@
package org.openapitools.client.infrastructure
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
class ByteArrayAdapter {
@ToJson
fun toJson(data: ByteArray): String = String(data)
@FromJson
fun fromJson(data: String): ByteArray = data.toByteArray()
}