Skip to content

Commit 53873ff

Browse files
authored
[kotlin] Fix empty body with POST/etc in OkHttp client (#13666)
1 parent 1e66f2f commit 53873ff

File tree

21 files changed

+168
-168
lines changed
  • modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure
  • samples/client/petstore
    • kotlin-allOff-discriminator/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-array-simple-string-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-default-values-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-modelMutable/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin/src/main/kotlin/org/openapitools/client/infrastructure

21 files changed

+168
-168
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,24 +343,24 @@ import com.squareup.moshi.adapter
343343
}.build()
344344

345345
// take content-type/accept from spec or set to default (application/json) if not defined
346-
if (requestConfig.headers[ContentType].isNullOrEmpty()) {
346+
if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
347347
requestConfig.headers[ContentType] = JsonMediaType
348348
}
349349
if (requestConfig.headers[Accept].isNullOrEmpty()) {
350350
requestConfig.headers[Accept] = JsonMediaType
351351
}
352352
val headers = requestConfig.headers
353353

354-
if(headers[ContentType].isNullOrEmpty()) {
355-
throw kotlin.IllegalStateException("Missing Content-Type header. This is required.")
356-
}
357-
358-
if(headers[Accept].isNullOrEmpty()) {
354+
if (headers[Accept].isNullOrEmpty()) {
359355
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
360356
}
361357

362-
// TODO: support multiple contentType options here.
363-
val contentType = (headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
358+
val contentType = if (headers[ContentType] != null) {
359+
// TODO: support multiple contentType options here.
360+
(headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
361+
} else {
362+
null
363+
}
364364

365365
val request = when (requestConfig.method) {
366366
RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
158158
}.build()
159159

160160
// take content-type/accept from spec or set to default (application/json) if not defined
161-
if (requestConfig.headers[ContentType].isNullOrEmpty()) {
161+
if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
162162
requestConfig.headers[ContentType] = JsonMediaType
163163
}
164164
if (requestConfig.headers[Accept].isNullOrEmpty()) {
165165
requestConfig.headers[Accept] = JsonMediaType
166166
}
167167
val headers = requestConfig.headers
168168

169-
if(headers[ContentType].isNullOrEmpty()) {
170-
throw kotlin.IllegalStateException("Missing Content-Type header. This is required.")
171-
}
172-
173-
if(headers[Accept].isNullOrEmpty()) {
169+
if (headers[Accept].isNullOrEmpty()) {
174170
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
175171
}
176172

177-
// TODO: support multiple contentType options here.
178-
val contentType = (headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
173+
val contentType = if (headers[ContentType] != null) {
174+
// TODO: support multiple contentType options here.
175+
(headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
176+
} else {
177+
null
178+
}
179179

180180
val request = when (requestConfig.method) {
181181
RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,24 +156,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
156156
}.build()
157157

158158
// take content-type/accept from spec or set to default (application/json) if not defined
159-
if (requestConfig.headers[ContentType].isNullOrEmpty()) {
159+
if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
160160
requestConfig.headers[ContentType] = JsonMediaType
161161
}
162162
if (requestConfig.headers[Accept].isNullOrEmpty()) {
163163
requestConfig.headers[Accept] = JsonMediaType
164164
}
165165
val headers = requestConfig.headers
166166

167-
if(headers[ContentType].isNullOrEmpty()) {
168-
throw kotlin.IllegalStateException("Missing Content-Type header. This is required.")
169-
}
170-
171-
if(headers[Accept].isNullOrEmpty()) {
167+
if (headers[Accept].isNullOrEmpty()) {
172168
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
173169
}
174170

175-
// TODO: support multiple contentType options here.
176-
val contentType = (headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
171+
val contentType = if (headers[ContentType] != null) {
172+
// TODO: support multiple contentType options here.
173+
(headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
174+
} else {
175+
null
176+
}
177177

178178
val request = when (requestConfig.method) {
179179
RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
158158
}.build()
159159

160160
// take content-type/accept from spec or set to default (application/json) if not defined
161-
if (requestConfig.headers[ContentType].isNullOrEmpty()) {
161+
if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
162162
requestConfig.headers[ContentType] = JsonMediaType
163163
}
164164
if (requestConfig.headers[Accept].isNullOrEmpty()) {
165165
requestConfig.headers[Accept] = JsonMediaType
166166
}
167167
val headers = requestConfig.headers
168168

169-
if(headers[ContentType].isNullOrEmpty()) {
170-
throw kotlin.IllegalStateException("Missing Content-Type header. This is required.")
171-
}
172-
173-
if(headers[Accept].isNullOrEmpty()) {
169+
if (headers[Accept].isNullOrEmpty()) {
174170
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
175171
}
176172

177-
// TODO: support multiple contentType options here.
178-
val contentType = (headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
173+
val contentType = if (headers[ContentType] != null) {
174+
// TODO: support multiple contentType options here.
175+
(headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
176+
} else {
177+
null
178+
}
179179

180180
val request = when (requestConfig.method) {
181181
RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))

samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
158158
}.build()
159159

160160
// take content-type/accept from spec or set to default (application/json) if not defined
161-
if (requestConfig.headers[ContentType].isNullOrEmpty()) {
161+
if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
162162
requestConfig.headers[ContentType] = JsonMediaType
163163
}
164164
if (requestConfig.headers[Accept].isNullOrEmpty()) {
165165
requestConfig.headers[Accept] = JsonMediaType
166166
}
167167
val headers = requestConfig.headers
168168

169-
if(headers[ContentType].isNullOrEmpty()) {
170-
throw kotlin.IllegalStateException("Missing Content-Type header. This is required.")
171-
}
172-
173-
if(headers[Accept].isNullOrEmpty()) {
169+
if (headers[Accept].isNullOrEmpty()) {
174170
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
175171
}
176172

177-
// TODO: support multiple contentType options here.
178-
val contentType = (headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
173+
val contentType = if (headers[ContentType] != null) {
174+
// TODO: support multiple contentType options here.
175+
(headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
176+
} else {
177+
null
178+
}
179179

180180
val request = when (requestConfig.method) {
181181
RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))

samples/client/petstore/kotlin-default-values-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,24 +156,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
156156
}.build()
157157

158158
// take content-type/accept from spec or set to default (application/json) if not defined
159-
if (requestConfig.headers[ContentType].isNullOrEmpty()) {
159+
if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
160160
requestConfig.headers[ContentType] = JsonMediaType
161161
}
162162
if (requestConfig.headers[Accept].isNullOrEmpty()) {
163163
requestConfig.headers[Accept] = JsonMediaType
164164
}
165165
val headers = requestConfig.headers
166166

167-
if(headers[ContentType].isNullOrEmpty()) {
168-
throw kotlin.IllegalStateException("Missing Content-Type header. This is required.")
169-
}
170-
171-
if(headers[Accept].isNullOrEmpty()) {
167+
if (headers[Accept].isNullOrEmpty()) {
172168
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
173169
}
174170

175-
// TODO: support multiple contentType options here.
176-
val contentType = (headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
171+
val contentType = if (headers[ContentType] != null) {
172+
// TODO: support multiple contentType options here.
173+
(headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
174+
} else {
175+
null
176+
}
177177

178178
val request = when (requestConfig.method) {
179179
RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))

samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
158158
}.build()
159159

160160
// take content-type/accept from spec or set to default (application/json) if not defined
161-
if (requestConfig.headers[ContentType].isNullOrEmpty()) {
161+
if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
162162
requestConfig.headers[ContentType] = JsonMediaType
163163
}
164164
if (requestConfig.headers[Accept].isNullOrEmpty()) {
165165
requestConfig.headers[Accept] = JsonMediaType
166166
}
167167
val headers = requestConfig.headers
168168

169-
if(headers[ContentType].isNullOrEmpty()) {
170-
throw kotlin.IllegalStateException("Missing Content-Type header. This is required.")
171-
}
172-
173-
if(headers[Accept].isNullOrEmpty()) {
169+
if (headers[Accept].isNullOrEmpty()) {
174170
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
175171
}
176172

177-
// TODO: support multiple contentType options here.
178-
val contentType = (headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
173+
val contentType = if (headers[ContentType] != null) {
174+
// TODO: support multiple contentType options here.
175+
(headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
176+
} else {
177+
null
178+
}
179179

180180
val request = when (requestConfig.method) {
181181
RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))

samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
158158
}.build()
159159

160160
// take content-type/accept from spec or set to default (application/json) if not defined
161-
if (requestConfig.headers[ContentType].isNullOrEmpty()) {
161+
if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
162162
requestConfig.headers[ContentType] = JsonMediaType
163163
}
164164
if (requestConfig.headers[Accept].isNullOrEmpty()) {
165165
requestConfig.headers[Accept] = JsonMediaType
166166
}
167167
val headers = requestConfig.headers
168168

169-
if(headers[ContentType].isNullOrEmpty()) {
170-
throw kotlin.IllegalStateException("Missing Content-Type header. This is required.")
171-
}
172-
173-
if(headers[Accept].isNullOrEmpty()) {
169+
if (headers[Accept].isNullOrEmpty()) {
174170
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
175171
}
176172

177-
// TODO: support multiple contentType options here.
178-
val contentType = (headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
173+
val contentType = if (headers[ContentType] != null) {
174+
// TODO: support multiple contentType options here.
175+
(headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
176+
} else {
177+
null
178+
}
179179

180180
val request = when (requestConfig.method) {
181181
RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,24 +176,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
176176
}.build()
177177

178178
// take content-type/accept from spec or set to default (application/json) if not defined
179-
if (requestConfig.headers[ContentType].isNullOrEmpty()) {
179+
if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
180180
requestConfig.headers[ContentType] = JsonMediaType
181181
}
182182
if (requestConfig.headers[Accept].isNullOrEmpty()) {
183183
requestConfig.headers[Accept] = JsonMediaType
184184
}
185185
val headers = requestConfig.headers
186186

187-
if(headers[ContentType].isNullOrEmpty()) {
188-
throw kotlin.IllegalStateException("Missing Content-Type header. This is required.")
189-
}
190-
191-
if(headers[Accept].isNullOrEmpty()) {
187+
if (headers[Accept].isNullOrEmpty()) {
192188
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
193189
}
194190

195-
// TODO: support multiple contentType options here.
196-
val contentType = (headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
191+
val contentType = if (headers[ContentType] != null) {
192+
// TODO: support multiple contentType options here.
193+
(headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
194+
} else {
195+
null
196+
}
197197

198198
val request = when (requestConfig.method) {
199199
RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,24 +176,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
176176
}.build()
177177

178178
// take content-type/accept from spec or set to default (application/json) if not defined
179-
if (requestConfig.headers[ContentType].isNullOrEmpty()) {
179+
if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
180180
requestConfig.headers[ContentType] = JsonMediaType
181181
}
182182
if (requestConfig.headers[Accept].isNullOrEmpty()) {
183183
requestConfig.headers[Accept] = JsonMediaType
184184
}
185185
val headers = requestConfig.headers
186186

187-
if(headers[ContentType].isNullOrEmpty()) {
188-
throw kotlin.IllegalStateException("Missing Content-Type header. This is required.")
189-
}
190-
191-
if(headers[Accept].isNullOrEmpty()) {
187+
if (headers[Accept].isNullOrEmpty()) {
192188
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
193189
}
194190

195-
// TODO: support multiple contentType options here.
196-
val contentType = (headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
191+
val contentType = if (headers[ContentType] != null) {
192+
// TODO: support multiple contentType options here.
193+
(headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
194+
} else {
195+
null
196+
}
197197

198198
val request = when (requestConfig.method) {
199199
RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))

0 commit comments

Comments
 (0)