@goast/kotlin 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,30 @@
1
+ package @PACKAGE_NAME@
2
+
3
+ typealias MultiValueMap = MutableMap<String, List<String>>
4
+
5
+ fun collectionDelimiter(collectionFormat: String) = when (collectionFormat) {
6
+ "csv" -> ","
7
+ "tsv" -> "\t"
8
+ "pipe" -> "|"
9
+ "space" -> " "
10
+ else -> ""
11
+ }
12
+
13
+ val defaultMultiValueConverter: (item: Any?) -> String = { item -> "$item" }
14
+
15
+ fun <T : Any?> toMultiValue(
16
+ items: Array<T>,
17
+ collectionFormat: String,
18
+ map: (item: T) -> String = defaultMultiValueConverter
19
+ ) = toMultiValue(items.asIterable(), collectionFormat, map)
20
+
21
+ fun <T : Any?> toMultiValue(
22
+ items: Iterable<T>,
23
+ collectionFormat: String,
24
+ map: (item: T) -> String = defaultMultiValueConverter
25
+ ): List<String> {
26
+ return when (collectionFormat) {
27
+ "multi" -> items.map(map)
28
+ else -> listOf(items.joinToString(separator = collectionDelimiter(collectionFormat), transform = map))
29
+ }
30
+ }
@@ -0,0 +1,248 @@
1
+ package @PACKAGE_NAME@
2
+
3
+ import com.fasterxml.jackson.core.type.TypeReference
4
+ import okhttp3.FormBody
5
+ import okhttp3.Headers.Companion.toHeaders
6
+ import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
7
+ import okhttp3.MediaType.Companion.toMediaTypeOrNull
8
+ import okhttp3.MultipartBody
9
+ import okhttp3.OkHttpClient
10
+ import okhttp3.Request
11
+ import okhttp3.RequestBody
12
+ import okhttp3.RequestBody.Companion.asRequestBody
13
+ import okhttp3.RequestBody.Companion.toRequestBody
14
+ import okhttp3.ResponseBody
15
+ import okhttp3.internal.EMPTY_REQUEST
16
+ import java.io.File
17
+ import java.net.URLConnection
18
+ import java.time.LocalDate
19
+ import java.time.LocalDateTime
20
+ import java.time.LocalTime
21
+ import java.time.OffsetDateTime
22
+ import java.time.OffsetTime
23
+ import java.util.Locale
24
+
25
+ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClient) {
26
+ companion object {
27
+ protected const val ContentType = "Content-Type"
28
+ protected const val Accept = "Accept"
29
+ protected const val Authorization = "Authorization"
30
+ protected const val JsonMediaType = "application/json"
31
+ protected const val FormDataMediaType = "multipart/form-data"
32
+ protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
33
+ protected const val XmlMediaType = "application/xml"
34
+
35
+ val apiKey: MutableMap<String, String> = mutableMapOf()
36
+ val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
37
+ var username: String? = null
38
+ var password: String? = null
39
+ var accessToken: String? = null
40
+ const val baseUrlKey = "org.openapitools.client.baseUrl"
41
+
42
+ @JvmStatic
43
+ val defaultClient: OkHttpClient by lazy {
44
+ builder.build()
45
+ }
46
+
47
+ @JvmStatic
48
+ val builder: OkHttpClient.Builder = OkHttpClient.Builder()
49
+ }
50
+
51
+ /**
52
+ * Guess Content-Type header from the given file (defaults to "application/octet-stream").
53
+ *
54
+ * @param file The given file
55
+ * @return The guessed Content-Type
56
+ */
57
+ protected fun guessContentTypeFromFile(file: File): String {
58
+ val contentType = URLConnection.guessContentTypeFromName(file.name)
59
+ return contentType ?: "application/octet-stream"
60
+ }
61
+
62
+ protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
63
+ when {
64
+ content is File -> content.asRequestBody(
65
+ (mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()
66
+ )
67
+
68
+ mediaType == FormDataMediaType ->
69
+ MultipartBody.Builder()
70
+ .setType(MultipartBody.FORM)
71
+ .apply {
72
+ // content's type *must* be Map<String, PartConfig<*>>
73
+ @Suppress("UNCHECKED_CAST")
74
+ (content as Map<String, PartConfig<*>>).forEach { (name, part) ->
75
+ if (part.body is File) {
76
+ val partHeaders = part.headers.toMutableMap() +
77
+ ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
78
+ val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
79
+ addPart(
80
+ partHeaders.toHeaders(),
81
+ part.body.asRequestBody(fileMediaType)
82
+ )
83
+ } else {
84
+ val partHeaders = part.headers.toMutableMap() +
85
+ ("Content-Disposition" to "form-data; name=\"$name\"")
86
+ addPart(
87
+ partHeaders.toHeaders(),
88
+ parameterToString(part.body).toRequestBody(null)
89
+ )
90
+ }
91
+ }
92
+ }.build()
93
+
94
+ mediaType == FormUrlEncMediaType -> {
95
+ FormBody.Builder().apply {
96
+ // content's type *must* be Map<String, PartConfig<*>>
97
+ @Suppress("UNCHECKED_CAST")
98
+ (content as Map<String, PartConfig<*>>).forEach { (name, part) ->
99
+ add(name, parameterToString(part.body))
100
+ }
101
+ }.build()
102
+ }
103
+
104
+ mediaType == null || mediaType.startsWith("application/") && mediaType.endsWith("json") ->
105
+ if (content == null) {
106
+ EMPTY_REQUEST
107
+ } else {
108
+ Serializer.jacksonObjectMapper.writeValueAsString(content)
109
+ .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
110
+ }
111
+
112
+ mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
113
+ // TODO: this should be extended with other serializers
114
+ else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
115
+ }
116
+
117
+ protected inline fun <reified T : Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
118
+ if (body == null) {
119
+ return null
120
+ }
121
+ if (T::class.java == File::class.java) {
122
+ // return tempFile
123
+ // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options
124
+ val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
125
+ tempFile.deleteOnExit()
126
+ body.byteStream().use { inputStream ->
127
+ tempFile.outputStream().use { tempFileOutputStream ->
128
+ inputStream.copyTo(tempFileOutputStream)
129
+ }
130
+ }
131
+ return tempFile as T
132
+ }
133
+ val bodyContent = body.string()
134
+ if (bodyContent.isEmpty()) {
135
+ return null
136
+ }
137
+ return when {
138
+ mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
139
+ Serializer.jacksonObjectMapper.readValue(bodyContent, object : TypeReference<T>() {})
140
+
141
+ else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
142
+ }
143
+ }
144
+
145
+ protected inline fun <reified I, reified T : Any?> request(requestConfig: RequestConfig<I>): ApiResponse<T?> {
146
+ val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")
147
+
148
+ val url = httpUrl.newBuilder()
149
+ .addEncodedPathSegments(requestConfig.path.trimStart('/'))
150
+ .apply {
151
+ requestConfig.query.forEach { query ->
152
+ query.value.forEach { queryValue ->
153
+ addQueryParameter(query.key, queryValue)
154
+ }
155
+ }
156
+ }.build()
157
+
158
+ // take content-type/accept from spec or set to default (application/json) if not defined
159
+ if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
160
+ requestConfig.headers[ContentType] = JsonMediaType
161
+ }
162
+ if (requestConfig.headers[Accept].isNullOrEmpty()) {
163
+ requestConfig.headers[Accept] = JsonMediaType
164
+ }
165
+ val headers = requestConfig.headers
166
+
167
+ if (headers[Accept].isNullOrEmpty()) {
168
+ throw kotlin.IllegalStateException("Missing Accept header. This is required.")
169
+ }
170
+
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
+ }
177
+
178
+ val request = when (requestConfig.method) {
179
+ RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))
180
+ RequestMethod.GET -> Request.Builder().url(url)
181
+ RequestMethod.HEAD -> Request.Builder().url(url).head()
182
+ RequestMethod.PATCH -> Request.Builder().url(url).patch(requestBody(requestConfig.body, contentType))
183
+ RequestMethod.PUT -> Request.Builder().url(url).put(requestBody(requestConfig.body, contentType))
184
+ RequestMethod.POST -> Request.Builder().url(url).post(requestBody(requestConfig.body, contentType))
185
+ RequestMethod.OPTIONS -> Request.Builder().url(url).method("OPTIONS", null)
186
+ }.apply {
187
+ headers.forEach { header -> addHeader(header.key, header.value) }
188
+ }.build()
189
+
190
+ val response = client.newCall(request).execute()
191
+
192
+ val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault())
193
+
194
+ // TODO: handle specific mapping types. e.g. Map<int, Class<?>>
195
+ return when {
196
+ response.isRedirect -> Redirection(
197
+ response.code,
198
+ response.headers.toMultimap()
199
+ )
200
+
201
+ response.isInformational -> Informational(
202
+ response.message,
203
+ response.code,
204
+ response.headers.toMultimap()
205
+ )
206
+
207
+ response.isSuccessful -> Success(
208
+ responseBody(response.body, accept),
209
+ response.code,
210
+ response.headers.toMultimap()
211
+ )
212
+
213
+ response.isClientError -> ClientError(
214
+ response.message,
215
+ response.body?.string(),
216
+ response.code,
217
+ response.headers.toMultimap()
218
+ )
219
+
220
+ else -> ServerError(
221
+ response.message,
222
+ response.body?.string(),
223
+ response.code,
224
+ response.headers.toMultimap()
225
+ )
226
+ }
227
+ }
228
+
229
+ protected fun parameterToString(value: Any?): String = when (value) {
230
+ null -> ""
231
+ is Array<*> -> toMultiValue(value, "csv").toString()
232
+ is Iterable<*> -> toMultiValue(value, "csv").toString()
233
+ is OffsetDateTime, is OffsetTime, is LocalDateTime, is LocalDate, is LocalTime ->
234
+ parseDateToQueryString(value)
235
+
236
+ else -> value.toString()
237
+ }
238
+
239
+ protected inline fun <reified T : Any> parseDateToQueryString(value: T): String {
240
+ /*
241
+ .replace("\"", "") converts the json object string to an actual string for the query parameter.
242
+ The moshi or gson adapter allows a more generic solution instead of trying to use a native
243
+ formatter. It also easily allows to provide a simple way to define a custom date format pattern
244
+ inside a gson/moshi adapter.
245
+ */
246
+ return Serializer.jacksonObjectMapper.writeValueAsString(value).replace("\"", "")
247
+ }
248
+ }
@@ -0,0 +1,43 @@
1
+ package @PACKAGE_NAME@
2
+
3
+ enum class ResponseType {
4
+ Success, Informational, Redirection, ClientError, ServerError
5
+ }
6
+
7
+ interface Response
8
+
9
+ abstract class ApiResponse<T>(val responseType: ResponseType) : Response {
10
+ abstract val statusCode: Int
11
+ abstract val headers: Map<String, List<String>>
12
+ }
13
+
14
+ class Success<T>(
15
+ val data: T,
16
+ override val statusCode: Int = -1,
17
+ override val headers: Map<String, List<String>> = mapOf()
18
+ ) : ApiResponse<T>(ResponseType.Success)
19
+
20
+ class Informational<T>(
21
+ val statusText: String,
22
+ override val statusCode: Int = -1,
23
+ override val headers: Map<String, List<String>> = mapOf()
24
+ ) : ApiResponse<T>(ResponseType.Informational)
25
+
26
+ class Redirection<T>(
27
+ override val statusCode: Int = -1,
28
+ override val headers: Map<String, List<String>> = mapOf()
29
+ ) : ApiResponse<T>(ResponseType.Redirection)
30
+
31
+ class ClientError<T>(
32
+ val message: String? = null,
33
+ val body: Any? = null,
34
+ override val statusCode: Int = -1,
35
+ override val headers: Map<String, List<String>> = mapOf()
36
+ ) : ApiResponse<T>(ResponseType.ClientError)
37
+
38
+ class ServerError<T>(
39
+ val message: String? = null,
40
+ val body: Any? = null,
41
+ override val statusCode: Int = -1,
42
+ override val headers: Map<String, List<String>>
43
+ ) : ApiResponse<T>(ResponseType.ServerError)
@@ -0,0 +1,21 @@
1
+ @file:Suppress("unused")
2
+
3
+ package @PACKAGE_NAME@
4
+
5
+ import java.lang.RuntimeException
6
+
7
+ open class ClientException(message: kotlin.String? = null, val statusCode: Int = -1, val response: Response? = null) :
8
+ RuntimeException(message) {
9
+
10
+ companion object {
11
+ private const val serialVersionUID: Long = 123L
12
+ }
13
+ }
14
+
15
+ open class ServerException(message: kotlin.String? = null, val statusCode: Int = -1, val response: Response? = null) :
16
+ RuntimeException(message) {
17
+
18
+ companion object {
19
+ private const val serialVersionUID: Long = 456L
20
+ }
21
+ }
@@ -0,0 +1,11 @@
1
+ package @PACKAGE_NAME@
2
+
3
+ /**
4
+ * Defines a config object for a given part of a multi-part request.
5
+ * NOTE: Headers is a Map<String,String> because rfc2616 defines
6
+ * multi-valued headers as csv-only.
7
+ */
8
+ data class PartConfig<T>(
9
+ val headers: MutableMap<String, String> = mutableMapOf(),
10
+ val body: T? = null
11
+ )
@@ -0,0 +1,18 @@
1
+ package @PACKAGE_NAME@
2
+
3
+ /**
4
+ * Defines a config object for a given request.
5
+ * NOTE: This object doesn't include 'body' because it
6
+ * allows for caching of the constructed object
7
+ * for many request definitions.
8
+ * NOTE: Headers is a Map<String,String> because rfc2616 defines
9
+ * multi-valued headers as csv-only.
10
+ */
11
+ data class RequestConfig<T>(
12
+ val method: RequestMethod,
13
+ val path: String,
14
+ val headers: MutableMap<String, String> = mutableMapOf(),
15
+ val query: MutableMap<String, List<String>> = mutableMapOf(),
16
+ val requiresAuthentication: Boolean,
17
+ val body: T? = null
18
+ )
@@ -0,0 +1,8 @@
1
+ package @PACKAGE_NAME@
2
+
3
+ /**
4
+ * Provides enumerated HTTP verbs
5
+ */
6
+ enum class RequestMethod {
7
+ GET, DELETE, HEAD, OPTIONS, PATCH, POST, PUT
8
+ }
@@ -0,0 +1,24 @@
1
+ package @PACKAGE_NAME@
2
+
3
+ import okhttp3.Response
4
+
5
+ /**
6
+ * Provides an extension to evaluation whether the response is a 1xx code
7
+ */
8
+ val Response.isInformational: Boolean get() = this.code in 100..199
9
+
10
+ /**
11
+ * Provides an extension to evaluation whether the response is a 3xx code
12
+ */
13
+ @Suppress("EXTENSION_SHADOWED_BY_MEMBER")
14
+ val Response.isRedirect: Boolean get() = this.code in 300..399
15
+
16
+ /**
17
+ * Provides an extension to evaluation whether the response is a 4xx code
18
+ */
19
+ val Response.isClientError: Boolean get() = this.code in 400..499
20
+
21
+ /**
22
+ * Provides an extension to evaluation whether the response is a 5xx (Standard) through 999 (non-standard) code
23
+ */
24
+ val Response.isServerError: Boolean get() = this.code in 500..999
@@ -0,0 +1,14 @@
1
+ package @PACKAGE_NAME@
2
+
3
+ import com.fasterxml.jackson.annotation.JsonInclude
4
+ import com.fasterxml.jackson.databind.ObjectMapper
5
+ import com.fasterxml.jackson.databind.SerializationFeature
6
+ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
7
+
8
+ object Serializer {
9
+ @JvmStatic
10
+ val jacksonObjectMapper: ObjectMapper = jacksonObjectMapper()
11
+ .findAndRegisterModules()
12
+ .setSerializationInclusion(JsonInclude.Include.NON_ABSENT)
13
+ .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
14
+ }
@@ -51,6 +51,11 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
51
51
  }
