From c1eda61874e574bd2440be11853ed6b0d05cbf51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Grill?= Date: Tue, 3 Jul 2018 08:25:48 +0200 Subject: [PATCH] Fix some Kotlin formatting issues and make source more Kotlin like (#427) --- .../infrastructure/ApiClient.kt.mustache | 61 +++++++++---------- .../ApiInfrastructureResponse.kt.mustache | 30 ++++----- .../infrastructure/RequestConfig.kt.mustache | 9 +-- .../infrastructure/Serializer.kt.mustache | 6 +- .../client/infrastructure/ApiClient.kt | 61 +++++++++---------- .../ApiInfrastructureResponse.kt | 30 ++++----- .../client/infrastructure/RequestConfig.kt | 9 +-- .../client/infrastructure/Serializer.kt | 6 +- .../client/infrastructure/ApiClient.kt | 61 +++++++++---------- .../ApiInfrastructureResponse.kt | 30 ++++----- .../client/infrastructure/RequestConfig.kt | 9 +-- .../client/infrastructure/Serializer.kt | 6 +- .../client/infrastructure/ApiClient.kt | 61 +++++++++---------- .../ApiInfrastructureResponse.kt | 30 ++++----- .../client/infrastructure/RequestConfig.kt | 9 +-- .../client/infrastructure/Serializer.kt | 6 +- .../ktor/.openapi-generator/VERSION | 2 +- .../petstore/kotlin-server/ktor/README.md | 2 +- 18 files changed, 210 insertions(+), 218 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache index 934cf63524d0..8b7d4f3b665e 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache @@ -5,11 +5,11 @@ import java.io.File open class ApiClient(val baseUrl: String) { companion object { - protected val ContentType = "Content-Type" - protected val Accept = "Accept" - protected val JsonMediaType = "application/json" - protected val FormDataMediaType = "multipart/form-data" - protected val XmlMediaType = "application/xml" + protected const val ContentType = "Content-Type" + protected const val Accept = "Accept" + protected const val JsonMediaType = "application/json" + protected const val FormDataMediaType = "multipart/form-data" + protected const val XmlMediaType = "application/xml" @JvmStatic val client by lazy { @@ -26,32 +26,29 @@ open class ApiClient(val baseUrl: String) { val jsonHeaders: Map = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType) } - inline protected fun requestBody(content: T, mediaType: String = JsonMediaType): RequestBody { - if(content is File) { - return RequestBody.create( - MediaType.parse(mediaType), content - ) - } else if(mediaType == FormDataMediaType) { - var builder = FormBody.Builder() - // content's type *must* be Map - @Suppress("UNCHECKED_CAST") - (content as Map).forEach { key, value -> - builder = builder.add(key, value) - } - return builder.build() - } else if(mediaType == JsonMediaType) { - return RequestBody.create( - MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content) - ) - } else if (mediaType == XmlMediaType) { - TODO("xml not currently supported.") - } - - // TODO: this should be extended with other serializers - TODO("requestBody currently only supports JSON body and File body.") - } - - inline protected fun responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? { + protected inline fun requestBody(content: T, mediaType: String = JsonMediaType): RequestBody = + when { + content is File -> RequestBody.create( + MediaType.parse(mediaType), content + ) + mediaType == FormDataMediaType -> { + var builder = FormBody.Builder() + // content's type *must* be Map + @Suppress("UNCHECKED_CAST") + (content as Map).forEach { key, value -> + builder = builder.add(key, value) + } + builder.build() + } + mediaType == JsonMediaType -> RequestBody.create( + MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content) + ) + mediaType == XmlMediaType -> TODO("xml not currently supported.") + // TODO: this should be extended with other serializers + else -> TODO("requestBody currently only supports JSON body and File body.") + } + + protected inline fun responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? { if(body == null) return null return when(mediaType) { JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source()) @@ -59,7 +56,7 @@ open class ApiClient(val baseUrl: String) { } } - inline protected fun request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse { + protected inline fun request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse { val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.") var urlBuilder = httpUrl.newBuilder() diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiInfrastructureResponse.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiInfrastructureResponse.kt.mustache index a4e0f0f424d9..9b6bdad4ba5e 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiInfrastructureResponse.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiInfrastructureResponse.kt.mustache @@ -10,31 +10,31 @@ abstract class ApiInfrastructureResponse(val responseType: ResponseType) { } class Success( - val data: T, - override val statusCode: Int = -1, - override val headers: Map> = mapOf() + val data: T, + override val statusCode: Int = -1, + override val headers: Map> = mapOf() ): ApiInfrastructureResponse(ResponseType.Success) class Informational( - val statusText: String, - override val statusCode: Int = -1, - override val headers: Map> = mapOf() + val statusText: String, + override val statusCode: Int = -1, + override val headers: Map> = mapOf() ) : ApiInfrastructureResponse(ResponseType.Informational) class Redirection( - override val statusCode: Int = -1, - override val headers: Map> = mapOf() + override val statusCode: Int = -1, + override val headers: Map> = mapOf() ) : ApiInfrastructureResponse(ResponseType.Redirection) class ClientError( - val body: Any? = null, - override val statusCode: Int = -1, - override val headers: Map> = mapOf() + val body: Any? = null, + override val statusCode: Int = -1, + override val headers: Map> = mapOf() ) : ApiInfrastructureResponse(ResponseType.ClientError) class ServerError( - val message: String? = null, - val body: Any? = null, - override val statusCode: Int = -1, - override val headers: Map> + val message: String? = null, + val body: Any? = null, + override val statusCode: Int = -1, + override val headers: Map> ): ApiInfrastructureResponse(ResponseType.ServerError) \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/RequestConfig.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/RequestConfig.kt.mustache index 58a3d605aa11..5fc06ceb6122 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/RequestConfig.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/RequestConfig.kt.mustache @@ -9,7 +9,8 @@ package {{packageName}}.infrastructure * multi-valued headers as csv-only. */ data class RequestConfig( - val method: RequestMethod, - val path: String, - val headers: Map = mapOf(), - val query: Map> = mapOf()) \ No newline at end of file + val method: RequestMethod, + val path: String, + val headers: Map = mapOf(), + val query: Map> = mapOf() +) \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/Serializer.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/Serializer.kt.mustache index 71cb991ddba1..9d448cb9a738 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/Serializer.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/Serializer.kt.mustache @@ -8,7 +8,7 @@ import java.util.* object Serializer { @JvmStatic val moshi: Moshi = Moshi.Builder() - .add(KotlinJsonAdapterFactory()) - .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) - .build() + .add(KotlinJsonAdapterFactory()) + .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) + .build() } diff --git a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 5c4ebd2acbce..02c7b2cddc03 100644 --- a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -5,11 +5,11 @@ import java.io.File open class ApiClient(val baseUrl: String) { companion object { - protected val ContentType = "Content-Type" - protected val Accept = "Accept" - protected val JsonMediaType = "application/json" - protected val FormDataMediaType = "multipart/form-data" - protected val XmlMediaType = "application/xml" + protected const val ContentType = "Content-Type" + protected const val Accept = "Accept" + protected const val JsonMediaType = "application/json" + protected const val FormDataMediaType = "multipart/form-data" + protected const val XmlMediaType = "application/xml" @JvmStatic val client by lazy { @@ -26,32 +26,29 @@ open class ApiClient(val baseUrl: String) { val jsonHeaders: Map = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType) } - inline protected fun requestBody(content: T, mediaType: String = JsonMediaType): RequestBody { - if(content is File) { - return RequestBody.create( - MediaType.parse(mediaType), content - ) - } else if(mediaType == FormDataMediaType) { - var builder = FormBody.Builder() - // content's type *must* be Map - @Suppress("UNCHECKED_CAST") - (content as Map).forEach { key, value -> - builder = builder.add(key, value) - } - return builder.build() - } else if(mediaType == JsonMediaType) { - return RequestBody.create( - MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content) - ) - } else if (mediaType == XmlMediaType) { - TODO("xml not currently supported.") - } - - // TODO: this should be extended with other serializers - TODO("requestBody currently only supports JSON body and File body.") - } - - inline protected fun responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? { + protected inline fun requestBody(content: T, mediaType: String = JsonMediaType): RequestBody = + when { + content is File -> RequestBody.create( + MediaType.parse(mediaType), content + ) + mediaType == FormDataMediaType -> { + var builder = FormBody.Builder() + // content's type *must* be Map + @Suppress("UNCHECKED_CAST") + (content as Map).forEach { key, value -> + builder = builder.add(key, value) + } + builder.build() + } + mediaType == JsonMediaType -> RequestBody.create( + MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content) + ) + mediaType == XmlMediaType -> TODO("xml not currently supported.") + // TODO: this should be extended with other serializers + else -> TODO("requestBody currently only supports JSON body and File body.") + } + + protected inline fun responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? { if(body == null) return null return when(mediaType) { JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source()) @@ -59,7 +56,7 @@ open class ApiClient(val baseUrl: String) { } } - inline protected fun request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse { + protected inline fun request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse { val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.") var urlBuilder = httpUrl.newBuilder() diff --git a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiInfrastructureResponse.kt b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiInfrastructureResponse.kt index 1f5fbf1a3f0b..f1a8aecc914b 100644 --- a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiInfrastructureResponse.kt +++ b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiInfrastructureResponse.kt @@ -10,31 +10,31 @@ abstract class ApiInfrastructureResponse(val responseType: ResponseType) { } class Success( - val data: T, - override val statusCode: Int = -1, - override val headers: Map> = mapOf() + val data: T, + override val statusCode: Int = -1, + override val headers: Map> = mapOf() ): ApiInfrastructureResponse(ResponseType.Success) class Informational( - val statusText: String, - override val statusCode: Int = -1, - override val headers: Map> = mapOf() + val statusText: String, + override val statusCode: Int = -1, + override val headers: Map> = mapOf() ) : ApiInfrastructureResponse(ResponseType.Informational) class Redirection( - override val statusCode: Int = -1, - override val headers: Map> = mapOf() + override val statusCode: Int = -1, + override val headers: Map> = mapOf() ) : ApiInfrastructureResponse(ResponseType.Redirection) class ClientError( - val body: Any? = null, - override val statusCode: Int = -1, - override val headers: Map> = mapOf() + val body: Any? = null, + override val statusCode: Int = -1, + override val headers: Map> = mapOf() ) : ApiInfrastructureResponse(ResponseType.ClientError) class ServerError( - val message: String? = null, - val body: Any? = null, - override val statusCode: Int = -1, - override val headers: Map> + val message: String? = null, + val body: Any? = null, + override val statusCode: Int = -1, + override val headers: Map> ): ApiInfrastructureResponse(ResponseType.ServerError) \ No newline at end of file diff --git a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt index b01f63a1013a..86e2dadf9a81 100644 --- a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt +++ b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt @@ -9,7 +9,8 @@ package org.openapitools.client.infrastructure * multi-valued headers as csv-only. */ data class RequestConfig( - val method: RequestMethod, - val path: String, - val headers: Map = mapOf(), - val query: Map> = mapOf()) \ No newline at end of file + val method: RequestMethod, + val path: String, + val headers: Map = mapOf(), + val query: Map> = mapOf() +) \ No newline at end of file diff --git a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt index 24dfb79dab1d..cf3fe8203d5b 100644 --- a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt +++ b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt @@ -8,7 +8,7 @@ import java.util.* object Serializer { @JvmStatic val moshi: Moshi = Moshi.Builder() - .add(KotlinJsonAdapterFactory()) - .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) - .build() + .add(KotlinJsonAdapterFactory()) + .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) + .build() } diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 5c4ebd2acbce..02c7b2cddc03 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -5,11 +5,11 @@ import java.io.File open class ApiClient(val baseUrl: String) { companion object { - protected val ContentType = "Content-Type" - protected val Accept = "Accept" - protected val JsonMediaType = "application/json" - protected val FormDataMediaType = "multipart/form-data" - protected val XmlMediaType = "application/xml" + protected const val ContentType = "Content-Type" + protected const val Accept = "Accept" + protected const val JsonMediaType = "application/json" + protected const val FormDataMediaType = "multipart/form-data" + protected const val XmlMediaType = "application/xml" @JvmStatic val client by lazy { @@ -26,32 +26,29 @@ open class ApiClient(val baseUrl: String) { val jsonHeaders: Map = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType) } - inline protected fun requestBody(content: T, mediaType: String = JsonMediaType): RequestBody { - if(content is File) { - return RequestBody.create( - MediaType.parse(mediaType), content - ) - } else if(mediaType == FormDataMediaType) { - var builder = FormBody.Builder() - // content's type *must* be Map - @Suppress("UNCHECKED_CAST") - (content as Map).forEach { key, value -> - builder = builder.add(key, value) - } - return builder.build() - } else if(mediaType == JsonMediaType) { - return RequestBody.create( - MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content) - ) - } else if (mediaType == XmlMediaType) { - TODO("xml not currently supported.") - } - - // TODO: this should be extended with other serializers - TODO("requestBody currently only supports JSON body and File body.") - } - - inline protected fun responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? { + protected inline fun requestBody(content: T, mediaType: String = JsonMediaType): RequestBody = + when { + content is File -> RequestBody.create( + MediaType.parse(mediaType), content + ) + mediaType == FormDataMediaType -> { + var builder = FormBody.Builder() + // content's type *must* be Map + @Suppress("UNCHECKED_CAST") + (content as Map).forEach { key, value -> + builder = builder.add(key, value) + } + builder.build() + } + mediaType == JsonMediaType -> RequestBody.create( + MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content) + ) + mediaType == XmlMediaType -> TODO("xml not currently supported.") + // TODO: this should be extended with other serializers + else -> TODO("requestBody currently only supports JSON body and File body.") + } + + protected inline fun responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? { if(body == null) return null return when(mediaType) { JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source()) @@ -59,7 +56,7 @@ open class ApiClient(val baseUrl: String) { } } - inline protected fun request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse { + protected inline fun request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse { val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.") var urlBuilder = httpUrl.newBuilder() diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiInfrastructureResponse.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiInfrastructureResponse.kt index 1f5fbf1a3f0b..f1a8aecc914b 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiInfrastructureResponse.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiInfrastructureResponse.kt @@ -10,31 +10,31 @@ abstract class ApiInfrastructureResponse(val responseType: ResponseType) { } class Success( - val data: T, - override val statusCode: Int = -1, - override val headers: Map> = mapOf() + val data: T, + override val statusCode: Int = -1, + override val headers: Map> = mapOf() ): ApiInfrastructureResponse(ResponseType.Success) class Informational( - val statusText: String, - override val statusCode: Int = -1, - override val headers: Map> = mapOf() + val statusText: String, + override val statusCode: Int = -1, + override val headers: Map> = mapOf() ) : ApiInfrastructureResponse(ResponseType.Informational) class Redirection( - override val statusCode: Int = -1, - override val headers: Map> = mapOf() + override val statusCode: Int = -1, + override val headers: Map> = mapOf() ) : ApiInfrastructureResponse(ResponseType.Redirection) class ClientError( - val body: Any? = null, - override val statusCode: Int = -1, - override val headers: Map> = mapOf() + val body: Any? = null, + override val statusCode: Int = -1, + override val headers: Map> = mapOf() ) : ApiInfrastructureResponse(ResponseType.ClientError) class ServerError( - val message: String? = null, - val body: Any? = null, - override val statusCode: Int = -1, - override val headers: Map> + val message: String? = null, + val body: Any? = null, + override val statusCode: Int = -1, + override val headers: Map> ): ApiInfrastructureResponse(ResponseType.ServerError) \ No newline at end of file diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt index b01f63a1013a..86e2dadf9a81 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt @@ -9,7 +9,8 @@ package org.openapitools.client.infrastructure * multi-valued headers as csv-only. */ data class RequestConfig( - val method: RequestMethod, - val path: String, - val headers: Map = mapOf(), - val query: Map> = mapOf()) \ No newline at end of file + val method: RequestMethod, + val path: String, + val headers: Map = mapOf(), + val query: Map> = mapOf() +) \ No newline at end of file diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt index 24dfb79dab1d..cf3fe8203d5b 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt @@ -8,7 +8,7 @@ import java.util.* object Serializer { @JvmStatic val moshi: Moshi = Moshi.Builder() - .add(KotlinJsonAdapterFactory()) - .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) - .build() + .add(KotlinJsonAdapterFactory()) + .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) + .build() } diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 5c4ebd2acbce..02c7b2cddc03 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -5,11 +5,11 @@ import java.io.File open class ApiClient(val baseUrl: String) { companion object { - protected val ContentType = "Content-Type" - protected val Accept = "Accept" - protected val JsonMediaType = "application/json" - protected val FormDataMediaType = "multipart/form-data" - protected val XmlMediaType = "application/xml" + protected const val ContentType = "Content-Type" + protected const val Accept = "Accept" + protected const val JsonMediaType = "application/json" + protected const val FormDataMediaType = "multipart/form-data" + protected const val XmlMediaType = "application/xml" @JvmStatic val client by lazy { @@ -26,32 +26,29 @@ open class ApiClient(val baseUrl: String) { val jsonHeaders: Map = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType) } - inline protected fun requestBody(content: T, mediaType: String = JsonMediaType): RequestBody { - if(content is File) { - return RequestBody.create( - MediaType.parse(mediaType), content - ) - } else if(mediaType == FormDataMediaType) { - var builder = FormBody.Builder() - // content's type *must* be Map - @Suppress("UNCHECKED_CAST") - (content as Map).forEach { key, value -> - builder = builder.add(key, value) - } - return builder.build() - } else if(mediaType == JsonMediaType) { - return RequestBody.create( - MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content) - ) - } else if (mediaType == XmlMediaType) { - TODO("xml not currently supported.") - } - - // TODO: this should be extended with other serializers - TODO("requestBody currently only supports JSON body and File body.") - } - - inline protected fun responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? { + protected inline fun requestBody(content: T, mediaType: String = JsonMediaType): RequestBody = + when { + content is File -> RequestBody.create( + MediaType.parse(mediaType), content + ) + mediaType == FormDataMediaType -> { + var builder = FormBody.Builder() + // content's type *must* be Map + @Suppress("UNCHECKED_CAST") + (content as Map).forEach { key, value -> + builder = builder.add(key, value) + } + builder.build() + } + mediaType == JsonMediaType -> RequestBody.create( + MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content) + ) + mediaType == XmlMediaType -> TODO("xml not currently supported.") + // TODO: this should be extended with other serializers + else -> TODO("requestBody currently only supports JSON body and File body.") + } + + protected inline fun responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? { if(body == null) return null return when(mediaType) { JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source()) @@ -59,7 +56,7 @@ open class ApiClient(val baseUrl: String) { } } - inline protected fun request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse { + protected inline fun request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse { val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.") var urlBuilder = httpUrl.newBuilder() diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiInfrastructureResponse.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiInfrastructureResponse.kt index 1f5fbf1a3f0b..f1a8aecc914b 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiInfrastructureResponse.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiInfrastructureResponse.kt @@ -10,31 +10,31 @@ abstract class ApiInfrastructureResponse(val responseType: ResponseType) { } class Success( - val data: T, - override val statusCode: Int = -1, - override val headers: Map> = mapOf() + val data: T, + override val statusCode: Int = -1, + override val headers: Map> = mapOf() ): ApiInfrastructureResponse(ResponseType.Success) class Informational( - val statusText: String, - override val statusCode: Int = -1, - override val headers: Map> = mapOf() + val statusText: String, + override val statusCode: Int = -1, + override val headers: Map> = mapOf() ) : ApiInfrastructureResponse(ResponseType.Informational) class Redirection( - override val statusCode: Int = -1, - override val headers: Map> = mapOf() + override val statusCode: Int = -1, + override val headers: Map> = mapOf() ) : ApiInfrastructureResponse(ResponseType.Redirection) class ClientError( - val body: Any? = null, - override val statusCode: Int = -1, - override val headers: Map> = mapOf() + val body: Any? = null, + override val statusCode: Int = -1, + override val headers: Map> = mapOf() ) : ApiInfrastructureResponse(ResponseType.ClientError) class ServerError( - val message: String? = null, - val body: Any? = null, - override val statusCode: Int = -1, - override val headers: Map> + val message: String? = null, + val body: Any? = null, + override val statusCode: Int = -1, + override val headers: Map> ): ApiInfrastructureResponse(ResponseType.ServerError) \ No newline at end of file diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt index b01f63a1013a..86e2dadf9a81 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt @@ -9,7 +9,8 @@ package org.openapitools.client.infrastructure * multi-valued headers as csv-only. */ data class RequestConfig( - val method: RequestMethod, - val path: String, - val headers: Map = mapOf(), - val query: Map> = mapOf()) \ No newline at end of file + val method: RequestMethod, + val path: String, + val headers: Map = mapOf(), + val query: Map> = mapOf() +) \ No newline at end of file diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt index 24dfb79dab1d..cf3fe8203d5b 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt @@ -8,7 +8,7 @@ import java.util.* object Serializer { @JvmStatic val moshi: Moshi = Moshi.Builder() - .add(KotlinJsonAdapterFactory()) - .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) - .build() + .add(KotlinJsonAdapterFactory()) + .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) + .build() } diff --git a/samples/server/petstore/kotlin-server/ktor/.openapi-generator/VERSION b/samples/server/petstore/kotlin-server/ktor/.openapi-generator/VERSION index 1c00c5181548..0628777500bd 100644 --- a/samples/server/petstore/kotlin-server/ktor/.openapi-generator/VERSION +++ b/samples/server/petstore/kotlin-server/ktor/.openapi-generator/VERSION @@ -1 +1 @@ -3.0.2-SNAPSHOT \ No newline at end of file +3.1.0-SNAPSHOT \ No newline at end of file diff --git a/samples/server/petstore/kotlin-server/ktor/README.md b/samples/server/petstore/kotlin-server/ktor/README.md index a2d2c5e11ab8..82c3596aa35a 100644 --- a/samples/server/petstore/kotlin-server/ktor/README.md +++ b/samples/server/petstore/kotlin-server/ktor/README.md @@ -2,7 +2,7 @@ This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. -Generated by OpenAPI Generator 3.0.2-SNAPSHOT. +Generated by OpenAPI Generator 3.1.0-SNAPSHOT. ## Requires