From aace459217adb57e5f9a835e5ce04d251f113e3e Mon Sep 17 00:00:00 2001 From: Alexander Navratil Date: Mon, 18 Feb 2019 11:46:44 +0100 Subject: [PATCH] [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 --- .../codegen/languages/KotlinClientCodegen.java | 1 + .../infrastructure/ApiClient.kt.mustache | 4 +++- .../infrastructure/ByteArrayAdapter.kt.mustache | 12 ++++++++++++ .../openapitools/client/infrastructure/ApiClient.kt | 4 +++- .../client/infrastructure/ByteArrayAdapter.kt | 12 ++++++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ByteArrayAdapter.kt.mustache create mode 100644 samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ByteArrayAdapter.kt diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java index 85ac703ef54..2795ad22bb3 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java @@ -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")); } } diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache index a7d4ae11f5c..4b6a2adc4b2 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache @@ -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() } } diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ByteArrayAdapter.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ByteArrayAdapter.kt.mustache new file mode 100644 index 00000000000..404f093eb65 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ByteArrayAdapter.kt.mustache @@ -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() +} \ No newline at end of file diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index fa6f15fbd05..9e79028a90b 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -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() } } diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ByteArrayAdapter.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ByteArrayAdapter.kt new file mode 100644 index 00000000000..617ac3fe906 --- /dev/null +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ByteArrayAdapter.kt @@ -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() +} \ No newline at end of file