Skip to content

Commit 6bc629e

Browse files
committed
✨ Add a possibility to ignore API starting slash
Added a new generator additional parameter called ignoreEndpointStartingSlash which will ignore a starting slash from a retrofit API PATH definition
1 parent 796d04d commit 6bc629e

File tree

7 files changed

+87
-29
lines changed

7 files changed

+87
-29
lines changed

CHANGELOG.md

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

4+
## 2.0.0 (2020-03-09)
5+
- Introduced a new config parameter `ignoreEndpointStartingSlash` to ignore endpoint's starting slash in generated
6+
Retrofit API service class. See `IgnoreStartingSlashLambda` for more details.
7+
- Introduced a new lambda `IgnoreStartingSlashLambda` (`{{#lambda.ignoreStartingSlash}}{{paramName}}{{/lambda.ignoreStartingSlash}}`)
8+
which removes word *minus* in a fragment if it's in it.
9+
- For rest of changes see all `2.0.0-xx` changelog messages.
10+
411
## 2.0.0-rc03 (2020-02-18)
512

613
### Fixed
@@ -26,7 +33,7 @@ Change Log
2633
## 2.0.0-alpha02 (2019-01-21)
2734

2835
### Added
29-
- Introduced a new labda `RemoveMinusTextFromNameLambda` (`{{#lambda.removeMinusText}}{{paramName}}{{/lambda.removeMinusText}}`) which removes word *minus* in a fragment if it's in it.
36+
- Introduced a new lambda `RemoveMinusTextFromNameLambda` (`{{#lambda.removeMinusText}}{{paramName}}{{/lambda.removeMinusText}}`) which removes word *minus* in a fragment if it's in it.
3037
- New property to remove `minus` word from the header's property name: `additionalProperties["removeMinusTextInHeaderProperty"] = true`. By default `false` will be used.
3138

3239
### Fixed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# OpenApi 3 Codegen / Swagger
22

