Skip to content

Commit 5c367fc

Browse files
authored
Merge pull request #8827 from Frozenstep/Frozenstep-Kotlin-fixing-file-uploading
Frozenstep kotlin fixing file uploading
2 parents d9c4e01 + 46def07 commit 5c367fc

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

modules/swagger-codegen/src/main/resources/kotlin-client/api.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class {{classname}}(basePath: kotlin.String = "{{{basePath}}}") : ApiClient(base
2121
*/{{#returnType}}
2222
@Suppress("UNCHECKED_CAST"){{/returnType}}
2323
fun {{operationId}}({{#allParams}}{{paramName}}: {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) : {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}} {
24-
val localVariableBody: kotlin.Any? = {{#hasBodyParam}}{{#bodyParams}}{{paramName}}{{/bodyParams}}{{/hasBodyParam}}{{^hasBodyParam}}{{^hasFormParams}}null{{/hasFormParams}}{{#hasFormParams}}mapOf({{#formParams}}"{{{baseName}}}" to "${{paramName}}"{{#hasMore}}, {{/hasMore}}{{/formParams}}){{/hasFormParams}}{{/hasBodyParam}}
24+
val localVariableBody: kotlin.Any? = {{#hasBodyParam}}{{#bodyParams}}{{paramName}}{{/bodyParams}}{{/hasBodyParam}}{{^hasBodyParam}}{{^hasFormParams}}null{{/hasFormParams}}{{#hasFormParams}}mapOf({{#formParams}}"{{{baseName}}}" to {{paramName}}{{#hasMore}}, {{/hasMore}}{{/formParams}}){{/hasFormParams}}{{/hasBodyParam}}
2525
val localVariableQuery: MultiValueMap = {{^hasQueryParams}}mapOf(){{/hasQueryParams}}{{#hasQueryParams}}mapOf({{#queryParams}}"{{baseName}}" to {{#isContainer}}toMultiValue({{paramName}}.toList(), "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf("${{paramName}}"){{/isContainer}}{{#hasMore}}, {{/hasMore}}{{/queryParams}}){{/hasQueryParams}}
2626

2727
val contentHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf({{#hasFormParams}}"Content-Type" to "multipart/form-data"{{/hasFormParams}})

modules/swagger-codegen/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,22 @@ open class ApiClient(val baseUrl: String) {
3434
MediaType.parse(mediaType), content
3535
)
3636
} else if(mediaType == FormDataMediaType) {
37-
var builder = FormBody.Builder()
37+
val requestBodyBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
38+
3839
// content's type *must* be Map<String, Any>
3940
@Suppress("UNCHECKED_CAST")
40-
(content as Map<String,String>).forEach { key, value ->
41-
builder = builder.add(key, value)
41+
(content as Map<String,Any>).forEach { key, value ->
42+
if(value::class == File::class) {
43+
val file = value as File
44+
requestBodyBuilder.addFormDataPart(key, file.name, RequestBody.create(MediaType.parse("application/octet-stream"), file))
45+
} else {
46+
val stringValue = value as String
47+
requestBodyBuilder.addFormDataPart(key, stringValue)
48+
}
49+
TODO("Handle other types inside FormDataMediaType")
4250
}
43-
return builder.build()
51+
52+
return requestBodyBuilder.build()
4453
} else if(mediaType == JsonMediaType) {
4554
return RequestBody.create(
4655
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)

samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/apis/PetApi.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
253253
* @return void
254254
*/
255255
fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String, status: kotlin.String) : Unit {
256-
val localVariableBody: kotlin.Any? = mapOf("name" to "$name", "status" to "$status")
256+
val localVariableBody: kotlin.Any? = mapOf("name" to name, "status" to status)
257257
val localVariableQuery: MultiValueMap = mapOf()
258258

259259
val contentHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("Content-Type" to "multipart/form-data")
@@ -293,7 +293,7 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
293293
*/
294294
@Suppress("UNCHECKED_CAST")
295295
fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String, file: java.io.File) : ApiResponse {
296-
val localVariableBody: kotlin.Any? = mapOf("additionalMetadata" to "$additionalMetadata", "file" to "$file")
296+
val localVariableBody: kotlin.Any? = mapOf("additionalMetadata" to additionalMetadata, "file" to file)
297297
val localVariableQuery: MultiValueMap = mapOf()
298298

299299
val contentHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("Content-Type" to "multipart/form-data")

samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/infrastructure/ApiClient.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,22 @@ open class ApiClient(val baseUrl: String) {
3434
MediaType.parse(mediaType), content
3535
)
3636
} else if(mediaType == FormDataMediaType) {
37-
var builder = FormBody.Builder()
37+
val requestBodyBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
38+
3839
// content's type *must* be Map<String, Any>
3940
@Suppress("UNCHECKED_CAST")
40-
(content as Map<String,String>).forEach { key, value ->
41-
builder = builder.add(key, value)
41+
(content as Map<String,Any>).forEach { key, value ->
42+
if(value::class == File::class) {
43+
val file = value as File
44+
requestBodyBuilder.addFormDataPart(key, file.name, RequestBody.create(MediaType.parse("application/octet-stream"), file))
45+
} else {
46+
val stringValue = value as String
47+
requestBodyBuilder.addFormDataPart(key, stringValue)
48+
}
49+
TODO("Handle other types inside FormDataMediaType")
4250
}
43-
return builder.build()
51+
52+
return requestBodyBuilder.build()
4453
} else if(mediaType == JsonMediaType) {
4554
return RequestBody.create(
4655
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)

0 commit comments

Comments
 (0)