Skip to content

Commit 8defce0

Browse files
alexnavratilwing328
authored andcommitted
[kotlin][client] bytearray conversion (OpenAPITools#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
1 parent c14ca64 commit 8defce0

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,6 @@ public void processOpts() {
164164
supportingFiles.add(new SupportingFile("infrastructure/ResponseExtensions.kt.mustache", infrastructureFolder, "ResponseExtensions.kt"));
165165
supportingFiles.add(new SupportingFile("infrastructure/Serializer.kt.mustache", infrastructureFolder, "Serializer.kt"));
166166
supportingFiles.add(new SupportingFile("infrastructure/Errors.kt.mustache", infrastructureFolder, "Errors.kt"));
167+
supportingFiles.add(new SupportingFile("infrastructure/ByteArrayAdapter.kt.mustache", infrastructureFolder, "ByteArrayAdapter.kt"));
167168
}
168169
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ open class ApiClient(val baseUrl: String) {
6060
fun toJson(uuid: UUID) = uuid.toString()
6161
@FromJson
6262
fun fromJson(s: String) = UUID.fromString(s)
63-
}).build().adapter(T::class.java).fromJson(body.source())
63+
})
64+
.add(ByteArrayAdapter())
65+
.build().adapter(T::class.java).fromJson(body.source())
6466
else -> TODO()
6567
}
6668
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package {{packageName}}.infrastructure
2+
3+
import com.squareup.moshi.FromJson
4+
import com.squareup.moshi.ToJson
5+
6+
class ByteArrayAdapter {
7+
@ToJson
8+
fun toJson(data: ByteArray): String = String(data)
9+
10+
@FromJson
11+
fun fromJson(data: String): ByteArray = data.toByteArray()
12+
}

samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ open class ApiClient(val baseUrl: String) {
6060
fun toJson(uuid: UUID) = uuid.toString()
6161
@FromJson
6262
fun fromJson(s: String) = UUID.fromString(s)
63-
}).build().adapter(T::class.java).fromJson(body.source())
63+
})
64+
.add(ByteArrayAdapter())
65+
.build().adapter(T::class.java).fromJson(body.source())
6466
else -> TODO()
6567
}
6668
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.openapitools.client.infrastructure
2+
3+
import com.squareup.moshi.FromJson
4+
import com.squareup.moshi.ToJson
5+
6+
class ByteArrayAdapter {
7+
@ToJson
8+
fun toJson(data: ByteArray): String = String(data)
9+
10+
@FromJson
11+
fun fromJson(data: String): ByteArray = data.toByteArray()
12+
}

0 commit comments

Comments
 (0)