From 7b6916146eb67300dc50ad0bc2b38c16f459063f Mon Sep 17 00:00:00 2001 From: vsouhrada Date: Mon, 10 May 2021 14:19:08 +0200 Subject: [PATCH 1/2] Add a new parameter `generateApi` --- CHANGELOG.md | 2 + README.md | 6 ++- gradle.properties | 2 +- .../generator/kotlin/KotlinClientCodegen.kt | 45 ++++++++++++++++++- .../swagger/codegen/language/CodegenConst.kt | 5 ++- 5 files changed, 55 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 400d551..e49cfea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ Change Log ## 2.2.4 (TBD) ### Added: +- (Kotlin) Add a new parameter `generateApi` you can enable/disable to generate API with infrastructure. + If you set to false, only a model classes will be generated. - Support for Kotlin Multiplatform `kotlinx.datetime` date library ## 2.2.3 (2020-08-31) diff --git a/README.md b/README.md index a1a89f9..782212a 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ configure { "enumPropertyNaming" to "UPPERCASE", "modelNameSuffix" to "Dto", "apiNameSuffix" to "Service", + "generateApi" to true, "generateInfrastructure" to false, "emptyDataClasses" to false, "composedArrayAsAny" to true, @@ -82,7 +83,8 @@ configure { - `templateEngine` - Currently this generator is supporting only `mustache`. Support of `handlebars` is in a progress. - `dateLibrary` - By this property you can set date library used to serialize dates and times. - `enumPropertyNaming` - By this property you can change enum property naming style. ("camelCase", "PascalCase", "snake_case", "original", "UPPERCASE") - - `generateInfrastructure` - By this property you can enable to generate API infrastructure. + - `generateApi` - By this property you can enable/disable to generate API with infrastructure. If you set to false, only a model classes will be generated. + - `generateInfrastructure` - By this property you can enable to generate API infrastructure. If property `generateApi=false`, then this property is ignored. - `collectionType` - By this property cou can change collection type. - `emptyDataClasses` - By this property you can enable empty data classes being generated. (Note: it should not pass Kotlin compilation.) - `generateAliasAsModel` - By this property you can generate alias (array, map) as model. @@ -106,4 +108,4 @@ If your OpenApi contains some specific objects for parsing JSON, .... You need a ```kotlin implementation("com.squareup.moshi:moshi-kotlin:1.9.2") implementation("com.squareup.moshi:moshi-adapters:1.9.2") -``` \ No newline at end of file +``` diff --git a/gradle.properties b/gradle.properties index 096eb0b..4808929 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ org.gradle.jvmargs=-Xmx1536m -version=2.2.4 +version=3.0.4 diff --git a/lib/src/main/kotlin/cz/eman/swagger/codegen/generator/kotlin/KotlinClientCodegen.kt b/lib/src/main/kotlin/cz/eman/swagger/codegen/generator/kotlin/KotlinClientCodegen.kt index 5b93c8c..2b9e933 100644 --- a/lib/src/main/kotlin/cz/eman/swagger/codegen/generator/kotlin/KotlinClientCodegen.kt +++ b/lib/src/main/kotlin/cz/eman/swagger/codegen/generator/kotlin/KotlinClientCodegen.kt @@ -10,6 +10,8 @@ import cz.eman.swagger.codegen.language.COMPOSED_VARS_NOT_REQUIRED import cz.eman.swagger.codegen.language.COMPOSED_VARS_NOT_REQUIRED_DESCRIPTION import cz.eman.swagger.codegen.language.EMPTY_DATA_CLASS import cz.eman.swagger.codegen.language.EMPTY_DATA_CLASS_DESCRIPTION +import cz.eman.swagger.codegen.language.GENERATE_API +import cz.eman.swagger.codegen.language.GENERATE_API_DESCRIPTION import cz.eman.swagger.codegen.language.GENERATE_INFRASTRUCTURE_API import cz.eman.swagger.codegen.language.GENERATE_INFRASTRUCTURE_API_DESCRIPTION import cz.eman.swagger.codegen.language.GENERATE_PRIMITIVE_TYPE_ALIAS @@ -59,7 +61,8 @@ import java.io.File * * Additional generator options: * - `dateLibrary` - By this property you can set date library used to serialize dates and times. - * - `generateInfrastructure` - By this property you can enable to generate API infrastructure. + * - `generateApi` - By this property you can enable/disable to generate API with infrastructure. If you set to false, only a model classes will be generated. Default is true + * - `generateInfrastructure` - By this property you can enable to generate API infrastructure. If property `generateApi=false`, then this property is ignored. * - `collectionType` - By this property cou can change collection type. * - `emptyDataClasses` - By this property you can enable empty data classes being generated. (Note: it should not pass Kotlin compilation.) * - `composedArrayAsAny` - By this property array of composed is changed to array of object (kotlin.Any). @@ -173,6 +176,7 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient */ override fun processOpts() { super.processOpts() + processOptsApi() processOptsInfrastructure() processOptsAdditionalSupportingFiles() processOptsAdditional() @@ -327,6 +331,7 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient * @since 2.0.0 */ private fun initSettings() { + initSettingsApi() initSettingsInfrastructure() initSettingsEmptyDataClass() initSettingsComposedArrayAny() @@ -354,6 +359,22 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient cliOptions.add(infrastructureCli) } + /** + * Settings used to generate api with infrastructure or without both of them. + * + * @since 2.2.4 + */ + private fun initSettingsApi() { + val apiCli = CliOption(GENERATE_API, GENERATE_API_DESCRIPTION) + val infraOptions = HashMap() + infraOptions[GenerateApiType.API.value] = "Generate API" + infraOptions[EndpointsCommands.INGORE_ENDPOINT_STARTING_SLASH.value] = + REMOVE_ENDPOINT_STARTING_SLASH_DESCRIPTION + apiCli.enum = infraOptions + + cliOptions.add(apiCli) + } + /** * Adds all headers options to this generator * @@ -502,6 +523,28 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient } } + /** + * Processes options for API generated classes. Removes all API template files from the generation isf this + * option is set to false (do not generate API). For more information see [initSettingsInfrastructure]. + * + * @since 2.2.4 + */ + private fun processOptsApi() { + var generateApi = true + if (additionalProperties.containsKey(GENERATE_API)) { + generateApi = convertPropertyToBooleanAndWriteBack(GENERATE_API) + } + + if (!generateApi) { + apiDocTemplateFiles.clear() + apiTemplateFiles.clear() + if (additionalProperties.containsKey(GENERATE_API)) { + additionalProperties[GENERATE_INFRASTRUCTURE_API] = false + // The processOptsInfrastructure() function should be called after thsi function + } + } + } + /** * Adds additional supporting files like Readme, build.gradle or settings.gradle. * diff --git a/lib/src/main/kotlin/cz/eman/swagger/codegen/language/CodegenConst.kt b/lib/src/main/kotlin/cz/eman/swagger/codegen/language/CodegenConst.kt index 3b723cd..6077c3b 100644 --- a/lib/src/main/kotlin/cz/eman/swagger/codegen/language/CodegenConst.kt +++ b/lib/src/main/kotlin/cz/eman/swagger/codegen/language/CodegenConst.kt @@ -7,6 +7,9 @@ package cz.eman.swagger.codegen.language const val GENERATE_INFRASTRUCTURE_API = "generateInfrastructure" const val GENERATE_INFRASTRUCTURE_API_DESCRIPTION = "Option to add infrastructure package" +const val GENERATE_API = "generateApi" +const val GENERATE_API_DESCRIPTION = "Option to add api generated classes" + const val EMPTY_DATA_CLASS = "emptyDataClasses" const val EMPTY_DATA_CLASS_DESCRIPTION = "Option to allow empty data classes (default: false)." @@ -36,4 +39,4 @@ const val REMOVE_MINUS_TEXT_FROM_HEADER_DESCRIPTION = "Remove minus text from he const val REMOVE_ENDPOINT_STARTING_SLASH = "ignoreEndpointStartingSlash" const val REMOVE_ENDPOINT_STARTING_SLASH_DESCRIPTION = - "Remove/Ignore a starting slash from an endpoint definition if it is present." \ No newline at end of file + "Remove/Ignore a starting slash from an endpoint definition if it is present." From 309e1e7781f005adbaf9a32c229fdf8b43b08c6d Mon Sep 17 00:00:00 2001 From: vsouhrada Date: Mon, 10 May 2021 14:22:49 +0200 Subject: [PATCH 2/2] Fix generator version --- CHANGELOG.md | 2 +- gradle.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e49cfea..59c8b63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Change Log ### Added: - (Kotlin) Add a new parameter `generateApi` you can enable/disable to generate API with infrastructure. - If you set to false, only a model classes will be generated. + If you set it to false, only model classes will be generated. - Support for Kotlin Multiplatform `kotlinx.datetime` date library ## 2.2.3 (2020-08-31) diff --git a/gradle.properties b/gradle.properties index 4808929..096eb0b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ org.gradle.jvmargs=-Xmx1536m -version=3.0.4 +version=2.2.4