Skip to content

Commit 288d299

Browse files
authored
Merge pull request #4 from eManPrague/feature/minus_lambda
✨ Add possibility to remove minus word from header
2 parents ed33b8e + 6a3711c commit 288d299

File tree

5 files changed

+93
-33
lines changed

5 files changed

+93
-33
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ Change Log
22
==========
33
## 2.0.0-TBD (TBD)
44

5+
### Added
6+
- Introduced a new labda `RemoveMinusTextFromNameLambda` (`{{#lambda.removeMinusText}}{{paramName}}{{/lambda.removeMinusText}}`) which removes word *minus* in a fragment if it's in it.
7+
- New property to remove `minus` word from the header's property name: `additionalProperties["removeMinusTextInHeaderProperty"] = true`. By default `false` will be used.
8+
59
### Fixed
610
- The `modelNameSuffix` has been added twice to generated code. It was caused by migration to OpenApi (this bug has been fixed in OpenApi, so we removed our fix.)
711

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

+51-32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package cz.eman.swagger.codegen.generator.kotlin
22

3+
import com.google.common.collect.ImmutableMap
4+
import com.samskivert.mustache.Mustache
35
import cz.eman.swagger.codegen.language.*
6+
import cz.eman.swagger.codegen.templating.mustache.RemoveMinusTextFromNameLambda
47
import io.swagger.v3.oas.models.media.*
58
import org.openapitools.codegen.*
69
import org.openapitools.codegen.languages.AbstractKotlinCodegen
@@ -26,6 +29,7 @@ import java.io.File
2629
* - `emptyDataClasses` - By this property you can enable empty data classes being generated. (Note: it should not pass Kotlin compilation.)
2730
* - `composedArrayAsAny` - By this property array of composed is changed to array of object (kotlin.Any).
2831
* - `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.
2933
*
3034
* @author eMan s.r.o. ([email protected])
3135
* @author eMan s.r.o. ([email protected])
@@ -53,6 +57,10 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
5357
API("api")
5458
}
5559

60+
enum class HeadersCommands constructor(val value: String) {
61+
REMOVE_MINUS_WORD_FROM_PROPERTY(REMOVE_MINUS_TEXT_FROM_HEADER)
62+
}
63+
5664
/**
5765
* Constructs an instance of `KotlinClientCodegen`.
5866
*/
@@ -61,9 +69,17 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
6169
initArtifact()
6270
initTemplates()
6371
initSettings()
72+
initHeaders()
6473
addLibraries()
6574
}
6675

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+
6783
override fun setLibrary(library: String?) {
6884
super.setLibrary(library)
6985
logger.info("Setting library: $library")
@@ -88,16 +104,6 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
88104
return toModelName(name)
89105
}
90106

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-
101107
override fun toApiName(name: String?): String {
102108
return super.toApiName(name) + apiNameSuffix
103109
}
@@ -166,8 +172,8 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
166172
*/
167173
@Suppress("UNCHECKED_CAST")
168174
override fun postProcessOperationsWithModels(
169-
objs: MutableMap<String?, Any?>,
170-
allModels: List<Any?>?
175+
objs: MutableMap<String?, Any?>,
176+
allModels: List<Any?>?
171177
): Map<String, Any>? {
172178
super.postProcessOperationsWithModels(objs, allModels)
173179
val operations = objs["operations"] as? Map<String, Any>?
@@ -263,6 +269,19 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
263269
cliOptions.add(infrastructureCli)
264270
}
265271

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+
266285
/**
267286
* Settings to allow empty data classes. These are not allowed by default because they do not pass
268287
* kotlin compile. All empty data classes are re-typed to String.
@@ -271,11 +290,11 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
271290
*/
272291
private fun initSettingsEmptyDataClass() {
273292
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+
)
279298
)
280299
}
281300

@@ -286,11 +305,11 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
286305
*/
287306
private fun initSettingsComposedArrayAny() {
288307
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+
)
294313
)
295314
}
296315

@@ -302,11 +321,11 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
302321
*/
303322
private fun initSettingsGeneratePrimitiveTypeAlias() {
304323
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+
)
310329
)
311330
}
312331

@@ -317,9 +336,9 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
317336
*/
318337
private fun addLibraries() {
319338
supportedLibraries[ROOM] =
320-
"Platform: Room v1. JSON processing: Moshi 1.9.2."
339+
"Platform: Room v1. JSON processing: Moshi 1.9.2."
321340
supportedLibraries[ROOM2] =
322-
"Platform: Room v2 (androidx). JSON processing: Moshi 1.9.2."
341+
"Platform: Room v2 (androidx). JSON processing: Moshi 1.9.2."
323342

324343
val libraryOption = CliOption(CodegenConstants.LIBRARY, "Library template (sub-template) to use")
325344
libraryOption.enum = supportedLibraries
@@ -395,8 +414,8 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
395414
if (!emptyDataClasses) {
396415
schema?.let {
397416
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())
400419
) {
401420
logger.info("Schema: $name re-typed to \"string\"")
402421
it.type = "string"
@@ -471,7 +490,7 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
471490
private fun escapePropertyBaseNameLiteral(modelProperties: List<CodegenProperty>) {
472491
modelProperties.forEach { property ->
473492
property.vendorExtensions[VENDOR_EXTENSION_BASE_NAME_LITERAL] =
474-
property.baseName.replace("$", "\\$")
493+
property.baseName.replace("$", "\\$")
475494
}
476495
}
477496

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ const val COMPOSED_ARRAY_ANY_DESCRIPTION =
2626
"Option to cast array of composed schema (Array<OneOf...>) to array of kotlin.Any (Array<kotlin.Any)."
2727

2828
const val GENERATE_PRIMITIVE_TYPE_ALIAS = "generatePrimitiveTypeAlias"
29-
const val GENERATE_PRIMITIVE_TYPE_ALIAS_DESCRIPTION = "Option to generate typealias for primitives."
29+
const val GENERATE_PRIMITIVE_TYPE_ALIAS_DESCRIPTION = "Option to generate typealias for primitives."
30+
31+
const val HEADER_CLI = "headerCliOptions"
32+
const val HEADER_CLI_DESCRIPTION = "Options to generate Header"
33+
const val REMOVE_MINUS_TEXT_FROM_HEADER = "removeMinusTextInHeaderProperty"
34+
const val REMOVE_MINUS_TEXT_FROM_HEADER_DESCRIPTION = "Remove minus text from header's property name if it is present."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cz.eman.swagger.codegen.templating.mustache
2+
3+
import com.samskivert.mustache.Mustache
4+
import com.samskivert.mustache.Template
5+
import org.openapitools.codegen.CodegenConfig
6+
import java.io.Writer
7+
8+
/**
9+
* A lambda which removes word *minus* in a fragment if it's in it.
10+
*
11+
*
12+
* ```mustache
13+
* {{#lambda.removeMinusText}}{{paramName}}{{/lambda.removeMinusText}}
14+
* ```
15+
*
16+
* For instance if _paramName=dogMinusCat_ so result will be _dogCat_
17+
*
18+
* @author eMan s.r.o.
19+
* @since 2.0.0
20+
* @see[Mustache.Lambda]
21+
*/
22+
class RemoveMinusTextFromNameLambda(private val generator: CodegenConfig) : Mustache.Lambda {
23+
24+
companion object {
25+
const val LAMBDA_NAME = "removeMinusText"
26+
}
27+
28+
override fun execute(frag: Template.Fragment, out: Writer) {
29+
out.write(frag.execute().replace("minus", "", true))
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#isHeaderParam}}@Header("{{baseName}}") {{#removeMinusTextInHeaderProperty}}{{#lambda.removeMinusText}}{{paramName}}{{/lambda.removeMinusText}}{{/removeMinusTextInHeaderProperty}}{{^removeMinusTextInHeaderProperty}}{{paramName}}{{/removeMinusTextInHeaderProperty}}: {{{dataType}}}{{/isHeaderParam}}

0 commit comments

Comments
 (0)