Skip to content

Commit 46def07

Browse files
Astakhov, AndrewAstakhov, Andrew
authored andcommitted
[Kotlin] Fixing file Uploading
1 parent 2e0b730 commit 46def07

File tree

21 files changed

+126
-43
lines changed

21 files changed

+126
-43
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/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This runs all tests and packages the library.
3131
<a name="documentation-for-api-endpoints"></a>
3232
## Documentation for API Endpoints
3333

34-
All URIs are relative to *https://petstore.swagger.io/v2*
34+
All URIs are relative to *http://petstore.swagger.io/v2*
3535

3636
Class | Method | HTTP request | Description
3737
------------ | ------------- | ------------- | -------------
@@ -60,8 +60,10 @@ Class | Method | HTTP request | Description
6060
<a name="documentation-for-models"></a>
6161
## Documentation for Models
6262

63+
- [io.swagger.client.models.Amount](docs/Amount.md)
6364
- [io.swagger.client.models.ApiResponse](docs/ApiResponse.md)
6465
- [io.swagger.client.models.Category](docs/Category.md)
66+
- [io.swagger.client.models.Currency](docs/Currency.md)
6567
- [io.swagger.client.models.Order](docs/Order.md)
6668
- [io.swagger.client.models.Pet](docs/Pet.md)
6769
- [io.swagger.client.models.Tag](docs/Tag.md)
@@ -83,7 +85,7 @@ Class | Method | HTTP request | Description
8385

8486
- **Type**: OAuth
8587
- **Flow**: implicit
86-
- **Authorization URL**: https://petstore.swagger.io/oauth/dialog
88+
- **Authorization URL**: http://petstore.swagger.io/api/oauth/dialog
8789
- **Scopes**:
8890
- write:pets: modify pets in your account
8991
- read:pets: read your pets
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
# Amount
3+
4+
## Properties
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**value** | **kotlin.Double** | some description |
8+
**currency** | [**Currency**](Currency.md) | |
9+
10+
11+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
# Currency
3+
4+
## Properties
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
8+
9+

samples/client/petstore/kotlin/docs/PetApi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# PetApi
22

3-
All URIs are relative to *https://petstore.swagger.io/v2*
3+
All URIs are relative to *http://petstore.swagger.io/v2*
44

55
Method | HTTP request | Description
66
------------- | ------------- | -------------
@@ -161,7 +161,7 @@ Name | Type | Description | Notes
161161
162162
Finds Pets by tags
163163

164-
Muliple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
164+
Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
165165

166166
### Example
167167
```kotlin

samples/client/petstore/kotlin/docs/StoreApi.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# StoreApi
22

3-
All URIs are relative to *https://petstore.swagger.io/v2*
3+
All URIs are relative to *http://petstore.swagger.io/v2*
44

55
Method | HTTP request | Description
66
------------- | ------------- | -------------
@@ -16,7 +16,7 @@ Method | HTTP request | Description
1616
1717
Delete purchase order by ID
1818

19-
For valid response try integer IDs with positive integer value. Negative or non-integer values will generate API errors
19+
For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors
2020

2121
### Example
2222
```kotlin
@@ -25,7 +25,7 @@ For valid response try integer IDs with positive integer value. Negative or non-
2525
//import io.swagger.client.models.*
2626