52
52
  generateFileContent(ctx, builder) {
53
53
  let schema = ctx.schema;
54
+ if (schema.kind === 'oneOf') {
55
+ schema = ctx.config.oneOfBehavior === 'treat-as-any-of'
56
+ ? Object.assign(Object.assign({}, schema), { kind: 'combined', anyOf: schema.oneOf, allOf: [], oneOf: undefined }) : Object.assign(Object.assign({}, schema), { kind: 'combined', allOf: schema.oneOf, anyOf: [], oneOf: undefined });
57
+ ctx.schema = schema;
58
+ }
54
59
  if (schema.kind === 'object' || schema.kind === 'combined') {
55
60
  const mergedSchema = (0, core_1.resolveAnyOfAndAllOf)(schema, true);
56
61
  if (mergedSchema) {
@@ -2,4 +2,4 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.defaultKotlinModelsGeneratorConfig = void 0;
4
4
  const config_1 = require("../../config");
5
- exports.defaultKotlinModelsGeneratorConfig = Object.assign(Object.assign({}, config_1.defaultKotlinGeneratorConfig), { packageName: 'com.openapi.generated', packageSuffix: '.model' });
5
+ exports.defaultKotlinModelsGeneratorConfig = Object.assign(Object.assign({}, config_1.defaultKotlinGeneratorConfig), { packageName: 'com.openapi.generated', packageSuffix: '.model', oneOfBehavior: 'treat-as-any-of' });
@@ -37,7 +37,6 @@ class KotlinOkHttp3ClientsGenerator extends core_1.OpenApiServicesGenerationProv
37
37
  });
38
38
  }
