Skip to content

Commit c1eda61

Browse files
guenhterjmini
authored andcommitted
Fix some Kotlin formatting issues and make source more Kotlin like (#427)
1 parent bece8d2 commit c1eda61

File tree

18 files changed

+210
-218
lines changed

18 files changed

+210
-218
lines changed

modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import java.io.File
55

66
open class ApiClient(val baseUrl: String) {
77
companion object {
8-
protected val ContentType = "Content-Type"
9-
protected val Accept = "Accept"
10-
protected val JsonMediaType = "application/json"
11-
protected val FormDataMediaType = "multipart/form-data"
12-
protected val XmlMediaType = "application/xml"
8+
protected const val ContentType = "Content-Type"
9+
protected const val Accept = "Accept"
10+
protected const val JsonMediaType = "application/json"
11+
protected const val FormDataMediaType = "multipart/form-data"
12+
protected const val XmlMediaType = "application/xml"
1313
1414
@JvmStatic
1515
val client by lazy {
@@ -26,40 +26,37 @@ open class ApiClient(val baseUrl: String) {
2626
val jsonHeaders: Map<String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType)
2727
}
2828

29-
inline protected fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody {
30-
if(content is File) {
31-
return RequestBody.create(
32-
MediaType.parse(mediaType), content
33-
)
34-
} else if(mediaType == FormDataMediaType) {
35-
var builder = FormBody.Builder()
36-
// content's type *must* be Map<String, Any>
37-
@Suppress("UNCHECKED_CAST")
38-
(content as Map<String,String>).forEach { key, value ->
39-
builder = builder.add(key, value)
40-
}
41-
return builder.build()
42-
} else if(mediaType == JsonMediaType) {
43-
return RequestBody.create(
44-
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
45-
)
46-
} else if (mediaType == XmlMediaType) {
47-
TODO("xml not currently supported.")
48-
}
49-
50-
// TODO: this should be extended with other serializers
51-
TODO("requestBody currently only supports JSON body and File body.")
52-
}
53-
54-
inline protected fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
29+
protected inline fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
30+
when {
31+
content is File -> RequestBody.create(
32+
MediaType.parse(mediaType), content
33+
)
34+
mediaType == FormDataMediaType -> {
35+
var builder = FormBody.Builder()
36+
// content's type *must* be Map<String, Any>
37+
@Suppress("UNCHECKED_CAST")
38+
(content as Map<String,String>).forEach { key, value ->
39+
builder = builder.add(key, value)
40+
}
41+
builder.build()
42+
}
43+
mediaType == JsonMediaType -> RequestBody.create(
44+
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
45+
)
46+
mediaType == XmlMediaType -> TODO("xml not currently supported.")
47+
// TODO: this should be extended with other serializers
48+
else -> TODO("requestBody currently only supports JSON body and File body.")
49+
}
50+
51+
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
5552
if(body == null) return null
5653
return when(mediaType) {
5754
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source())
5855
else -> TODO()
5956
}
6057
}
6158
62-
inline protected fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
59+
protected inline fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
6360
val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.")
6461
6562
var urlBuilder = httpUrl.newBuilder()

modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiInfrastructureResponse.kt.mustache

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,31 @@ abstract class ApiInfrastructureResponse<T>(val responseType: ResponseType) {
1010
}
1111

1212
class Success<T>(
13-
val data: T,
14-
override val statusCode: Int = -1,
15-
override val headers: Map<String, List<String>> = mapOf()
13+
val data: T,
14+
override val statusCode: Int = -1,
15+
override val headers: Map<String, List<String>> = mapOf()
1616
): ApiInfrastructureResponse<T>(ResponseType.Success)
1717

1818
class Informational<T>(
19-
val statusText: String,
20-
override val statusCode: Int = -1,
21-
override val headers: Map<String, List<String>> = mapOf()
19+
val statusText: String,
20+
override val statusCode: Int = -1,
21+
override val headers: Map<String, List<String>> = mapOf()
2222
) : ApiInfrastructureResponse<T>(ResponseType.Informational)
2323

2424
class Redirection<T>(
25-
override val statusCode: Int = -1,
26-
override val headers: Map<String, List<String>> = mapOf()
25+
override val statusCode: Int = -1,
26+
override val headers: Map<String, List<String>> = mapOf()
2727
) : ApiInfrastructureResponse<T>(ResponseType.Redirection)
2828

2929
class ClientError<T>(
30-
val body: Any? = null,
31-
override val statusCode: Int = -1,
32-
override val headers: Map<String, List<String>> = mapOf()
30+
val body: Any? = null,
31+
override val statusCode: Int = -1,
32+
override val headers: Map<String, List<String>> = mapOf()
3333
) : ApiInfrastructureResponse<T>(ResponseType.ClientError)
3434

3535
class ServerError<T>(
36-
val message: String? = null,
37-
val body: Any? = null,
38-
override val statusCode: Int = -1,
39-
override val headers: Map<String, List<String>>
36+
val message: String? = null,
37+
val body: Any? = null,
38+
override val statusCode: Int = -1,
39+
override val headers: Map<String, List<String>>
4040
): ApiInfrastructureResponse<T>(ResponseType.ServerError)

modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/RequestConfig.kt.mustache

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ package {{packageName}}.infrastructure
99
* multi-valued headers as csv-only.
1010
*/
1111
data class RequestConfig(
12-
val method: RequestMethod,
13-
val path: String,
14-
val headers: Map<String, String> = mapOf(),
15-
val query: Map<String, List<String>> = mapOf())
12+
val method: RequestMethod,
13+
val path: String,
14+
val headers: Map<String, String> = mapOf(),
15+
val query: Map<String, List<String>> = mapOf()
16+
)

modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/Serializer.kt.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import java.util.*
88
object Serializer {
99
@JvmStatic
1010
val moshi: Moshi = Moshi.Builder()
11-
.add(KotlinJsonAdapterFactory())
12-
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
13-
.build()
11+
.add(KotlinJsonAdapterFactory())
12+
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
13+
.build()
1414
}

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

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import java.io.File
55

66
open class ApiClient(val baseUrl: String) {
77
companion object {
8-
protected val ContentType = "Content-Type"
9-
protected val Accept = "Accept"
10-
protected val JsonMediaType = "application/json"
11-
protected val FormDataMediaType = "multipart/form-data"
12-
protected val XmlMediaType = "application/xml"
8+
protected const val ContentType = "Content-Type"
9+
protected const val Accept = "Accept"
10+
protected const val JsonMediaType = "application/json"
11+
protected const val FormDataMediaType = "multipart/form-data"
12+
protected const val XmlMediaType = "application/xml"
1313

1414
@JvmStatic
1515
val client by lazy {
@@ -26,40 +26,37 @@ open class ApiClient(val baseUrl: String) {
2626
val jsonHeaders: Map<String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType)
2727
}
2828

29-
inline protected fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody {
30-
if(content is File) {
31-
return RequestBody.create(
32-
MediaType.parse(mediaType), content
33-
)
34-
} else if(mediaType == FormDataMediaType) {
35-
var builder = FormBody.Builder()
36-
// content's type *must* be Map<String, Any>
37-
@Suppress("UNCHECKED_CAST")
38-
(content as Map<String,String>).forEach { key, value ->
39-
builder = builder.add(key, value)
40-
}
41-
return builder.build()
42-
} else if(mediaType == JsonMediaType) {
43-
return RequestBody.create(
44-
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
45-
)
46-
} else if (mediaType == XmlMediaType) {
47-
TODO("xml not currently supported.")
48-
}
49-
50-
// TODO: this should be extended with other serializers
51-
TODO("requestBody currently only supports JSON body and File body.")
52-
}
53-
54-
inline protected fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
29+
protected inline fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
30+
when {
31+
content is File -> RequestBody.create(
32+
MediaType.parse(mediaType), content
33+
)
34+
mediaType == FormDataMediaType -> {
35+
var builder = FormBody.Builder()
36+
// content's type *must* be Map<String, Any>
37+
@Suppress("UNCHECKED_CAST")
38+
(content as Map<String,String>).forEach { key, value ->
39+
builder = builder.add(key, value)
40+
}
41+
builder.build()
42+
}
43+
mediaType == JsonMediaType -> RequestBody.create(
44+
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
45+
)
46+
mediaType == XmlMediaType -> TODO("xml not currently supported.")
47+
// TODO: this should be extended with other serializers
48+
else -> TODO("requestBody currently only supports JSON body and File body.")
49+
}
50+
51+
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
5552
if(body == null) return null
5653
return when(mediaType) {
5754
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source())
5855
else -> TODO()
5956
}
6057
}
6158

62-
inline protected fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
59+
protected inline fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
6360
val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.")
6461

6562
var urlBuilder = httpUrl.newBuilder()

samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiInfrastructureResponse.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,31 @@ abstract class ApiInfrastructureResponse<T>(val responseType: ResponseType) {
1010
}
1111

1212
class Success<T>(
13-
val data: T,
14-
override val statusCode: Int = -1,
15-
override val headers: Map<String, List<String>> = mapOf()
13+
val data: T,
14+
override val statusCode: Int = -1,
15+
override val headers: Map<String, List<String>> = mapOf()
1616
): ApiInfrastructureResponse<T>(ResponseType.Success)
1717

1818
class Informational<T>(
19-
val statusText: String,
20-
override val statusCode: Int = -1,
21-
override val headers: Map<String, List<String>> = mapOf()
19+
val statusText: String,
20+
override val statusCode: Int = -1,
21+
override val headers: Map<String, List<String>> = mapOf()
2222
) : ApiInfrastructureResponse<T>(ResponseType.Informational)
2323