2727
val apiInstance = StoreApi()
28-
val orderId : kotlin.Long = 789 // kotlin.Long | ID of the order that needs to be deleted
28+
val orderId : kotlin.String = orderId_example // kotlin.String | ID of the order that needs to be deleted
2929
try {
3030
apiInstance.deleteOrder(orderId)
3131
} catch (e: ClientException) {
@@ -41,7 +41,7 @@ try {
4141

4242
Name | Type | Description | Notes
4343
------------- | ------------- | ------------- | -------------
44-
**orderId** | **kotlin.Long**| ID of the order that needs to be deleted |
44+
**orderId** | **kotlin.String**| ID of the order that needs to be deleted |
4545

4646
### Return type
4747

@@ -105,7 +105,7 @@ This endpoint does not need any parameter.
105105
106106
Find purchase order by ID
107107

108-
For valid response try integer IDs with value &gt;&#x3D; 1 and &lt;&#x3D; 10. Other values will generated exceptions
108+
For valid response try integer IDs with value &lt;&#x3D; 5 or &gt; 10. Other values will generated exceptions
109109

110110
### Example
111111
```kotlin

samples/client/petstore/kotlin/docs/UserApi.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# UserApi
22

3-
All URIs are relative to *https://petstore.swagger.io/v2*
3+
All URIs are relative to *http://petstore.swagger.io/v2*
44

55
Method | HTTP request | Description
66
------------- | ------------- | -------------
@@ -213,7 +213,7 @@ Get user by user name
213213
//import io.swagger.client.models.*
214214

215215
val apiInstance = UserApi()
216-
val username : kotlin.String = username_example // kotlin.String | The name that needs to be fetched. Use user1 for testing.
216+
val username : kotlin.String = username_example // kotlin.String | The name that needs to be fetched. Use user1 for testing.
217217
try {
218218
val result : User = apiInstance.getUserByName(username)
219219
println(result)
@@ -230,7 +230,7 @@ try {
230230

231231
Name | Type | Description | Notes
232232
------------- | ------------- | ------------- | -------------
233-
**username** | **kotlin.String**| The name that needs to be fetched. Use user1 for testing. |
233+
**username** | **kotlin.String**| The name that needs to be fetched. Use user1 for testing. |
234234

235235
### Return type
236236

@@ -351,7 +351,7 @@ This can only be done by the logged in user.
351351
//import io.swagger.client.models.*
352352

353353
val apiInstance = UserApi()
354-
val username : kotlin.String = username_example // kotlin.String | name that need to be updated
354+
val username : kotlin.String = username_example // kotlin.String | name that need to be deleted
355355
val body : User = // User | Updated user object
356356
try {
357357
apiInstance.updateUser(username, body)
@@ -368,7 +368,7 @@ try {
368368

369369
Name | Type | Description | Notes
370370
------------- | ------------- | ------------- | -------------
371-
**username** | **kotlin.String**| name that need to be updated |
371+
**username** | **kotlin.String**| name that need to be deleted |
372372
**body** | [**User**](User.md)| Updated user object |
373373

374374
### Return type
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rootProject.name = 'kotlin-client'
1+
rootProject.name = 'kotlin-petstore-client'

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import io.swagger.client.models.Pet
1616

1717
import io.swagger.client.infrastructure.*
1818

19-
class PetApi(basePath: kotlin.String = "https://petstore.swagger.io/v2") : ApiClient(basePath) {
19+
class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiClient(basePath) {
2020

2121
/**
2222
* Add a new pet to the store
@@ -102,7 +102,7 @@ class PetApi(basePath: kotlin.String = "https://petstore.swagger.io/v2") : ApiCl
102102
@Suppress("UNCHECKED_CAST")
103103
fun findPetsByStatus(status: kotlin.Array<kotlin.String>) : kotlin.Array<Pet> {
104104
val localVariableBody: kotlin.Any? = null
105-
val localVariableQuery: MultiValueMap = mapOf("status" to toMultiValue(status.toList(), "multi"))
105+
val localVariableQuery: MultiValueMap = mapOf("status" to toMultiValue(status.toList(), "csv"))
106106

107107
val contentHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf()
108108
val acceptsHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("Accept" to "application/xml, application/json")
@@ -133,14 +133,14 @@ class PetApi(basePath: kotlin.String = "https://petstore.swagger.io/v2") : ApiCl
133133

134134
/**
135135
* Finds Pets by tags
136-
* Muliple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
136+
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
137137
* @param tags Tags to filter by
138138
* @return kotlin.Array<Pet>
139139
*/
140140
@Suppress("UNCHECKED_CAST")
141141
fun findPetsByTags(tags: kotlin.Array<kotlin.String>) : kotlin.Array<Pet> {
142142
val localVariableBody: kotlin.Any? = null
143-
val localVariableQuery: MultiValueMap = mapOf("tags" to toMultiValue(tags.toList(), "multi"))
143+
val localVariableQuery: MultiValueMap = mapOf("tags" to toMultiValue(tags.toList(), "csv"))
144144

145145
val contentHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf()
146146
val acceptsHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("Accept" to "application/xml, application/json")
@@ -253,7 +253,7 @@ class PetApi(basePath: kotlin.String = "https://petstore.swagger.io/v2") : ApiCl
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 = "https://petstore.swagger.io/v2") : ApiCl
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")

0 commit comments

Comments
 (0)