1
1
package cz.eman.swagger.codegen.generator.kotlin
2
2
3
+ import com.google.common.collect.ImmutableMap
4
+ import com.samskivert.mustache.Mustache
3
5
import cz.eman.swagger.codegen.language.*
6
+ import cz.eman.swagger.codegen.templating.mustache.RemoveMinusTextFromNameLambda
4
7
import io.swagger.v3.oas.models.media.*
5
8
import org.openapitools.codegen.*
6
9
import org.openapitools.codegen.languages.AbstractKotlinCodegen
@@ -26,6 +29,7 @@ import java.io.File
26
29
* - `emptyDataClasses` - By this property you can enable empty data classes being generated. (Note: it should not pass Kotlin compilation.)
27
30
* - `composedArrayAsAny` - By this property array of composed is changed to array of object (kotlin.Any).
28
31
* - `generatePrimitiveTypeAlias` - By this property aliases to primitive are also generated.
32
+ * - `removeMinusTextInHeaderProperty` - By this property you can enable to generate name of header property without text minus if it is present.
29
33
*
30
34
* @author eMan s.r.o. ([email protected] )
31
35
* @author eMan s.r.o. ([email protected] )
@@ -53,6 +57,10 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
53
57
API (" api" )
54
58
}
55
59
60
+ enum class HeadersCommands constructor(val value : String ) {
61
+ REMOVE_MINUS_WORD_FROM_PROPERTY (REMOVE_MINUS_TEXT_FROM_HEADER )
62
+ }
63
+
56
64
/* *
57
65
* Constructs an instance of `KotlinClientCodegen`.
58
66
*/
@@ -61,9 +69,17 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
61
69
initArtifact()
62
70
initTemplates()
63
71
initSettings()
72
+ initHeaders()
64
73
addLibraries()
65
74
}
66
75
76
+ override fun addMustacheLambdas (): ImmutableMap .Builder <String , Mustache .Lambda > {
77
+ val lambdas = super .addMustacheLambdas()
78
+ lambdas.put(RemoveMinusTextFromNameLambda .LAMBDA_NAME , RemoveMinusTextFromNameLambda (this ))
79
+
80
+ return lambdas
81
+ }
82
+
67
83
override fun setLibrary (library : String? ) {
68
84
super .setLibrary(library)
69
85
logger.info(" Setting library: $library " )
@@ -88,16 +104,6 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
88
104
return toModelName(name)
89
105
}
90
106
91
- override fun toModelName (name : String ): String {
92
- return super .toModelName(name)
93
- /* val modelName = super.toModelName(name)
94
- return if (modelName.startsWith("kotlin.") || modelName.startsWith("java.")) {
95
- modelName
96
- } else {
97
- "$modelNamePrefix$modelName$modelNameSuffix"
98
- }*/
99
- }
100
-
101
107
override fun toApiName (name : String? ): String {
102
108
return super .toApiName(name) + apiNameSuffix
103
109
}
@@ -166,8 +172,8 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
166
172
*/
167
173
@Suppress(" UNCHECKED_CAST" )
168
174
override fun postProcessOperationsWithModels (
169
- objs : MutableMap <String ?, Any ?>,
170
- allModels : List <Any ?>?
175
+ objs : MutableMap <String ?, Any ?>,
176
+ allModels : List <Any ?>?
171
177
): Map <String , Any >? {
172
178
super .postProcessOperationsWithModels(objs, allModels)
173
179
val operations = objs[" operations" ] as ? Map <String , Any >?
@@ -263,6 +269,19 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
263
269
cliOptions.add(infrastructureCli)
264
270
}
265
271
272
+ /* *
273
+ * Adds all headers options to this generator
274
+ *
275
+ * @since 2.0.0
276
+ */
277
+ private fun initHeaders () {
278
+ val headersCli = CliOption (HEADER_CLI , HEADER_CLI_DESCRIPTION )
279
+ val headersOptions = HashMap <String , String >()
280
+ headersOptions[HeadersCommands .REMOVE_MINUS_WORD_FROM_PROPERTY .value] = REMOVE_MINUS_TEXT_FROM_HEADER_DESCRIPTION
281
+
282
+ cliOptions.add(headersCli)
283
+ }
284
+
266
285
/* *
267
286
* Settings to allow empty data classes. These are not allowed by default because they do not pass
268
287
* kotlin compile. All empty data classes are re-typed to String.
@@ -271,11 +290,11 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
271
290
*/
272
291
private fun initSettingsEmptyDataClass () {
273
292
cliOptions.add(
274
- CliOption .newBoolean(
275
- EMPTY_DATA_CLASS ,
276
- EMPTY_DATA_CLASS_DESCRIPTION ,
277
- false
278
- )
293
+ CliOption .newBoolean(
294
+ EMPTY_DATA_CLASS ,
295
+ EMPTY_DATA_CLASS_DESCRIPTION ,
296
+ false
297
+ )
279
298
)
280
299
}
281
300
@@ -286,11 +305,11 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
286
305
*/
287
306
private fun initSettingsComposedArrayAny () {
288
307
cliOptions.add(
289
- CliOption .newBoolean(
290
- COMPOSED_ARRAY_ANY ,
291
- COMPOSED_ARRAY_ANY_DESCRIPTION ,
292
- true
293
- )
308
+ CliOption .newBoolean(
309
+ COMPOSED_ARRAY_ANY ,
310
+ COMPOSED_ARRAY_ANY_DESCRIPTION ,
311
+ true
312
+ )
294
313
)
295
314
}
296
315
@@ -302,11 +321,11 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
302
321
*/
303
322
private fun initSettingsGeneratePrimitiveTypeAlias () {
304
323
cliOptions.add(
305
- CliOption .newBoolean(
306
- GENERATE_PRIMITIVE_TYPE_ALIAS ,
307
- GENERATE_PRIMITIVE_TYPE_ALIAS_DESCRIPTION ,
308
- false
309
- )
324
+ CliOption .newBoolean(
325
+ GENERATE_PRIMITIVE_TYPE_ALIAS ,
326
+ GENERATE_PRIMITIVE_TYPE_ALIAS_DESCRIPTION ,
327
+ false
328
+ )
310
329
)
311
330
}
312
331
@@ -317,9 +336,9 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
317
336
*/
318
337
private fun addLibraries () {
319
338
supportedLibraries[ROOM ] =
320
- " Platform: Room v1. JSON processing: Moshi 1.9.2."
339
+ " Platform: Room v1. JSON processing: Moshi 1.9.2."
321
340
supportedLibraries[ROOM2 ] =
322
- " Platform: Room v2 (androidx). JSON processing: Moshi 1.9.2."
341
+ " Platform: Room v2 (androidx). JSON processing: Moshi 1.9.2."
323
342
324
343
val libraryOption = CliOption (CodegenConstants .LIBRARY , " Library template (sub-template) to use" )
325
344
libraryOption.enum = supportedLibraries
@@ -395,8 +414,8 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
395
414
if (! emptyDataClasses) {
396
415
schema?.let {
397
416
if (it !is ArraySchema && it !is MapSchema && it !is ComposedSchema
398
- && (it.type == null || it.type.isEmpty())
399
- && (it.properties == null || it.properties.isEmpty())
417
+ && (it.type == null || it.type.isEmpty())
418
+ && (it.properties == null || it.properties.isEmpty())
400
419
) {
401
420
logger.info(" Schema: $name re-typed to \" string\" " )
402
421
it.type = " string"
@@ -471,7 +490,7 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
471
490
private fun escapePropertyBaseNameLiteral (modelProperties : List <CodegenProperty >) {
472
491
modelProperties.forEach { property ->
473
492
property.vendorExtensions[VENDOR_EXTENSION_BASE_NAME_LITERAL ] =
474
- property.baseName.replace(" $" , " \\ $" )
493
+ property.baseName.replace(" $" , " \\ $" )
475
494
}
476
495
}
477
496
0 commit comments