Skip to content

Commit 72b110f

Browse files
committed
[kotlin][client] update samples
1 parent 63996d3 commit 72b110f

File tree

86 files changed

+785
-625
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+785
-625
lines changed

samples/client/petstore/kotlin-array-simple-string/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ src/main/kotlin/org/openapitools/client/infrastructure/Errors.kt
1717
src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt
1818
src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt
1919
src/main/kotlin/org/openapitools/client/infrastructure/OffsetDateTimeAdapter.kt
20+
src/main/kotlin/org/openapitools/client/infrastructure/PartConfig.kt
2021
src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt
2122
src/main/kotlin/org/openapitools/client/infrastructure/RequestMethod.kt
2223
src/main/kotlin/org/openapitools/client/infrastructure/ResponseExtensions.kt

samples/client/petstore/kotlin-array-simple-string/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import org.openapitools.client.infrastructure.ClientError
3232
import org.openapitools.client.infrastructure.ServerException
3333
import org.openapitools.client.infrastructure.ServerError
3434
import org.openapitools.client.infrastructure.MultiValueMap
35+
import org.openapitools.client.infrastructure.PartConfig
3536
import org.openapitools.client.infrastructure.RequestConfig
3637
import org.openapitools.client.infrastructure.RequestMethod
3738
import org.openapitools.client.infrastructure.ResponseType

samples/client/petstore/kotlin-array-simple-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import okhttp3.ResponseBody
1010
import okhttp3.MediaType.Companion.toMediaTypeOrNull
1111
import okhttp3.Request
1212
import okhttp3.Headers
13+
import okhttp3.Headers.Companion.toHeaders
1314
import okhttp3.MultipartBody
1415
import okhttp3.Call
1516
import okhttp3.Callback
@@ -65,53 +66,46 @@ open class ApiClient(val baseUrl: String) {
6566
return contentType ?: "application/octet-stream"
6667
}
6768

