Skip to content

Commit 97ddb8a

Browse files
committed
Keep previous constants so not to break existing code.
1 parent f732b71 commit 97ddb8a

103 files changed

Lines changed: 1096 additions & 1069 deletions

File tree

Some content is hidden

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

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,60 @@ import com.squareup.moshi.adapter
7373
protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream"
7474
protected const val TEXT_MEDIA_TYPE: String = "text/plain"
7575
76+
@Deprecated(
77+
message = "Please use the capitalized constant `CONTENT_TYPE` instead.",
78+
replaceWith = ReplaceWith("CONTENT_TYPE")
79+
)
80+
protected const val ContentType: String = CONTENT_TYPE
81+
82+
@Deprecated(
83+
message = "Please use the capitalized constant `ACCEPT` instead.",
84+
replaceWith = ReplaceWith("ACCEPT")
85+
)
86+
protected const val Accept: String = ACCEPT
87+
88+
@Deprecated(
89+
message = "Please use the capitalized constant `AUTHORIZATION` instead.",
90+
replaceWith = ReplaceWith("AUTHORIZATION")
91+
)
92+
protected const val Authorization: String = AUTHORIZATION
93+
94+
@Deprecated(
95+
message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.",
96+
replaceWith = ReplaceWith("JSON_MEDIA_TYPE")
97+
)
98+
protected const val JsonMediaType: String = JSON_MEDIA_TYPE
99+
100+
@Deprecated(
101+
message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.",
102+
replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE")
103+
)
104+
protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE
105+
106+
@Deprecated(
107+
message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.",
108+
replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE")
109+
)
110+
protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE
111+
112+
@Deprecated(
113+
message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.",
114+
replaceWith = ReplaceWith("XML_MEDIA_TYPE")
115+
)
116+
protected const val XmlMediaType: String = XML_MEDIA_TYPE
117+
118+
@Deprecated(
119+
message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.",
120+
replaceWith = ReplaceWith("OCTET_MEDIA_TYPE")
121+
)
122+
protected const val OctetMediaType: String = OCTET_MEDIA_TYPE
123+
124+
@Deprecated(
125+
message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.",
126+
replaceWith = ReplaceWith("TEXT_MEDIA_TYPE")
127+
)
128+
protected const val TextMediaType: String = TEXT_MEDIA_TYPE
129+
76130
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val apiKey: MutableMap<String, String> = mutableMapOf()
77131
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
78132
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}var username: String? = null

samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/AuthApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ open class AuthApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto
4040
companion object {
4141
@JvmStatic
4242
val defaultBasePath: String by lazy {
43-
System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost:3000")
43+
System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost:3000")
4444
}
4545
}
4646

samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/BodyApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ open class BodyApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto
4242
companion object {
4343
@JvmStatic
4444
val defaultBasePath: String by lazy {
45-
System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost:3000")
45+
System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost:3000")
4646
}
4747
}
4848

samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/FormApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ open class FormApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto
4040
companion object {
4141
@JvmStatic
4242
val defaultBasePath: String by lazy {
43-
System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost:3000")
43+
System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost:3000")
4444
}
4545
}
4646

samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/HeaderApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ open class HeaderApi(basePath: kotlin.String = defaultBasePath, client: Call.Fac
4141
companion object {
4242
@JvmStatic
4343
val defaultBasePath: String by lazy {
44-
System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost:3000")
44+
System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost:3000")
4545
}
4646
}
4747

samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/PathApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ open class PathApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto
4141
companion object {
4242
@JvmStatic
4343
val defaultBasePath: String by lazy {
44-
System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost:3000")
44+
System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost:3000")
4545
}
4646
}
4747

samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/QueryApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ open class QueryApi(basePath: kotlin.String = defaultBasePath, client: Call.Fact
4343
companion object {
4444
@JvmStatic
4545
val defaultBasePath: String by lazy {
46-
System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost:3000")
46+
System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost:3000")
4747
}
4848
}
4949

samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody()
3434

3535
open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) {
3636
companion object {
37-
protected const val ContentType: String = "Content-Type"
38-
protected const val Accept: String = "Accept"
39-
protected const val Authorization: String = "Authorization"
40-
protected const val JsonMediaType: String = "application/json"
41-
protected const val FormDataMediaType: String = "multipart/form-data"
42-
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
43-
protected const val XmlMediaType: String = "application/xml"
44-
protected const val OctetMediaType: String = "application/octet-stream"
45-
protected const val TextMediaType: String = "text/plain"
37+
protected const val CONTENT_TYPE: String = "Content-Type"
38+
protected const val ACCEPT: String = "ACCEPT"
39+
protected const val AUTHORIZATION: String = "AUTHORIZATION"
40+
protected const val JSON_MEDIA_TYPE: String = "application/json"
41+
protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data"
42+
protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded"
43+
protected const val XML_MEDIA_TYPE: String = "application/xml"
44+
protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream"
45+
protected const val TEXT_MEDIA_TYPE: String = "text/plain"
4646

4747
val apiKey: MutableMap<String, String> = mutableMapOf()
4848
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
4949
var username: String? = null
5050
var password: String? = null
5151
var accessToken: String? = null
52-
const val baseUrlKey: String = "org.openapitools.client.baseUrl"
52+
const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl"
5353

5454
@JvmStatic
5555
val defaultClient: OkHttpClient by lazy {
@@ -69,7 +69,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
6969
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
7070
val contentType = try {
7171
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
72-
} catch (io: IOException) {
72+
} catch (_: IOException) {
7373
"application/octet-stream"
7474
}
7575
return contentType
@@ -108,7 +108,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
108108
/**
109109
* Adds a File to a MultipartBody.Builder
110110
* Defined a helper in the requestBody method to not duplicate code
111-
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
111+
* It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File
112112
*
113113
* @param name The field name to add in the request
114114
* @param headers The headers that are in the PartConfig
@@ -138,7 +138,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
138138
if (serializer != null) {
139139
return serializer(obj)
140140
}
141-
141+
142142
return if (contentType?.contains("json") == true) {
143143
Serializer.moshi.adapter(Any::class.java).toJson(obj)
144144
} else {
@@ -149,7 +149,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
149149
/**
150150
* Adds any type to a MultipartBody.Builder
151151
* Defined a helper in the requestBody method to not duplicate code
152-
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
152+
* It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File.
153153
*
154154
* @param name The field name to add in the request
155155
* @param headers The headers that are in the PartConfig
@@ -172,7 +172,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
172172
when {
173173
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
174174
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
175-
mediaType == FormDataMediaType ->
175+
mediaType == FORM_DATA_MEDIA_TYPE ->
176176
MultipartBody.Builder()
177177
.setType(MultipartBody.FORM)
178178
.apply {
@@ -194,7 +194,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
194194
}
195195
}
196196
}.build()
197-
mediaType == FormUrlEncMediaType -> {
197+
mediaType == FORM_URL_ENC_MEDIA_TYPE -> {
198198
FormBody.Builder().apply {
199199
// content's type *must* be Map<String, PartConfig<*>>
200200
@Suppress("UNCHECKED_CAST")
@@ -208,21 +208,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
208208
EMPTY_REQUEST
209209
} else {
210210
Serializer.moshi.adapter(T::class.java).toJson(content)
211-
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
211+
.toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull())
212212
}
213-
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
214-
mediaType == TextMediaType && content is String ->
215-
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
213+
mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.")
214+
mediaType == TEXT_MEDIA_TYPE && content is String ->
215+
content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull())
216216
// TODO: this should be extended with other serializers
217217
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
218218
}
219219

220220
@OptIn(ExperimentalStdlibApi::class)
221-
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JsonMediaType): T? {
221+
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? {
222222
val body = response.body
223-
if(body == null) {
224-
return null
225-
} else if (T::class.java == Unit::class.java) {
223+
224+
if (T::class.java == Unit::class.java) {
226225
// No need to parse the body when we're not interested in the body
227226
// Useful when API is returning other Content-Type
228227
return null
@@ -284,23 +283,23 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
284283
}
285284
Serializer.moshi.adapter<T>().fromJson(bodyContent)
286285
}
287-
mediaType == OctetMediaType -> body.bytes() as? T
288-
mediaType == TextMediaType -> body.string() as? T
286+
mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T
287+
mediaType == TEXT_MEDIA_TYPE -> body.string() as? T
289288
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
290289
}
291290
}
292291

293292
protected fun <T> updateAuthParams(requestConfig: RequestConfig<T>) {
294-
if (requestConfig.headers[Authorization].isNullOrEmpty()) {
293+
if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) {
295294
username?.let { username ->
296295
password?.let { password ->
297-
requestConfig.headers[Authorization] = okhttp3.Credentials.basic(username, password)
296+
requestConfig.headers[AUTHORIZATION] = okhttp3.Credentials.basic(username, password)
298297
}
299298
}
300299
}
301-
if (requestConfig.headers[Authorization].isNullOrEmpty()) {
300+
if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) {
302301
accessToken?.let { accessToken ->
303-
requestConfig.headers[Authorization] = "Bearer $accessToken"
302+
requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken"
304303
}
305304
}
306305
}
@@ -322,21 +321,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
322321
}.build()
323322

324323
// take content-type/accept from spec or set to default (application/json) if not defined
325-
if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
326-
requestConfig.headers[ContentType] = JsonMediaType
324+
if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) {
325+
requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE
327326
}
328-
if (requestConfig.headers[Accept].isNullOrEmpty()) {
329-
requestConfig.headers[Accept] = JsonMediaType
327+
if (requestConfig.headers[ACCEPT].isNullOrEmpty()) {
328+
requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE
330329
}
331330
val headers = requestConfig.headers
332331

333-
if (headers[Accept].isNullOrEmpty()) {
334-
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
332+
if (headers[ACCEPT].isNullOrEmpty()) {
333+
throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.")
335334
}
336335

337-
val contentType = if (headers[ContentType] != null) {
336+
val contentType = if (headers[CONTENT_TYPE] != null) {
338337
// TODO: support multiple contentType options here.
339-
(headers[ContentType] as String).substringBefore(";").lowercase(Locale.US)
338+
(headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US)
340339
} else {
341340
null
342341
}
@@ -359,7 +358,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
359358

360359
val response = client.newCall(request).execute()
361360

362-
val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US)
361+
val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US)
363362

364363
// TODO: handle specific mapping types. e.g. Map<int, Class<?>>
365364
@Suppress("UNNECESSARY_SAFE_CALL")

samples/client/echo_api/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ class ApiClient(
159159

160160
companion object {
161161
@JvmStatic
162-
protected val baseUrlKey: String = "org.openapitools.client.baseUrl"
162+
protected val BASE_URL_KEY: String = "org.openapitools.client.baseUrl"
163163

164164
@JvmStatic
165165
val defaultBasePath: String by lazy {
166-
System.getProperties().getProperty(baseUrlKey, "http://localhost:3000")
166+
System.getProperties().getProperty(BASE_URL_KEY, "http://localhost:3000")
167167
}
168168
}
169169
}

samples/client/others/kotlin-integer-enum/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ open class DefaultApi(basePath: kotlin.String = defaultBasePath, client: Call.Fa
4141
companion object {
4242
@JvmStatic
4343
val defaultBasePath: String by lazy {
44-
System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost")
44+
System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost")
4545
}
4646
}
4747

0 commit comments

Comments
 (0)