Skip to content

Commit f35b132

Browse files
authored
[Kotlin] fix(#14044): ktor JSON serialization handling (#16843)
1 parent ced31f4 commit f35b132

File tree

9 files changed

+41
-0
lines changed
  • modules/openapi-generator/src/main/resources/kotlin-client/libraries
  • samples/client/petstore
    • kotlin-array-simple-string-multiplatform/src/commonMain/kotlin/org/openapitools/client/infrastructure
    • kotlin-bigdecimal-default-multiplatform/src/commonMain/kotlin/org/openapitools/client/infrastructure
    • kotlin-default-values-multiplatform/src/commonMain/kotlin/org/openapitools/client/infrastructure
    • kotlin-jvm-ktor-gson/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-jvm-ktor-jackson/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-jvm-ktor-kotlinx_serialization/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-multiplatform/src/commonMain/kotlin/org/openapitools/client/infrastructure

9 files changed

+41
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import io.ktor.client.request.request
1313
import io.ktor.client.request.setBody
1414
import io.ktor.client.statement.HttpResponse
1515
import io.ktor.http.contentType
16+
import io.ktor.http.ContentType
1617
import io.ktor.http.HttpHeaders
1718
import io.ktor.http.HttpMethod
1819
import io.ktor.http.Parameters
@@ -196,6 +197,9 @@ import {{packageName}}.auth.*
196197
this.method = requestConfig.method.httpMethod
197198
headers.filter { header -> !UNSAFE_HEADERS.contains(header.key) }.forEach { header -> this.header(header.key, header.value) }
198199
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH)) {
200+
val contentType = (requestConfig.headers[HttpHeaders.ContentType]?.let { ContentType.parse(it) }
201+
?: ContentType.Application.Json)
202+
this.contentType(contentType)
199203
setBody(body)
200204
}
201205
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import io.ktor.client.request.forms.MultiPartFormDataContent
1010
import io.ktor.client.request.header
1111
import io.ktor.client.request.parameter
1212
import io.ktor.client.statement.HttpResponse
13+
import io.ktor.http.ContentType
1314
import io.ktor.serialization.kotlinx.json.json
1415
import io.ktor.http.*
1516
import io.ktor.http.content.PartData
17+
import io.ktor.http.contentType
1618
import kotlin.Unit
1719
import kotlinx.serialization.json.Json
1820

@@ -165,6 +167,9 @@ import {{packageName}}.auth.*
165167
this.method = requestConfig.method.httpMethod
166168
headers.filter { header -> !UNSAFE_HEADERS.contains(header.key) }.forEach { header -> this.header(header.key, header.value) }
167169
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH)) {
170+
val contentType = (requestConfig.headers[HttpHeaders.ContentType]?.let { ContentType.parse(it) }
171+
?: ContentType.Application.Json)
172+
this.contentType(contentType)
168173
this.setBody(body)
169174
}
170175
}

samples/client/petstore/kotlin-array-simple-string-multiplatform/src/commonMain/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import io.ktor.client.request.forms.MultiPartFormDataContent
1010
import io.ktor.client.request.header
1111
import io.ktor.client.request.parameter
1212
import io.ktor.client.statement.HttpResponse
13+
import io.ktor.http.ContentType
1314
import io.ktor.serialization.kotlinx.json.json
1415
import io.ktor.http.*
1516
import io.ktor.http.content.PartData
17+
import io.ktor.http.contentType
1618
import kotlin.Unit
1719
import kotlinx.serialization.json.Json
1820

@@ -154,6 +156,9 @@ open class ApiClient(
154156
this.method = requestConfig.method.httpMethod
155157
headers.filter { header -> !UNSAFE_HEADERS.contains(header.key) }.forEach { header -> this.header(header.key, header.value) }
156158
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH)) {
159+
val contentType = (requestConfig.headers[HttpHeaders.ContentType]?.let { ContentType.parse(it) }
160+
?: ContentType.Application.Json)
161+
this.contentType(contentType)
157162
this.setBody(body)
158163
}
159164
}

samples/client/petstore/kotlin-bigdecimal-default-multiplatform/src/commonMain/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import io.ktor.client.request.forms.MultiPartFormDataContent
1010
import io.ktor.client.request.header
1111
import io.ktor.client.request.parameter
1212
import io.ktor.client.statement.HttpResponse
13+
import io.ktor.http.ContentType
1314
import io.ktor.serialization.kotlinx.json.json
1415
import io.ktor.http.*
1516
import io.ktor.http.content.PartData
17+
import io.ktor.http.contentType
1618
import kotlin.Unit
1719
import kotlinx.serialization.json.Json
1820

@@ -154,6 +156,9 @@ open class ApiClient(
154156
this.method = requestConfig.method.httpMethod
155157
headers.filter { header -> !UNSAFE_HEADERS.contains(header.key) }.forEach { header -> this.header(header.key, header.value) }
156158
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH)) {
159+
val contentType = (requestConfig.headers[HttpHeaders.ContentType]?.let { ContentType.parse(it) }
160+
?: ContentType.Application.Json)
161+
this.contentType(contentType)
157162
this.setBody(body)
158163
}
159164
}