2424
class Redirection<T>(
25-
override val statusCode: Int = -1,
26-
override val headers: Map<String, List<String>> = mapOf()
25+
override val statusCode: Int = -1,
26+
override val headers: Map<String, List<String>> = mapOf()
2727
) : ApiInfrastructureResponse<T>(ResponseType.Redirection)
2828

2929
class ClientError<T>(
30-
val body: Any? = null,
31-
override val statusCode: Int = -1,
32-
override val headers: Map<String, List<String>> = mapOf()
30+
val body: Any? = null,
31+
override val statusCode: Int = -1,
32+
override val headers: Map<String, List<String>> = mapOf()
3333
) : ApiInfrastructureResponse<T>(ResponseType.ClientError)
3434

3535
class ServerError<T>(
36-
val message: String? = null,
37-
val body: Any? = null,
38-
override val statusCode: Int = -1,
39-
override val headers: Map<String, List<String>>
36+
val message: String? = null,
37+
val body: Any? = null,
38+
override val statusCode: Int = -1,
39+
override val headers: Map<String, List<String>>
4040
): ApiInfrastructureResponse<T>(ResponseType.ServerError)

samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ package org.openapitools.client.infrastructure
99
* multi-valued headers as csv-only.
1010
*/
1111
data class RequestConfig(
12-
val method: RequestMethod,
13-
val path: String,
14-
val headers: Map<String, String> = mapOf(),
15-
val query: Map<String, List<String>> = mapOf())
12+
val method: RequestMethod,
13+
val path: String,
14+
val headers: Map<String, String> = mapOf(),
15+
val query: Map<String, List<String>> = mapOf()
16+
)

samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import java.util.*
88
object Serializer {
99
@JvmStatic
1010
val moshi: Moshi = Moshi.Builder()
11-
.add(KotlinJsonAdapterFactory())
12-
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
13-
.build()
11+
.add(KotlinJsonAdapterFactory())
12+
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
13+
.build()
1414
}

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

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import java.io.File
55

66
open class ApiClient(val baseUrl: String) {
77
companion object {
8-
protected val ContentType = "Content-Type"
9-
protected val Accept = "Accept"
10-
protected val JsonMediaType = "application/json"
11-
protected val FormDataMediaType = "multipart/form-data"
12-
protected val XmlMediaType = "application/xml"
8+
protected const val ContentType = "Content-Type"
9+
protected const val Accept = "Accept"
10+
protected const val JsonMediaType = "application/json"
11+
protected const val FormDataMediaType = "multipart/form-data"
12+
protected const val XmlMediaType = "application/xml"
1313

1414
@JvmStatic
1515
val client by lazy {
@@ -26,40 +26,37 @@ open class ApiClient(val baseUrl: String) {
2626
val jsonHeaders: Map<String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType)
2727
}
2828

29-
inline protected fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody {
30-
if(content is File) {
31-
return RequestBody.create(
32-
MediaType.parse(mediaType), content
33-
)
34-
} else if(mediaType == FormDataMediaType) {
35-
var builder = FormBody.Builder()
36-
// content's type *must* be Map<String, Any>
37-
@Suppress("UNCHECKED_CAST")
38-
(content as Map<String,String>).forEach { key, value ->
39-
builder = builder.add(key, value)
40-
}
41-
return builder.build()
42-
} else if(mediaType == JsonMediaType) {
43-
return RequestBody.create(
44-
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
45-
)
46-
} else if (mediaType == XmlMediaType) {
47-
TODO("xml not currently supported.")
48-
}
49-
50-
// TODO: this should be extended with other serializers
51-
TODO("requestBody currently only supports JSON body and File body.")
52-
}
53-
54-
inline protected fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
29+
protected inline fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
30+
when {
31+
content is File -> RequestBody.create(
32+
MediaType.parse(mediaType), content
33+
)
34+
mediaType == FormDataMediaType -> {
35+
var builder = FormBody.Builder()
36+
// content's type *must* be Map<String, Any>
37+
@Suppress("UNCHECKED_CAST")
38+
(content as Map<String,String>).forEach { key, value ->
39+
builder = builder.add(key, value)
40+
}
41+
builder.build()
42+
}
43+
mediaType == JsonMediaType -> RequestBody.create(
44+
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
45+
)
46+
mediaType == XmlMediaType -> TODO("xml not currently supported.")
47+
// TODO: this should be extended with other serializers
48+
else -> TODO("requestBody currently only supports JSON body and File body.")
49+
}
50+
51+
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
5552
if(body == null) return null
5653
return when(mediaType) {
5754
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source())
5855
else -> TODO()
5956
}
6057
}
6158

62-
inline protected fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
59+
protected inline fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
6360
val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.")
6461

6562
var urlBuilder = httpUrl.newBuilder()

0 commit comments

Comments
 (0)