Skip to content

Commit 6dcd731

Browse files
committed
[kotlin][ktor] add set JSON as the ContentType header for ktor projects
samples revert content type
1 parent 53873ff commit 6dcd731

File tree

53 files changed

+92
-91
lines changed
  • modules/openapi-generator/src/main/resources/kotlin-client
  • samples
    • client/petstore
      • kotlin-allOff-discriminator/.openapi-generator
      • kotlin-array-simple-string-jvm-okhttp3/.openapi-generator
      • kotlin-array-simple-string-jvm-okhttp4/.openapi-generator
      • kotlin-array-simple-string-jvm-volley/.openapi-generator
      • kotlin-array-simple-string-multiplatform
      • kotlin-bigdecimal-default-multiplatform
      • kotlin-bigdecimal-default-okhttp4/.openapi-generator
      • kotlin-enum-default-value/.openapi-generator
      • kotlin-jackson/.openapi-generator
      • kotlin-json-request-string/.openapi-generator
      • kotlin-jvm-ktor-gson
      • kotlin-jvm-ktor-jackson
      • kotlin-jvm-okhttp4-coroutines/.openapi-generator
      • kotlin-jvm-volley/.openapi-generator
      • kotlin-modelMutable/.openapi-generator
      • kotlin-moshi-codegen/.openapi-generator
      • kotlin-multiplatform
        • .openapi-generator
        • src/commonMain/kotlin/org/openapitools/client/infrastructure
      • kotlin-nonpublic/.openapi-generator
      • kotlin-nullable/.openapi-generator
      • kotlin-okhttp3/.openapi-generator
      • kotlin-retrofit2-kotlinx_serialization/.openapi-generator
      • kotlin-retrofit2-rx3/.openapi-generator
      • kotlin-retrofit2/.openapi-generator
      • kotlin-string/.openapi-generator
      • kotlin-threetenbp/.openapi-generator
      • kotlin-uppercase-enum/.openapi-generator
    • server/petstore
      • kotlin-server-modelMutable
      • kotlin-server
      • kotlin-springboot-delegate
      • kotlin-springboot-modelMutable/.openapi-generator
      • kotlin-springboot-reactive/.openapi-generator
      • kotlin-springboot-source-swagger1/.openapi-generator
      • kotlin-springboot-source-swagger2/.openapi-generator
      • kotlin-springboot-springfox/.openapi-generator
      • kotlin-springboot/.openapi-generator
      • kotlin-vertx-modelMutable/.openapi-generator
      • kotlin/vertx/.openapi-generator

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+92
-91
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ wrapper {
1111
buildscript {
1212
ext.kotlin_version = '1.6.10'
1313
{{#jvm-ktor}}
14-
ext.ktor_version = '2.0.3'
14+
ext.ktor_version = '2.1.2'
1515
{{/jvm-ktor}}
1616
{{#jvm-retrofit2}}
1717
ext.retrofitVersion = '2.9.0'

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import io.ktor.client.request.parameter
1212
import io.ktor.client.request.request
1313
import io.ktor.client.request.setBody
1414
import io.ktor.client.statement.HttpResponse
15+
import io.ktor.http.contentType
1516
import io.ktor.http.HttpHeaders
1617
import io.ktor.http.HttpMethod
1718
import io.ktor.http.Parameters
@@ -34,11 +35,6 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
3435
import com.fasterxml.jackson.core.util.DefaultIndenter
3536
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter
3637
{{/jackson}}
37-
import org.openapitools.client.auth.ApiKeyAuth
38-
import org.openapitools.client.auth.Authentication
39-
import org.openapitools.client.auth.HttpBasicAuth
40-
import org.openapitools.client.auth.HttpBearerAuth
41-
import org.openapitools.client.auth.OAuth
4238
import {{packageName}}.auth.*
4339

4440
{{#nonPublicApi}}internal {{/nonPublicApi}}open class ApiClient(
@@ -111,7 +107,7 @@ import {{packageName}}.auth.*
111107
* @param username Username
112108
*/
113109
fun setUsername(username: String) {
114-
val auth = authentications.values.firstOrNull { it is HttpBasicAuth } as HttpBasicAuth?
110+
val auth = authentications?.values?.firstOrNull { it is HttpBasicAuth } as HttpBasicAuth?
115111
?: throw Exception("No HTTP basic authentication configured")
116112
auth.username = username
117113
}
@@ -122,7 +118,7 @@ import {{packageName}}.auth.*
122118
* @param password Password
123119
*/
124120
fun setPassword(password: String) {
125-
val auth = authentications.values.firstOrNull { it is HttpBasicAuth } as HttpBasicAuth?
121+
val auth = authentications?.values?.firstOrNull { it is HttpBasicAuth } as HttpBasicAuth?
126122
?: throw Exception("No HTTP basic authentication configured")
127123
auth.password = password
128124
}
@@ -134,7 +130,7 @@ import {{packageName}}.auth.*
134130
* @param paramName The name of the API key parameter, or null or set the first key.
135131
*/
136132
fun setApiKey(apiKey: String, paramName: String? = null) {
137-
val auth = authentications.values.firstOrNull { it is ApiKeyAuth && (paramName == null || paramName == it.paramName)} as ApiKeyAuth?
133+
val auth = authentications?.values?.firstOrNull { it is ApiKeyAuth && (paramName == null || paramName == it.paramName)} as ApiKeyAuth?
138134
?: throw Exception("No API key authentication configured")
139135
auth.apiKey = apiKey
140136
}
@@ -146,7 +142,7 @@ import {{packageName}}.auth.*
146142
* @param paramName The name of the API key parameter, or null or set the first key.
147143
*/
148144
fun setApiKeyPrefix(apiKeyPrefix: String, paramName: String? = null) {
149-
val auth = authentications.values.firstOrNull { it is ApiKeyAuth && (paramName == null || paramName == it.paramName) } as ApiKeyAuth?
145+
val auth = authentications?.values?.firstOrNull { it is ApiKeyAuth && (paramName == null || paramName == it.paramName) } as ApiKeyAuth?
150146
?: throw Exception("No API key authentication configured")
151147
auth.apiKeyPrefix = apiKeyPrefix
152148
}
@@ -157,7 +153,7 @@ import {{packageName}}.auth.*
157153
* @param accessToken Access token
158154
*/
159155
fun setAccessToken(accessToken: String) {
160-
val auth = authentications.values.firstOrNull { it is OAuth } as OAuth?
156+
val auth = authentications?.values?.firstOrNull { it is OAuth } as OAuth?
161157
?: throw Exception("No OAuth2 authentication configured")
162158
auth.accessToken = accessToken
163159
}
@@ -168,7 +164,7 @@ import {{packageName}}.auth.*
168164
* @param bearerToken The bearer token.
169165
*/
170166
fun setBearerToken(bearerToken: String) {
171-
val auth = authentications.values.firstOrNull { it is HttpBearerAuth } as HttpBearerAuth?
167+
val auth = authentications?.values?.firstOrNull { it is HttpBearerAuth } as HttpBearerAuth?
172168
?: throw Exception("No Bearer authentication configured")
173169
auth.bearerToken = bearerToken
174170
}
@@ -199,8 +195,9 @@ import {{packageName}}.auth.*
199195
}
200196
this.method = requestConfig.method.httpMethod
201197
headers.filter { header -> !UNSAFE_HEADERS.contains(header.key) }.forEach { header -> this.header(header.key, header.value) }
202-
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH))
198+
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH)) {
203199
setBody(body)
200+
}
204201
}
205202
}
206203

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import io.ktor.client.request.forms.MultiPartFormDataContent
1111
import io.ktor.client.request.header
1212
import io.ktor.client.request.parameter
1313
import io.ktor.client.statement.HttpResponse
14+
import io.ktor.http.ContentType.Application.Json
1415
import io.ktor.http.*
1516
import io.ktor.http.content.PartData
1617
import kotlin.Unit
17-
import kotlinx.serialization.json.Json
1818

1919
import {{packageName}}.auth.*
2020

@@ -153,9 +153,9 @@ import {{packageName}}.auth.*
153153
}
154154
this.method = requestConfig.method.httpMethod
155155
headers.filter { header -> !UNSAFE_HEADERS.contains(header.key) }.forEach { header -> this.header(header.key, header.value) }
156-
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH))
156+
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH)) {
157157
this.setBody(body)
158-
158+
}
159159
}
160160
}
161161

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.2.1-SNAPSHOT
1+
6.1.0-SNAPSHOT
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.2.1-SNAPSHOT
1+
6.1.0-SNAPSHOT
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.2.1-SNAPSHOT
1+
6.1.0-SNAPSHOT
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.2.1-SNAPSHOT
1+
6.1.0-SNAPSHOT
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.2.1-SNAPSHOT
1+
6.1.0-SNAPSHOT

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import io.ktor.client.request.forms.MultiPartFormDataContent
1111
import io.ktor.client.request.header
1212
import io.ktor.client.request.parameter
1313
import io.ktor.client.statement.HttpResponse
14+
import io.ktor.http.ContentType.Application.Json
1415
import io.ktor.http.*
1516
import io.ktor.http.content.PartData
1617
import kotlin.Unit
@@ -142,9 +143,10 @@ open class ApiClient(
142143
}
143144
this.method = requestConfig.method.httpMethod
144145
headers.filter { header -> !UNSAFE_HEADERS.contains(header.key) }.forEach { header -> this.header(header.key, header.value) }
145-
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH))
146+
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH)) {
147+
this.contentType(Json)
146148
this.setBody(body)
147-
149+
}
148150
}
149151
}
150152

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.2.1-SNAPSHOT
1+
6.1.0-SNAPSHOT

0 commit comments

Comments
 (0)