samples/client/petstore/kotlin-default-values-multiplatform/src/commonMain/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import io.ktor.client.request.forms.MultiPartFormDataContent
1010
import io.ktor.client.request.header
1111
import io.ktor.client.request.parameter
1212
import io.ktor.client.statement.HttpResponse
13+
import io.ktor.http.ContentType
1314
import io.ktor.serialization.kotlinx.json.json
1415
import io.ktor.http.*
1516
import io.ktor.http.content.PartData
17+
import io.ktor.http.contentType
1618
import kotlin.Unit
1719
import kotlinx.serialization.json.Json
1820

@@ -154,6 +156,9 @@ open class ApiClient(
154156
this.method = requestConfig.method.httpMethod
155157
headers.filter { header -> !UNSAFE_HEADERS.contains(header.key) }.forEach { header -> this.header(header.key, header.value) }
156158
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH)) {
159+
val contentType = (requestConfig.headers[HttpHeaders.ContentType]?.let { ContentType.parse(it) }
160+
?: ContentType.Application.Json)
161+
this.contentType(contentType)
157162
this.setBody(body)
158163
}
159164
}

samples/client/petstore/kotlin-jvm-ktor-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import io.ktor.client.request.request
1313
import io.ktor.client.request.setBody
1414
import io.ktor.client.statement.HttpResponse
1515
import io.ktor.http.contentType
16+
import io.ktor.http.ContentType
1617
import io.ktor.http.HttpHeaders
1718
import io.ktor.http.HttpMethod
1819
import io.ktor.http.Parameters
@@ -156,6 +157,9 @@ open class ApiClient(
156157
this.method = requestConfig.method.httpMethod
157158
headers.filter { header -> !UNSAFE_HEADERS.contains(header.key) }.forEach { header -> this.header(header.key, header.value) }
158159
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH)) {
160+
val contentType = (requestConfig.headers[HttpHeaders.ContentType]?.let { ContentType.parse(it) }
161+
?: ContentType.Application.Json)
162+
this.contentType(contentType)
159163
setBody(body)
160164
}
161165
}

samples/client/petstore/kotlin-jvm-ktor-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import io.ktor.client.request.request
1313
import io.ktor.client.request.setBody
1414
import io.ktor.client.statement.HttpResponse
1515
import io.ktor.http.contentType
16+
import io.ktor.http.ContentType
1617
import io.ktor.http.HttpHeaders
1718
import io.ktor.http.HttpMethod
1819
import io.ktor.http.Parameters
@@ -164,6 +165,9 @@ open class ApiClient(
164165
this.method = requestConfig.method.httpMethod
165166
headers.filter { header -> !UNSAFE_HEADERS.contains(header.key) }.forEach { header -> this.header(header.key, header.value) }
166167
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH)) {
168+
val contentType = (requestConfig.headers[HttpHeaders.ContentType]?.let { ContentType.parse(it) }
169+
?: ContentType.Application.Json)
170+
this.contentType(contentType)
167171
setBody(body)
168172
}
169173
}

samples/client/petstore/kotlin-jvm-ktor-kotlinx_serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import io.ktor.client.request.request
1313
import io.ktor.client.request.setBody
1414
import io.ktor.client.statement.HttpResponse
1515
import io.ktor.http.contentType
16+
import io.ktor.http.ContentType
1617
import io.ktor.http.HttpHeaders
1718
import io.ktor.http.HttpMethod
1819
import io.ktor.http.Parameters
@@ -147,6 +148,9 @@ open class ApiClient(
147148
this.method = requestConfig.method.httpMethod
148149
headers.filter { header -> !UNSAFE_HEADERS.contains(header.key) }.forEach { header -> this.header(header.key, header.value) }
149150
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH)) {
151+
val contentType = (requestConfig.headers[HttpHeaders.ContentType]?.let { ContentType.parse(it) }
152+
?: ContentType.Application.Json)
153+
this.contentType(contentType)
150154
setBody(body)
151155
}
152156
}

samples/client/petstore/kotlin-multiplatform/src/commonMain/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import io.ktor.client.request.forms.MultiPartFormDataContent
1010
import io.ktor.client.request.header
1111
import io.ktor.client.request.parameter
1212
import io.ktor.client.statement.HttpResponse
13+
import io.ktor.http.ContentType
1314
import io.ktor.serialization.kotlinx.json.json
1415
import io.ktor.http.*
1516
import io.ktor.http.content.PartData
17+
import io.ktor.http.contentType
1618
import kotlin.Unit
1719
import kotlinx.serialization.json.Json
1820

@@ -158,6 +160,9 @@ open class ApiClient(
158160
this.method = requestConfig.method.httpMethod
159161
headers.filter { header -> !UNSAFE_HEADERS.contains(header.key) }.forEach { header -> this.header(header.key, header.value) }
160162
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH)) {
163+
val contentType = (requestConfig.headers[HttpHeaders.ContentType]?.let { ContentType.parse(it) }
164+
?: ContentType.Application.Json)
165+
this.contentType(contentType)
161166
this.setBody(body)
162167
}
163168
}

0 commit comments

Comments
 (0)