@@ -30,6 +30,7 @@ import java.io.File
30
30
* - `composedArrayAsAny` - By this property array of composed is changed to array of object (kotlin.Any).
31
31
* - `generatePrimitiveTypeAlias` - By this property aliases to primitive are also generated.
32
32
* - `removeMinusTextInHeaderProperty` - By this property you can enable to generate name of header property without text minus if it is present.
33
+ * - `removeOperationParams` - By this property you can remove specific parameters from API operations.
33
34
*
34
35
* @author eMan s.r.o. ([email protected] )
35
36
* @author eMan s.r.o. ([email protected] )
@@ -40,6 +41,7 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
40
41
private var emptyDataClasses = false
41
42
private var composedArrayAsAny = true
42
43
private var generatePrimitiveTypeAlias = false
44
+ private var removeOperationParams: List <String > = emptyList()
43
45
private val numberDataTypes = arrayOf(" kotlin.Short" , " kotlin.Int" , " kotlin.Long" , " kotlin.Float" , " kotlin.Double" )
44
46
45
47
companion object {
@@ -172,14 +174,15 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
172
174
*/
173
175
@Suppress(" UNCHECKED_CAST" )
174
176
override fun postProcessOperationsWithModels (
175
- objs : MutableMap <String ?, Any ?>,
176
- allModels : List <Any ?>?
177
+ objs : MutableMap <String ?, Any ?>,
178
+ allModels : List <Any ?>?
177
179
): Map <String , Any >? {
178
180
super .postProcessOperationsWithModels(objs, allModels)
179
181
val operations = objs[" operations" ] as ? Map <String , Any >?
180
182
if (operations != null ) {
181
183
(operations[" operation" ] as List <* >? )?.forEach { operation ->
182
184
if (operation is CodegenOperation ) {
185
+ filterOperationParams(operation)
183
186
if (operation.hasConsumes) {
184
187
if (isMultipartType(operation.consumes)) {
185
188
operation.isMultipart = true
@@ -226,8 +229,6 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
226
229
* @since 2.0.0
227
230
*/
228
231
private fun initTemplates () {
229
- // templateDir = "kotlin-client-v2"
230
- // embeddedTemplateDir = templateDir
231
232
modelTemplateFiles[" model.mustache" ] = " .kt"
232
233
modelDocTemplateFiles[" model_doc.mustache" ] = " .md"
233
234
@@ -252,6 +253,7 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
252
253
initSettingsEmptyDataClass()
253
254
initSettingsComposedArrayAny()
254
255
initSettingsGeneratePrimitiveTypeAlias()
256
+ initSettingsRemoveOperationParams()
255
257
}
256
258
257
259
/* *
@@ -277,8 +279,9 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
277
279
private fun initHeaders () {
278
280
val headersCli = CliOption (HEADER_CLI , HEADER_CLI_DESCRIPTION )
279
281
val headersOptions = HashMap <String , String >()
280
- headersOptions[HeadersCommands .REMOVE_MINUS_WORD_FROM_PROPERTY .value] = REMOVE_MINUS_TEXT_FROM_HEADER_DESCRIPTION
281
-
282
+ headersOptions[HeadersCommands .REMOVE_MINUS_WORD_FROM_PROPERTY .value] =
283
+ REMOVE_MINUS_TEXT_FROM_HEADER_DESCRIPTION
284
+ headersCli.enum = headersOptions
282
285
cliOptions.add(headersCli)
283
286
}
284
287
@@ -290,11 +293,11 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
290
293
*/
291
294
private fun initSettingsEmptyDataClass () {
292
295
cliOptions.add(
293
- CliOption .newBoolean(
294
- EMPTY_DATA_CLASS ,
295
- EMPTY_DATA_CLASS_DESCRIPTION ,
296
- false
297
- )
296
+ CliOption .newBoolean(
297
+ EMPTY_DATA_CLASS ,
298
+ EMPTY_DATA_CLASS_DESCRIPTION ,
299
+ false
300
+ )
298
301
)
299
302
}
300
303
@@ -305,11 +308,11 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
305
308
*/
306
309
private fun initSettingsComposedArrayAny () {
307
310
cliOptions.add(
308
- CliOption .newBoolean(
309
- COMPOSED_ARRAY_ANY ,
310
- COMPOSED_ARRAY_ANY_DESCRIPTION ,
311
- true
312
- )
311
+ CliOption .newBoolean(
312
+ COMPOSED_ARRAY_ANY ,
313
+ COMPOSED_ARRAY_ANY_DESCRIPTION ,
314
+ true
315
+ )
313
316
)
314
317
}
315
318
@@ -321,24 +324,34 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
321
324
*/
322
325
private fun initSettingsGeneratePrimitiveTypeAlias () {
323
326
cliOptions.add(
324
- CliOption .newBoolean(
325
- GENERATE_PRIMITIVE_TYPE_ALIAS ,
326
- GENERATE_PRIMITIVE_TYPE_ALIAS_DESCRIPTION ,
327
- false
328
- )
327
+ CliOption .newBoolean(
328
+ GENERATE_PRIMITIVE_TYPE_ALIAS ,
329
+ GENERATE_PRIMITIVE_TYPE_ALIAS_DESCRIPTION ,
330
+ false
331
+ )
329
332
)
330
333
}
331
334
335
+ /* *
336
+ * Settings used to to remove parameters from operations. Value should an array/list of strings.
337
+ *
338
+ * @since 2.0.0
339
+ */
340
+ private fun initSettingsRemoveOperationParams () {
341
+ val removeOperationParamsCli = CliOption (REMOVE_OPERATION_PARAMS , REMOVE_OPERATION_PARAMS_DESCRIPTION )
342
+ cliOptions.add(removeOperationParamsCli)
343
+ }
344
+
332
345
/* *
333
346
* Adds additional libraries to this generator: [ROOM] and [ROOM2].
334
347
*
335
348
* @since 2.0.0
336
349
*/
337
350
private fun addLibraries () {
338
351
supportedLibraries[ROOM ] =
339
- " Platform: Room v1. JSON processing: Moshi 1.9.2."
352
+ " Platform: Room v1. JSON processing: Moshi 1.9.2."
340
353
supportedLibraries[ROOM2 ] =
341
- " Platform: Room v2 (androidx). JSON processing: Moshi 1.9.2."
354
+ " Platform: Room v2 (androidx). JSON processing: Moshi 1.9.2."
342
355
343
356
val libraryOption = CliOption (CodegenConstants .LIBRARY , " Library template (sub-template) to use" )
344
357
libraryOption.enum = supportedLibraries
@@ -399,6 +412,26 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
399
412
if (additionalProperties.containsKey(CodegenConstants .MODEL_NAME_SUFFIX )) {
400
413
setModelNameSuffix(additionalProperties[CodegenConstants .MODEL_NAME_SUFFIX ] as String? )
401
414
}
415
+
416
+ if (additionalProperties.containsKey(REMOVE_OPERATION_PARAMS )) {
417
+ removeOperationParams = when (val tempArray = additionalProperties[REMOVE_OPERATION_PARAMS ]) {
418
+ is Array <* > -> tempArray.mapNotNull { mapAnyToStringOrNull(it) }
419
+ is List <* > -> tempArray.mapNotNull { mapAnyToStringOrNull(it) }
420
+ else -> emptyList()
421
+ }
422
+ }
423
+ }
424
+
425
+ /* *
426
+ * Maps [Any]? value to [String]?.
427
+ *
428
+ * @param value to be mapped to [String]?
429
+ * @since 2.0.0
430
+ */
431
+ private fun mapAnyToStringOrNull (value : Any? ) : String? = if (value is String ) {
432
+ value
433
+ } else {
434
+ null
402
435
}
403
436
404
437
/* *
@@ -414,8 +447,8 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
414
447
if (! emptyDataClasses) {
415
448
schema?.let {
416
449
if (it !is ArraySchema && it !is MapSchema && it !is ComposedSchema
417
- && (it.type == null || it.type.isEmpty())
418
- && (it.properties == null || it.properties.isEmpty())
450
+ && (it.type == null || it.type.isEmpty())
451
+ && (it.properties == null || it.properties.isEmpty())
419
452
) {
420
453
logger.info(" Schema: $name re-typed to \" string\" " )
421
454
it.type = " string"
@@ -490,7 +523,19 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
490
523
private fun escapePropertyBaseNameLiteral (modelProperties : List <CodegenProperty >) {
491
524
modelProperties.forEach { property ->
492
525
property.vendorExtensions[VENDOR_EXTENSION_BASE_NAME_LITERAL ] =
493
- property.baseName.replace(" $" , " \\ $" )
526
+ property.baseName.replace(" $" , " \\ $" )
527
+ }
528
+ }
529
+
530
+ /* *
531
+ * Filters out operation params if their base name is contained in [removeOperationParams] list.
532
+ *
533
+ * @param operation to have params filtered
534
+ * @since 2.0.0
535
+ */
536
+ private fun filterOperationParams (operation : CodegenOperation ) {
537
+ if (removeOperationParams.isNotEmpty()) {
538
+ operation.allParams.removeIf { removeOperationParams.contains(it.baseName) }
494
539
}
495
540
}
496
541
@@ -505,5 +550,4 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
505
550
val firstType = consumes[0 ]
506
551
return " multipart/form-data" == firstType[" mediaType" ]
507
552
}
508
-
509
- }
553
+ }
0 commit comments