68-
protected inline fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
69+
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
6970
when {
70-
content is File -> content.asRequestBody(mediaType.toMediaTypeOrNull())
71-
mediaType == FormDataMediaType -> {
71+
mediaType == FormDataMediaType ->
7272
MultipartBody.Builder()
7373
.setType(MultipartBody.FORM)
7474
.apply {
75-
// content's type *must* be Map<String, Any?>
75+
// content's type *must* be Map<String, PartConfig<*>>
7676
@Suppress("UNCHECKED_CAST")
77-
(content as Map<String, Any?>).forEach { (key, value) ->
78-
if (value is File) {
79-
val partHeaders = Headers.headersOf(
80-
"Content-Disposition",
81-
"form-data; name=\"$key\"; filename=\"${value.name}\""
82-
)
83-
val fileMediaType = guessContentTypeFromFile(value).toMediaTypeOrNull()
84-
addPart(partHeaders, value.asRequestBody(fileMediaType))
85-
} else {
86-
val partHeaders = Headers.headersOf(
87-
"Content-Disposition",
88-
"form-data; name=\"$key\""
89-
)
90-
addPart(
91-
partHeaders,
92-
parameterToString(value).toRequestBody(null)
93-
)
77+
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
78+
val contentType = part.headers.remove("Content-Type")
79+
val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body)
80+
bodies.forEach { body ->
81+
val headers = part.headers.toMutableMap() +
82+
("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "")
83+
addPart(headers.toHeaders(),
84+
requestSingleBody(body, contentType))
9485
}
9586
}
9687
}.build()
97-
}
88+
else -> requestSingleBody(content, mediaType)
89+
}
90+
91+
protected inline fun <reified T> requestSingleBody(content: T, mediaType: String?): RequestBody =
92+
when {
93+
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
9894
mediaType == FormUrlEncMediaType -> {
9995
FormBody.Builder().apply {
100-
// content's type *must* be Map<String, Any?>
96+
// content's type *must* be Map<String, PartConfig<*>>
10197
@Suppress("UNCHECKED_CAST")
102-
(content as Map<String, Any?>).forEach { (key, value) ->
103-
add(key, parameterToString(value))
98+
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
99+
add(name, parameterToString(part.body))
104100
}
105101
}.build()
106102
}
107-
mediaType.startsWith("application/") && mediaType.endsWith("json") ->
103+
mediaType == null || mediaType.startsWith("application/") && mediaType.endsWith("json") ->
108104
if (content == null) {
109105
EMPTY_REQUEST
110106
} else {
111107
Serializer.moshi.adapter(T::class.java).toJson(content)
112-
.toRequestBody(
113-
mediaType.toMediaTypeOrNull()
114-
)
108+
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
115109
}
116110
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
117111
// TODO: this should be extended with other serializers
@@ -123,22 +117,20 @@ open class ApiClient(val baseUrl: String) {
123117
if(body == null) {
124118
return null
125119
}
126-
val bodyContent = body.string()
127-
if (bodyContent.isEmpty()) {
128-
return null
129-
}
130120
if (T::class.java == File::class.java) {
131121
// return tempfile
132122
// Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options
133123
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
134124
f.deleteOnExit()
135-
val out = BufferedWriter(FileWriter(f))
136-
out.write(bodyContent)
137-
out.close()
125+
body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) }
138126
return f as T
139127
}
128+
val bodyContent = body.string()
129+
if (bodyContent.isEmpty()) {
130+
return null
131+
}
140132
return when {
141-
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
133+
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
142134
Serializer.moshi.adapter<T>().fromJson(bodyContent)
143135
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
144136
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.openapitools.client.infrastructure
2+
3+
/**
4+
* Defines a config object for a given part of a multi-part request.
5+
* NOTE: Headers is a Map<String,String> because rfc2616 defines
6+
* multi-valued headers as csv-only.
7+
*/
8+
data class PartConfig<T>(
9+
val headers: MutableMap<String, String> = mutableMapOf(),
10+
val body: T? = null
11+
)

samples/client/petstore/kotlin-enum-default-value/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ src/main/kotlin/org/openapitools/client/infrastructure/Errors.kt
1818
src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt
1919
src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt
2020
src/main/kotlin/org/openapitools/client/infrastructure/OffsetDateTimeAdapter.kt
21+
src/main/kotlin/org/openapitools/client/infrastructure/PartConfig.kt
2122
src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt
2223
src/main/kotlin/org/openapitools/client/infrastructure/RequestMethod.kt
2324
src/main/kotlin/org/openapitools/client/infrastructure/ResponseExtensions.kt

samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import org.openapitools.client.infrastructure.ClientError
3333
import org.openapitools.client.infrastructure.ServerException
3434
import org.openapitools.client.infrastructure.ServerError
3535
import org.openapitools.client.infrastructure.MultiValueMap
36+
import org.openapitools.client.infrastructure.PartConfig
3637
import org.openapitools.client.infrastructure.RequestConfig
3738
import org.openapitools.client.infrastructure.RequestMethod
3839
import org.openapitools.client.infrastructure.ResponseType

samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import okhttp3.ResponseBody
1010
import okhttp3.MediaType.Companion.toMediaTypeOrNull
1111
import okhttp3.Request
1212
import okhttp3.Headers
13+
import okhttp3.Headers.Companion.toHeaders
1314
import okhttp3.MultipartBody
1415
import okhttp3.Call
1516
import okhttp3.Callback
@@ -65,53 +66,46 @@ open class ApiClient(val baseUrl: String) {
6566
return contentType ?: "application/octet-stream"
6667
}
6768

68-
protected inline fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
69+
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
6970
when {
70-
content is File -> content.asRequestBody(mediaType.toMediaTypeOrNull())
71-
mediaType == FormDataMediaType -> {
71+
mediaType == FormDataMediaType ->
7272
MultipartBody.Builder()
7373
.setType(MultipartBody.FORM)
7474
.apply {
75-
// content's type *must* be Map<String, Any?>
75+
// content's type *must* be Map<String, PartConfig<*>>
7676
@Suppress("UNCHECKED_CAST")
77-
(content as Map<String, Any?>).forEach { (key, value) ->
78-
if (value is File) {
79-
val partHeaders = Headers.headersOf(
80-
"Content-Disposition",
81-
"form-data; name=\"$key\"; filename=\"${value.name}\""
82-
)
83-
val fileMediaType = guessContentTypeFromFile(value).toMediaTypeOrNull()
84-
addPart(partHeaders, value.asRequestBody(fileMediaType))
85-
} else {
86-
val partHeaders = Headers.headersOf(
87-
"Content-Disposition",
88-
"form-data; name=\"$key\""
89-
)
90-
addPart(
91-
partHeaders,
92-
parameterToString(value).toRequestBody(null)
93-
)
77+
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
78+
val contentType = part.headers.remove("Content-Type")
79+
val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body)
80+
bodies.forEach { body ->
81+
val headers = part.headers.toMutableMap() +
82+
("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "")
83+
addPart(headers.toHeaders(),
84+
requestSingleBody(body, contentType))
9485
}
9586
}
9687
}.build()
97-
}
88+
else -> requestSingleBody(content, mediaType)
89+
}
90+
91+
protected inline fun <reified T> requestSingleBody(content: T, mediaType: String?): RequestBody =
92+
when {
93+
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
9894
mediaType == FormUrlEncMediaType -> {
9995
FormBody.Builder().apply {
100-
// content's type *must* be Map<String, Any?>
96+
// content's type *must* be Map<String, PartConfig<*>>
10197
@Suppress("UNCHECKED_CAST")
102-
(content as Map<String, Any?>).forEach { (key, value) ->
103-
add(key, parameterToString(value))
98+
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
99+
add(name, parameterToString(part.body))
104100
}
105101
}.build()
106102
}
107-
mediaType.startsWith("application/") && mediaType.endsWith("json") ->
103+
mediaType == null || mediaType.startsWith("application/") && mediaType.endsWith("json") ->
108104
if (content == null) {
109105
EMPTY_REQUEST
110106
} else {
111107
Serializer.moshi.adapter(T::class.java).toJson(content)
112-
.toRequestBody(
113-
mediaType.toMediaTypeOrNull()
114-
)
108+
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
115109
}
116110
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
117111
// TODO: this should be extended with other serializers
@@ -123,22 +117,20 @@ open class ApiClient(val baseUrl: String) {
123117
if(body == null) {
124118
return null
125119
}
126-
val bodyContent = body.string()
127-
if (bodyContent.isEmpty()) {
128-
return null
129-
}
130120
if (T::class.java == File::class.java) {
131121
// return tempfile
132122
// Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options
133123
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
134124
f.deleteOnExit()
135-
val out = BufferedWriter(FileWriter(f))
136-
out.write(bodyContent)
137-
out.close()
125+
body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) }
138126
return f as T
139127
}
128+
val bodyContent = body.string()
129+
if (bodyContent.isEmpty()) {
130+
return null
131+
}
140132
return when {
141-
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
133+
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
142134
Serializer.moshi.adapter<T>().fromJson(bodyContent)
143135
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
144136
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.openapitools.client.infrastructure
2+
3+
/**
4+
* Defines a config object for a given part of a multi-part request.
5+
* NOTE: Headers is a Map<String,String> because rfc2616 defines
6+
* multi-valued headers as csv-only.
7+
*/
8+
data class PartConfig<T>(
9+
val headers: MutableMap<String, String> = mutableMapOf(),
10+
val body: T? = null
11+
)

samples/client/petstore/kotlin-gson/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ src/main/kotlin/org/openapitools/client/infrastructure/Errors.kt
2525
src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt
2626
src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt
2727
src/main/kotlin/org/openapitools/client/infrastructure/OffsetDateTimeAdapter.kt
28+
src/main/kotlin/org/openapitools/client/infrastructure/PartConfig.kt
2829
src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt
2930
src/main/kotlin/org/openapitools/client/infrastructure/RequestMethod.kt
3031
src/main/kotlin/org/openapitools/client/infrastructure/ResponseExtensions.kt

samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import org.openapitools.client.infrastructure.ClientError
3434
import org.openapitools.client.infrastructure.ServerException
3535
import org.openapitools.client.infrastructure.ServerError
3636
import org.openapitools.client.infrastructure.MultiValueMap
37+
import org.openapitools.client.infrastructure.PartConfig
3738
import org.openapitools.client.infrastructure.RequestConfig
3839
import org.openapitools.client.infrastructure.RequestMethod
3940
import org.openapitools.client.infrastructure.ResponseType
@@ -532,7 +533,7 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
532533
fun updatePetWithFormWithHttpInfo(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?) : ApiResponse<Unit?> {
533534
val localVariableConfig = updatePetWithFormRequestConfig(petId = petId, name = name, status = status)
534535

535-
return request<Map<String, Any?>, Unit>(
536+
return request<Map<String, PartConfig<*>>, Unit>(
536537
localVariableConfig
537538
)
538539
}
@@ -545,8 +546,10 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
545546
* @param status Updated status of the pet (optional)
546547
* @return RequestConfig
547548
*/
548-
fun updatePetWithFormRequestConfig(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?) : RequestConfig<Map<String, Any?>> {
549-
val localVariableBody = mapOf("name" to name, "status" to status)
549+
fun updatePetWithFormRequestConfig(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?) : RequestConfig<Map<String, PartConfig<*>>> {
550+
val localVariableBody = mapOf(
551+
"name" to PartConfig(body = name, headers = mutableMapOf()),
552+
"status" to PartConfig(body = status, headers = mutableMapOf()),)
550553
val localVariableQuery: MultiValueMap = mutableMapOf()
551554
val localVariableHeaders: MutableMap<String, String> = mutableMapOf("Content-Type" to "application/x-www-form-urlencoded")
552555

@@ -607,7 +610,7 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
607610
fun uploadFileWithHttpInfo(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: java.io.File?) : ApiResponse<ModelApiResponse?> {
608611
val localVariableConfig = uploadFileRequestConfig(petId = petId, additionalMetadata = additionalMetadata, file = file)
609612

610-
return request<Map<String, Any?>, ModelApiResponse>(
613+
return request<Map<String, PartConfig<*>>, ModelApiResponse>(
611614
localVariableConfig
612615
)
613616
}
@@ -620,8 +623,10 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
620623
* @param file file to upload (optional)
621624
* @return RequestConfig
622625
*/
623-
fun uploadFileRequestConfig(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: java.io.File?) : RequestConfig<Map<String, Any?>> {
624-
val localVariableBody = mapOf("additionalMetadata" to additionalMetadata, "file" to file)
626+
fun uploadFileRequestConfig(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: java.io.File?) : RequestConfig<Map<String, PartConfig<*>>> {
627+
val localVariableBody = mapOf(
628+
"additionalMetadata" to PartConfig(body = additionalMetadata, headers = mutableMapOf()),
629+
"file" to PartConfig(body = file, headers = mutableMapOf()),)
625630
val localVariableQuery: MultiValueMap = mutableMapOf()
626631
val localVariableHeaders: MutableMap<String, String> = mutableMapOf("Content-Type" to "multipart/form-data")
627632
localVariableHeaders["Accept"] = "application/json"

0 commit comments

Comments
 (0)