3-
[ ![Download](https://api.bintray.com/packages/emanprague/maven/cz.eman.swagger.codegen/images/download.svg?version=2.0.0-rc01) ](https://bintray.com/emanprague/maven/cz.eman.swagger.codegen/2.0.0-rc03/link)
3+
[ ![Download](https://api.bintray.com/packages/emanprague/maven/cz.eman.swagger.codegen/images/download.svg?version=2.0.0) ](https://bintray.com/emanprague/maven/cz.eman.swagger.codegen/2.0.0/link)
44

55
The Swagger codegen contains a template-driven engine to generate documentation, code for Java, Kotlin and Android such like Retrofit and Room. It is a fork of the https://github.com/OpenAPITools/openapi-generator with modifications
66

@@ -17,7 +17,7 @@ buildscript {
1717

1818
// Kotlin Gradle DSL
1919
dependencies {
20-
classpath("cz.eman.swagger:swagger-codegen:2.0.0-rc03")
20+
classpath("cz.eman.swagger:swagger-codegen:2.0.0")
2121
}
2222
}
2323
```
@@ -53,6 +53,7 @@ configure<SwaggerCodeGenConfig> {
5353
"generateAliasAsModel" to true,
5454
"composedArrayAsAny" to true,
5555
"removeMinusTextInHeaderProperty" to true,
56+
"ignoreEndpointStartingSlash" to true,
5657
"generatePrimitiveTypeAlias" to false,
5758
"apiPackage" to "cz.mypackage.service",
5859
"modelPackage" to "cz.mypackage.model"
@@ -89,6 +90,7 @@ configure<SwaggerCodeGenConfig> {
8990
- `modelPackage` - By this property you can define a package name for your model classes
9091
- `removeMinusTextInHeaderProperty` - By this property you can enable to generate name of header property without text minus if it is present.
9192
- `removeOperationParams` - By this property you can remove specific parameters from API operations.
93+
- `ignoreEndpointStartingSlash` - By this property you can ignore a starting slash from an endpoint definition if it is present
9294

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

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-rc03
3+
version=2.0.0

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

+33-23
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cz.eman.swagger.codegen.generator.kotlin
33
import com.google.common.collect.ImmutableMap
44
import com.samskivert.mustache.Mustache
55
import cz.eman.swagger.codegen.language.*
6+
import cz.eman.swagger.codegen.templating.mustache.IgnoreStartingSlashLambda
67
import cz.eman.swagger.codegen.templating.mustache.RemoveMinusTextFromNameLambda
78
import io.swagger.v3.oas.models.media.*
89
import org.openapitools.codegen.*
@@ -31,6 +32,7 @@ import java.io.File
3132
* - `generatePrimitiveTypeAlias` - By this property aliases to primitive are also generated.
3233
* - `removeMinusTextInHeaderProperty` - By this property you can enable to generate name of header property without text minus if it is present.
3334
* - `removeOperationParams` - By this property you can remove specific parameters from API operations.
35+
* - `ignoreEndpointStartingSlash` - By this property you can ignore a starting slash from an endpoint definition if it is present.
3436
*
3537
* @author eMan s.r.o. ([email protected])
3638
* @author eMan s.r.o. ([email protected])
@@ -63,6 +65,10 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
6365
REMOVE_MINUS_WORD_FROM_PROPERTY(REMOVE_MINUS_TEXT_FROM_HEADER)
6466
}
6567

68+
enum class EndpointsCommands constructor(val value: String) {
69+
INGORE_ENDPOINT_STARTING_SLASH(REMOVE_ENDPOINT_STARTING_SLASH)
70+
}
71+
6672
/**
6773
* Constructs an instance of `KotlinClientCodegen`.
6874
*/
@@ -78,6 +84,7 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
7884
override fun addMustacheLambdas(): ImmutableMap.Builder<String, Mustache.Lambda> {
7985
val lambdas = super.addMustacheLambdas()
8086
lambdas.put(RemoveMinusTextFromNameLambda.LAMBDA_NAME, RemoveMinusTextFromNameLambda(this))
87+
lambdas.put(IgnoreStartingSlashLambda.LAMBDA_NAME, IgnoreStartingSlashLambda(this))
8188

8289
return lambdas
8390
}
@@ -174,8 +181,8 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
174181
*/
175182
@Suppress("UNCHECKED_CAST")
176183
override fun postProcessOperationsWithModels(
177-
objs: MutableMap<String?, Any?>,
178-
allModels: List<Any?>?
184+
objs: MutableMap<String?, Any?>,
185+
allModels: List<Any?>?
179186
): Map<String, Any>? {
180187
super.postProcessOperationsWithModels(objs, allModels)
181188
val operations = objs["operations"] as? Map<String, Any>?
@@ -267,7 +274,10 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
267274
val infraOptions = HashMap<String, String>()
268275
infraOptions[GenerateApiType.INFRASTRUCTURE.value] = "Generate Infrastructure API"
269276
infraOptions[GenerateApiType.API.value] = "Generate API"
277+
infraOptions[EndpointsCommands.INGORE_ENDPOINT_STARTING_SLASH.value] =
278+
REMOVE_ENDPOINT_STARTING_SLASH_DESCRIPTION
270279
infrastructureCli.enum = infraOptions
280+
271281
cliOptions.add(infrastructureCli)
272282
}
273283

@@ -280,7 +290,7 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
280290
val headersCli = CliOption(HEADER_CLI, HEADER_CLI_DESCRIPTION)
281291
val headersOptions = HashMap<String, String>()
282292
headersOptions[HeadersCommands.REMOVE_MINUS_WORD_FROM_PROPERTY.value] =
283-
REMOVE_MINUS_TEXT_FROM_HEADER_DESCRIPTION
293+
REMOVE_MINUS_TEXT_FROM_HEADER_DESCRIPTION
284294
headersCli.enum = headersOptions
285295
cliOptions.add(headersCli)
286296
}
@@ -293,11 +303,11 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
293303
*/
294304
private fun initSettingsEmptyDataClass() {
295305
cliOptions.add(
296-
CliOption.newBoolean(
297-
EMPTY_DATA_CLASS,
298-
EMPTY_DATA_CLASS_DESCRIPTION,
299-
false
300-
)
306+
CliOption.newBoolean(
307+
EMPTY_DATA_CLASS,
308+
EMPTY_DATA_CLASS_DESCRIPTION,
309+
false
310+
)
301311
)
302312
}
303313

@@ -308,11 +318,11 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
308318
*/
309319
private fun initSettingsComposedArrayAny() {
310320
cliOptions.add(
311-
CliOption.newBoolean(
312-
COMPOSED_ARRAY_ANY,
313-
COMPOSED_ARRAY_ANY_DESCRIPTION,
314-
true
315-
)
321+
CliOption.newBoolean(
322+
COMPOSED_ARRAY_ANY,
323+
COMPOSED_ARRAY_ANY_DESCRIPTION,
324+
true
325+
)
316326
)
317327
}
318328

@@ -324,11 +334,11 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
324334
*/
325335
private fun initSettingsGeneratePrimitiveTypeAlias() {
326336
cliOptions.add(
327-
CliOption.newBoolean(
328-
GENERATE_PRIMITIVE_TYPE_ALIAS,
329-
GENERATE_PRIMITIVE_TYPE_ALIAS_DESCRIPTION,
330-
false
331-
)
337+
CliOption.newBoolean(
338+
GENERATE_PRIMITIVE_TYPE_ALIAS,
339+
GENERATE_PRIMITIVE_TYPE_ALIAS_DESCRIPTION,
340+
false
341+
)
332342
)
333343
}
334344

@@ -349,9 +359,9 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
349359
*/
350360
private fun addLibraries() {
351361
supportedLibraries[ROOM] =
352-
"Platform: Room v1. JSON processing: Moshi 1.9.2."
362+
"Platform: Room v1. JSON processing: Moshi 1.9.2."
353363
supportedLibraries[ROOM2] =
354-
"Platform: Room v2 (androidx). JSON processing: Moshi 1.9.2."
364+
"Platform: Room v2 (androidx). JSON processing: Moshi 1.9.2."
355365

356366
val libraryOption = CliOption(CodegenConstants.LIBRARY, "Library template (sub-template) to use")
357367
libraryOption.enum = supportedLibraries
@@ -447,8 +457,8 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
447457
if (!emptyDataClasses) {
448458
schema?.let {
449459
if (it !is ArraySchema && it !is MapSchema && it !is ComposedSchema
450-
&& (it.type == null || it.type.isEmpty())
451-
&& (it.properties == null || it.properties.isEmpty())
460+
&& (it.type == null || it.type.isEmpty())
461+
&& (it.properties == null || it.properties.isEmpty())
452462
) {
453463
logger.info("Schema: $name re-typed to \"string\"")
454464
it.type = "string"
@@ -523,7 +533,7 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient
523533
private fun escapePropertyBaseNameLiteral(modelProperties: List<CodegenProperty>) {
524534
modelProperties.forEach { property ->
525535
property.vendorExtensions[VENDOR_EXTENSION_BASE_NAME_LITERAL] =
526-
property.baseName.replace("$", "\\$")
536+
property.baseName.replace("$", "\\$")
527537
}
528538
}
529539

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ const val REMOVE_OPERATION_PARAMS_DESCRIPTION = "Option to remove specific param
3434
const val HEADER_CLI = "headerCliOptions"
3535
const val HEADER_CLI_DESCRIPTION = "Options to generate Header"
3636
const val REMOVE_MINUS_TEXT_FROM_HEADER = "removeMinusTextInHeaderProperty"
37-
const val REMOVE_MINUS_TEXT_FROM_HEADER_DESCRIPTION = "Remove minus text from header's property name if it is present."
37+
const val REMOVE_MINUS_TEXT_FROM_HEADER_DESCRIPTION = "Remove minus text from header's property name if it is present."
38+
39+
const val REMOVE_ENDPOINT_STARTING_SLASH = "ignoreEndpointStartingSlash"
40+
const val REMOVE_ENDPOINT_STARTING_SLASH_DESCRIPTION = "Remove/Ignore a starting slash from an endpoint definition if it is present."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 a starting *`/`* in a fragment if it's present.
10+
*
11+
*
12+
* ```mustache
13+
* {{#lambda.ignoreStartingSlash}}{{paramName}}{{/lambda.ignoreStartingSlash}}
14+
* ```
15+
*
16+
* For instance if _paramName=/v1/api/nice_ so result will be _v1/api/nice_
17+
*
18+
* @author eMan s.r.o.
19+
* @since 2.0.0
20+
* @see[Mustache.Lambda]
21+
*/
22+
class IgnoreStartingSlashLambda(private val generator: CodegenConfig) : Mustache.Lambda {
23+
24+
companion object {
25+
const val LAMBDA_NAME = "ignoreStartingSlash"
26+
}
27+
28+
override fun execute(frag: Template.Fragment, out: Writer) {
29+
val segmentPath = frag.execute()
30+
if (segmentPath.startsWith("/")) {
31+
out.write(segmentPath.replaceFirst("/", "", true))
32+
} else {
33+
out.write(segmentPath)
34+
}
35+
}
36+
}

lib/src/main/resources/kotlin-client/libraries/jvm-retrofit2/api.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ interface {{classname}} {
3939
{{/-first}}
4040
{{/prioritizedContentTypes}}
4141
{{/formParams}}
42-
@{{httpMethod}}("{{{path}}}")
42+
@{{httpMethod}}("{{#ignoreEndpointStartingSlash}}{{#lambda.ignoreStartingSlash}}{{path}}{{/lambda.ignoreStartingSlash}}{{/ignoreEndpointStartingSlash}}{{^ignoreEndpointStartingSlash}}{{path}}{{/ignoreEndpointStartingSlash}}")
4343
fun {{operationId}}({{^allParams}}){{/allParams}}{{#allParams}}{{>libraries/jvm-retrofit2/queryParams}}{{>libraries/jvm-retrofit2/pathParams}}{{>libraries/jvm-retrofit2/headerParams}}{{>libraries/jvm-retrofit2/bodyParams}}{{>libraries/jvm-retrofit2/formParams}}{{#hasMore}}, {{/hasMore}}{{^hasMore}}){{/hasMore}}{{/allParams}}: Call<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}{{/isResponseFile}}>
4444

4545
{{/operation}}

0 commit comments

Comments
 (0)