Skip to content

Commit d7138df

Browse files
authored
Merge pull request #9 from Del-S/feature/ignore_headers
✨ Added option to remove operation parameter
2 parents ba66693 + d38858b commit d7138df

File tree

5 files changed

+83
-29
lines changed

5 files changed

+83
-29
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Change Log
22
==========
33

4+
## 2.0.0-rc02 (TBD)
5+
6+
### Added
7+
- Option to remove specific parameters from operations.
8+
49
## 2.0.0-rc01 (2019-02-05)
510

611
### Changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ configure<SwaggerCodeGenConfig> {
5656
"generatePrimitiveTypeAlias" to false,
5757
"apiPackage" to "cz.mypackage.service",
5858
"modelPackage" to "cz.mypackage.model"
59+
"removeOperationParams" to arrayOf("X-Access-Token", "Accept-Language", ...)
5960
)
6061
)
6162

@@ -87,6 +88,7 @@ configure<SwaggerCodeGenConfig> {
8788
- `apiPackage` - By this property you can define a package name for your service classes
8889
- `modelPackage` - By this property you can define a package name for your model classes
8990
- `removeMinusTextInHeaderProperty` - By this property you can enable to generate name of header property without text minus if it is present.
91+
- `removeOperationParams` - By this property you can remove specific parameters from API operations.
9092

9193
Other options can be found [here](https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-maven-plugin/README.md).
9294

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
org.gradle.jvmargs=-Xmx1536m
22

3-
version=2.0.0-rc01
3+
version=2.0.0-rc02

lib/src/main/kotlin/cz/eman/swagger/codegen/generator/kotlin/KotlinClientCodegen.kt

+72-28
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import java.io.File
3030
* - `composedArrayAsAny` - By this property array of composed is changed to array of object (kotlin.Any).
3131
* - `generatePrimitiveTypeAlias` - By this property aliases to primitive are also generated.
3232
* - `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.
3334
*
3435
* @author eMan s.r.o. ([email protected])
3536
* @author eMan s.r.o. ([email protected])
@@ -40,6 +41,7 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
4041
private var emptyDataClasses = false
4142
private var composedArrayAsAny = true
4243
private var generatePrimitiveTypeAlias = false
44+
private var removeOperationParams: List<String> = emptyList()
4345
private val numberDataTypes = arrayOf("kotlin.Short", "kotlin.Int", "kotlin.Long", "kotlin.Float", "kotlin.Double")
4446

4547
companion object {
@@ -172,14 +174,15 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
172174
*/
173175
@Suppress("UNCHECKED_CAST")
174176
override fun postProcessOperationsWithModels(
175-
objs: MutableMap<String?, Any?>,
176-
allModels: List<Any?>?
177+
objs: MutableMap<String?, Any?>,
178+
allModels: List<Any?>?
177179
): Map<String, Any>? {
178180
super.postProcessOperationsWithModels(objs, allModels)
179181
val operations = objs["operations"] as? Map<String, Any>?
180182
if (operations != null) {
181183
(operations["operation"] as List<*>?)?.forEach { operation ->
182184
if (operation is CodegenOperation) {
185+
filterOperationParams(operation)
183186
if (operation.hasConsumes) {
184187
if (isMultipartType(operation.consumes)) {
185188
operation.isMultipart = true
@@ -226,8 +229,6 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
226229
* @since 2.0.0
227230
*/
228231
private fun initTemplates() {
229-
// templateDir = "kotlin-client-v2"
230-
// embeddedTemplateDir = templateDir
231232
modelTemplateFiles["model.mustache"] = ".kt"
232233
modelDocTemplateFiles["model_doc.mustache"] = ".md"
233234

@@ -252,6 +253,7 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
252253
initSettingsEmptyDataClass()
253254
initSettingsComposedArrayAny()
254255
initSettingsGeneratePrimitiveTypeAlias()
256+
initSettingsRemoveOperationParams()
255257
}
256258

257259
/**
@@ -277,8 +279,9 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
277279
private fun initHeaders() {
278280
val headersCli = CliOption(HEADER_CLI, HEADER_CLI_DESCRIPTION)
279281
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
282285
cliOptions.add(headersCli)
283286
}
284287

@@ -290,11 +293,11 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
290293
*/
291294
private fun initSettingsEmptyDataClass() {
292295
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+
)
298301
)
299302
}
300303

@@ -305,11 +308,11 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
305308
*/
306309
private fun initSettingsComposedArrayAny() {
307310
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+
)
313316
)
314317
}
315318

@@ -321,24 +324,34 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
321324
*/
322325
private fun initSettingsGeneratePrimitiveTypeAlias() {
323326
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+
)
329332
)
330333
}
331334

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+
332345
/**
333346
* Adds additional libraries to this generator: [ROOM] and [ROOM2].
334347
*
335348
* @since 2.0.0
336349
*/
337350
private fun addLibraries() {
338351
supportedLibraries[ROOM] =
339-
"Platform: Room v1. JSON processing: Moshi 1.9.2."
352+
"Platform: Room v1. JSON processing: Moshi 1.9.2."
340353
supportedLibraries[ROOM2] =
341-
"Platform: Room v2 (androidx). JSON processing: Moshi 1.9.2."
354+
"Platform: Room v2 (androidx). JSON processing: Moshi 1.9.2."
342355

343356
val libraryOption = CliOption(CodegenConstants.LIBRARY, "Library template (sub-template) to use")
344357
libraryOption.enum = supportedLibraries
@@ -399,6 +412,26 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
399412
if (additionalProperties.containsKey(CodegenConstants.MODEL_NAME_SUFFIX)) {
400413
setModelNameSuffix(additionalProperties[CodegenConstants.MODEL_NAME_SUFFIX] as String?)
401414
}
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
402435
}
403436

404437
/**
@@ -414,8 +447,8 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
414447
if (!emptyDataClasses) {
415448
schema?.let {
416449
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())
419452
) {
420453
logger.info("Schema: $name re-typed to \"string\"")
421454
it.type = "string"
@@ -490,7 +523,19 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
490523
private fun escapePropertyBaseNameLiteral(modelProperties: List<CodegenProperty>) {
491524
modelProperties.forEach { property ->
492525
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) }
494539
}
495540
}
496541

@@ -505,5 +550,4 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
505550
val firstType = consumes[0]
506551
return "multipart/form-data" == firstType["mediaType"]
507552
}
508-
509-
}
553+
}

lib/src/main/kotlin/cz/eman/swagger/codegen/language/CodegenConst.kt

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const val COMPOSED_ARRAY_ANY_DESCRIPTION =
2828
const val GENERATE_PRIMITIVE_TYPE_ALIAS = "generatePrimitiveTypeAlias"
2929
const val GENERATE_PRIMITIVE_TYPE_ALIAS_DESCRIPTION = "Option to generate typealias for primitives."
3030

31+
const val REMOVE_OPERATION_PARAMS = "removeOperationParams"
32+
const val REMOVE_OPERATION_PARAMS_DESCRIPTION = "Option to remove specific parameters from operation. Uses base name to filter the parameters."
33+
3134
const val HEADER_CLI = "headerCliOptions"
3235
const val HEADER_CLI_DESCRIPTION = "Options to generate Header"
3336
const val REMOVE_MINUS_TEXT_FROM_HEADER = "removeMinusTextInHeaderProperty"

0 commit comments

Comments
 (0)