39
39
  getInfrastructurePackageName(config) {
40
- console.log(config);
41
40
  if (typeof config.infrastructurePackageName === 'string') {
42
41
  return config.infrastructurePackageName;
43
42
  }
@@ -48,6 +48,11 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
48
48
  }
49
49
  generateFileContent(ctx, builder) {
50
50
  let schema = ctx.schema;
51
+ if (schema.kind === 'oneOf') {
52
+ schema = ctx.config.oneOfBehavior === 'treat-as-any-of'
53
+ ? Object.assign(Object.assign({}, schema), { kind: 'combined', anyOf: schema.oneOf, allOf: [], oneOf: undefined }) : Object.assign(Object.assign({}, schema), { kind: 'combined', allOf: schema.oneOf, anyOf: [], oneOf: undefined });
54
+ ctx.schema = schema;
55
+ }
51
56
  if (schema.kind === 'object' || schema.kind === 'combined') {
52
57
  const mergedSchema = resolveAnyOfAndAllOf(schema, true);
53
58
  if (mergedSchema) {
@@ -1,2 +1,2 @@
1
1
  import { defaultKotlinGeneratorConfig } from '../../config';
2
- export const defaultKotlinModelsGeneratorConfig = Object.assign(Object.assign({}, defaultKotlinGeneratorConfig), { packageName: 'com.openapi.generated', packageSuffix: '.model' });
2
+ export const defaultKotlinModelsGeneratorConfig = Object.assign(Object.assign({}, defaultKotlinGeneratorConfig), { packageName: 'com.openapi.generated', packageSuffix: '.model', oneOfBehavior: 'treat-as-any-of' });
@@ -34,7 +34,6 @@ export class KotlinOkHttp3ClientsGenerator extends OpenApiServicesGenerationProv
34
34
  });
35
35
  }
36
36
  getInfrastructurePackageName(config) {
37
- console.log(config);
38
37
  if (typeof config.infrastructurePackageName === 'string') {
39
38
  return config.infrastructurePackageName;
40
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goast/kotlin",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "repository": "https://github.com/MaSch0212/goast.git",
5
5
  "author": {
6
6
  "name": "Marc Schmidt (MaSch0212)",
@@ -4,6 +4,7 @@ import { KotlinGeneratorConfig } from '../../config';
4
4
  export type KotlinModelsGeneratorConfig = KotlinGeneratorConfig & {
5
5
  packageName: string;
6
6
  packageSuffix: string;
7
+ oneOfBehavior: 'treat-as-any-of' | 'treat-as-all-of';
7
8
  };
8
9
  export declare const defaultKotlinModelsGeneratorConfig: DefaultGenerationProviderConfig<KotlinModelsGeneratorConfig>;
9
10
  export type KotlinModelsGeneratorInput = {};