Skip to content

Commit 268727a

Browse files
ackintoshjimschubert
authored andcommitted
Fix type inference error (OpenAPITools#1773)
* Fix type inference error * Update samples bin/openapi3/kotlin-client-petstore.sh bin/kotlin-client-petstore.sh * Update samples
1 parent 7b9f4bf commit 268727a

File tree

7 files changed

+17
-8
lines changed
  • modules/openapi-generator/src/main/resources/kotlin-client
  • samples
    • client/petstore
      • kotlin-string/src/main/kotlin/org/openapitools/client/apis
      • kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis
      • kotlin/src/main/kotlin/org/openapitools/client/apis
    • openapi3/client/petstore/kotlin/src/main/kotlin/org/openapitools/client

7 files changed

+17
-8
lines changed

modules/openapi-generator/src/main/resources/kotlin-client/api.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class {{classname}}(basePath: kotlin.String = "{{{basePath}}}") : ApiClient(base
2323
fun {{operationId}}({{#allParams}}{{paramName}}: {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) : {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}} {
2424
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}}
26-
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf({{#hasFormParams}}"Content-Type" to "multipart/form-data"{{/hasFormParams}}{{^hasHeaderParams}}){{/hasHeaderParams}}{{#hasHeaderParams}}{{#hasFormParams}}, {{/hasFormParams}}{{#headerParams}}"{{baseName}}" to {{#isContainer}}{{paramName}}.joinToString(separator = collectionDelimiter("{{collectionFormat}}")){{/isContainer}}{{^isContainer}}{{paramName}}{{/isContainer}}{{#hasMore}}, {{/hasMore}}{{/headerParams}}){{/hasHeaderParams}}
26+
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf({{#hasFormParams}}"Content-Type" to "multipart/form-data"{{/hasFormParams}}{{^hasHeaderParams}}){{/hasHeaderParams}}{{#hasHeaderParams}}{{#hasFormParams}}, {{/hasFormParams}}{{#headerParams}}"{{baseName}}" to {{#isContainer}}{{paramName}}.joinToString(separator = collectionDelimiter("{{collectionFormat}}")){{/isContainer}}{{^isContainer}}{{paramName}}.toString(){{/isContainer}}{{#hasMore}}, {{/hasMore}}{{/headerParams}}){{/hasHeaderParams}}
2727
val localVariableConfig = RequestConfig(
2828
RequestMethod.{{httpMethod}},
2929
"{{path}}"{{#pathParams}}.replace("{"+"{{baseName}}"+"}", "${{paramName}}"){{/pathParams}},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
5959
fun deletePet(petId: kotlin.Long, apiKey: kotlin.String) : Unit {
6060
val localVariableBody: kotlin.Any? = null
6161
val localVariableQuery: MultiValueMap = mapOf()
62-
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("api_key" to apiKey)
62+
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("api_key" to apiKey.toString())
6363
val localVariableConfig = RequestConfig(
6464
RequestMethod.DELETE,
6565
"/pet/{petId}".replace("{"+"petId"+"}", "$petId"),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
6060
fun deletePet(petId: kotlin.Long, apiKey: kotlin.String) : Unit {
6161
val localVariableBody: kotlin.Any? = null
6262
val localVariableQuery: MultiValueMap = mapOf()
63-
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("api_key" to apiKey)
63+
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("api_key" to apiKey.toString())
6464
val localVariableConfig = RequestConfig(
6565
RequestMethod.DELETE,
6666
"/pet/{petId}".replace("{"+"petId"+"}", "$petId"),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
5959
fun deletePet(petId: kotlin.Long, apiKey: kotlin.String) : Unit {
6060
val localVariableBody: kotlin.Any? = null
6161
val localVariableQuery: MultiValueMap = mapOf()
62-
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("api_key" to apiKey)
62+
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("api_key" to apiKey.toString())
6363
val localVariableConfig = RequestConfig(
6464
RequestMethod.DELETE,
6565
"/pet/{petId}".replace("{"+"petId"+"}", "$petId"),

samples/openapi3/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/FakeApi.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ class FakeApi(basePath: kotlin.String = "http://petstore.swagger.io:80/v2") : Ap
303303
fun testEnumParameters(enumHeaderStringArray: kotlin.Array<kotlin.String>, enumHeaderString: kotlin.String, enumQueryStringArray: kotlin.Array<kotlin.String>, enumQueryString: kotlin.String, enumQueryInteger: kotlin.Int, enumQueryDouble: kotlin.Double, enumFormStringArray: kotlin.Array<kotlin.String>, enumFormString: kotlin.String) : Unit {
304304
val localVariableBody: kotlin.Any? = mapOf("enum_form_string_array" to "$enumFormStringArray", "enum_form_string" to "$enumFormString")
305305
val localVariableQuery: MultiValueMap = mapOf("enum_query_string_array" to toMultiValue(enumQueryStringArray.toList(), "multi"), "enum_query_string" to listOf("$enumQueryString"), "enum_query_integer" to listOf("$enumQueryInteger"), "enum_query_double" to listOf("$enumQueryDouble"))
306-
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("Content-Type" to "multipart/form-data", "enum_header_string_array" to enumHeaderStringArray.joinToString(separator = collectionDelimiter("csv")), "enum_header_string" to enumHeaderString)
306+
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("Content-Type" to "multipart/form-data", "enum_header_string_array" to enumHeaderStringArray.joinToString(separator = collectionDelimiter("csv")), "enum_header_string" to enumHeaderString.toString())
307307
val localVariableConfig = RequestConfig(
308308
RequestMethod.GET,
309309
"/fake",
@@ -339,7 +339,7 @@ class FakeApi(basePath: kotlin.String = "http://petstore.swagger.io:80/v2") : Ap
339339
fun testGroupParameters(requiredStringGroup: kotlin.Int, requiredBooleanGroup: kotlin.Boolean, requiredInt64Group: kotlin.Long, stringGroup: kotlin.Int, booleanGroup: kotlin.Boolean, int64Group: kotlin.Long) : Unit {
340340
val localVariableBody: kotlin.Any? = null
341341
val localVariableQuery: MultiValueMap = mapOf("required_string_group" to listOf("$requiredStringGroup"), "required_int64_group" to listOf("$requiredInt64Group"), "string_group" to listOf("$stringGroup"), "int64_group" to listOf("$int64Group"))
342-
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("required_boolean_group" to requiredBooleanGroup, "boolean_group" to booleanGroup)
342+
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("required_boolean_group" to requiredBooleanGroup.toString(), "boolean_group" to booleanGroup.toString())
343343
val localVariableConfig = RequestConfig(
344344
RequestMethod.DELETE,
345345
"/fake",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io:80/v2") : Api
5959
fun deletePet(petId: kotlin.Long, apiKey: kotlin.String) : Unit {
6060
val localVariableBody: kotlin.Any? = null
6161
val localVariableQuery: MultiValueMap = mapOf()
62-
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("api_key" to apiKey)
62+
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("api_key" to apiKey.toString())
6363
val localVariableConfig = RequestConfig(
6464
RequestMethod.DELETE,
6565
"/pet/{petId}".replace("{"+"petId"+"}", "$petId"),

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package org.openapitools.client.infrastructure
22

3+
import com.squareup.moshi.FromJson
4+
import com.squareup.moshi.Moshi
5+
import com.squareup.moshi.ToJson
36
import okhttp3.*
47
import java.io.File
8+
import java.util.*
59

610
open class ApiClient(val baseUrl: String) {
711
companion object {
@@ -51,7 +55,12 @@ open class ApiClient(val baseUrl: String) {
5155
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
5256
if(body == null) return null
5357
return when(mediaType) {
54-
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source())
58+
JsonMediaType -> Moshi.Builder().add(object {
59+
@ToJson
60+
fun toJson(uuid: UUID) = uuid.toString()
61+
@FromJson
62+
fun fromJson(s: String) = UUID.fromString(s)
63+
}).build().adapter(T::class.java).fromJson(body.source())
5564
else -> TODO()
5665
}
5766
}

0 commit comments

Comments
 (0)