diff --git a/docs/generators/elm.md b/docs/generators/elm.md index bab23d8acf6f..891a281d1eb3 100644 --- a/docs/generators/elm.md +++ b/docs/generators/elm.md @@ -6,7 +6,4 @@ CONFIG OPTIONS for elm 0.19 - Elm 0.19 0.18 - Elm 0.18 - elmPrefixCustomTypeVariants - Prefix custom type variants (Default: false) - Back to the [generators list](README.md) diff --git a/docs/generators/erlang-server.md b/docs/generators/erlang-server.md index 51fc0c2d9719..29c7d51313dc 100644 --- a/docs/generators/erlang-server.md +++ b/docs/generators/erlang-server.md @@ -4,7 +4,4 @@ CONFIG OPTIONS for erlang-server packageName Erlang package name (convention: lowercase). (Default: openapi) - openAPISpecName - Openapi Spec Name. (Default: openapi) - Back to the [generators list](README.md) diff --git a/docs/generators/kotlin-server.md b/docs/generators/kotlin-server.md index 1d69a88b7fb3..3ff4b6151717 100644 --- a/docs/generators/kotlin-server.md +++ b/docs/generators/kotlin-server.md @@ -19,6 +19,9 @@ CONFIG OPTIONS for kotlin-server enumPropertyNaming Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original' (Default: camelCase) + parcelizeModels + toggle "@Parcelize" for generated models + library library template (sub-template) to use (Default: ktor) ktor - ktor framework diff --git a/docs/generators/kotlin-spring.md b/docs/generators/kotlin-spring.md index 16c84f74b86e..568c3967eec9 100644 --- a/docs/generators/kotlin-spring.md +++ b/docs/generators/kotlin-spring.md @@ -19,6 +19,9 @@ CONFIG OPTIONS for kotlin-spring enumPropertyNaming Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original' (Default: camelCase) + parcelizeModels + toggle "@Parcelize" for generated models + title server title name or client service name (Default: OpenAPI Kotlin Spring) diff --git a/docs/generators/kotlin.md b/docs/generators/kotlin.md index 75529e492863..91ebf5fc6a6b 100644 --- a/docs/generators/kotlin.md +++ b/docs/generators/kotlin.md @@ -19,6 +19,9 @@ CONFIG OPTIONS for kotlin enumPropertyNaming Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original' (Default: camelCase) + parcelizeModels + toggle "@Parcelize" for generated models + dateLibrary Option. Date library to use string - String diff --git a/docs/generators/rust.md b/docs/generators/rust.md index c126505e9816..c8779939a309 100644 --- a/docs/generators/rust.md +++ b/docs/generators/rust.md @@ -10,9 +10,4 @@ CONFIG OPTIONS for rust hideGenerationTimestamp Hides the generation timestamp when files are generated. (Default: true) - library - library template (sub-template) to use. (Default: hyper) - hyper - HTTP client: Hyper. - reqwest - HTTP client: Reqwest. - Back to the [generators list](README.md) diff --git a/docs/generators/spring.md b/docs/generators/spring.md index e7d64807746b..ab0e1ad15a6e 100644 --- a/docs/generators/spring.md +++ b/docs/generators/spring.md @@ -152,9 +152,6 @@ CONFIG OPTIONS for spring useBeanValidation Use BeanValidation API annotations (Default: true) - performBeanValidation - Use Bean Validation Impl. to perform BeanValidation (Default: false) - implicitHeaders Use of @ApiImplicitParams for headers. (Default: false) @@ -167,9 +164,6 @@ CONFIG OPTIONS for spring useOptional Use Optional container for optional parameters (Default: false) - hateoas - Use Spring HATEOAS library to allow adding HATEOAS links (Default: false) - library library template (sub-template) to use (Default: spring-boot) spring-boot - Spring-boot Server application using the SpringFox integration. diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java index 1b7500c831e9..c66cce4b4f69 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java @@ -221,6 +221,10 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case, public static final String SOURCECODEONLY_GENERATION = "generateSourceCodeOnly"; public static final String SOURCECODEONLY_GENERATION_DESC = "Specifies that only a library source code is to be generated."; + public static final String PARCELIZE_MODELS = "parcelizeModels"; + public static final String PARCELIZE_MODELS_DESC = "toggle \"@Parcelize\" for generated models"; + + // Not user-configurable. System provided for use in templates. public static final String GENERATE_APIS = "generateApis"; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java index e59d72cd5892..38515871e000 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java @@ -44,6 +44,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co protected String apiDocPath = "docs/"; protected String modelDocPath = "docs/"; + protected boolean parcelizeModels = false; protected CodegenConstants.ENUM_PROPERTY_NAMING_TYPE enumPropertyNaming = CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.camelCase; @@ -198,6 +199,7 @@ public AbstractKotlinCodegen() { CliOption enumPropertyNamingOpt = new CliOption(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_DESC); cliOptions.add(enumPropertyNamingOpt.defaultValue(enumPropertyNaming.name())); + cliOptions.add(new CliOption(CodegenConstants.PARCELIZE_MODELS, CodegenConstants.PARCELIZE_MODELS_DESC)); } @Override @@ -350,6 +352,20 @@ public void processOpts() { LOGGER.warn(CodegenConstants.INVOKER_PACKAGE + " with " + this.getName() + " generator is ignored. Use " + CodegenConstants.PACKAGE_NAME + "."); } + if (additionalProperties.containsKey(CodegenConstants.PARCELIZE_MODELS)) { + this.setParcelizeModels(Boolean.valueOf((String)additionalProperties.get(CodegenConstants.PARCELIZE_MODELS))); + LOGGER.info(CodegenConstants.PARCELIZE_MODELS + " depends on the android framework and " + + "experimental parcelize feature. Make sure your build applies the android plugin:\n" + + "apply plugin: 'com.android.library' OR apply plugin: 'com.android.application'.\n" + + "and enables the experimental features:\n" + + "androidExtensions {\n" + + " experimental = true\n" + + "}" + ); + } else { + additionalProperties.put(CodegenConstants.PARCELIZE_MODELS, parcelizeModels); + } + additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage()); additionalProperties.put(CodegenConstants.MODEL_PACKAGE, modelPackage()); @@ -377,6 +393,14 @@ public void setSourceFolder(String sourceFolder) { this.sourceFolder = sourceFolder; } + public Boolean getParcelizeModels() { + return parcelizeModels; + } + + public void setParcelizeModels(Boolean parcelizeModels) { + this.parcelizeModels = parcelizeModels; + } + /** * Return the sanitized variable name for enum * diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/data_class.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/data_class.mustache index 1431de07d137..2238c6dae292 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/data_class.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/data_class.mustache @@ -1,19 +1,27 @@ {{#hasEnums}} import com.squareup.moshi.Json {{/hasEnums}} +{{#parcelizeModels}} +import android.os.Parcelable +import kotlinx.android.parcel.Parcelize + +{{/parcelizeModels}} /** * {{{description}}} {{#vars}} * @param {{name}} {{{description}}} {{/vars}} */ +{{#parcelizeModels}} +@Parcelize +{{/parcelizeModels}} data class {{classname}} ( {{#requiredVars}} {{>data_class_req_var}}{{^-last}}, {{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}}, {{/hasOptional}}{{/hasRequired}}{{#optionalVars}}{{>data_class_opt_var}}{{^-last}}, {{/-last}}{{/optionalVars}} -) { +){{#parcelizeModels}} : Parcelable{{/parcelizeModels}} { {{#hasEnums}}{{#vars}}{{#isEnum}} /** * {{{description}}} diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/data_class.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/data_class.mustache index 1237ec1f4311..feb6a0b317e6 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-server/data_class.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-server/data_class.mustache @@ -1,16 +1,24 @@ +{{#parcelizeModels}} +import android.os.Parcelable +import kotlinx.android.parcel.Parcelize + +{{/parcelizeModels}} /** * {{{description}}} {{#vars}} * @param {{name}} {{{description}}} {{/vars}} */ +{{#parcelizeModels}} +@Parcelize +{{/parcelizeModels}} data class {{classname}} ( {{#requiredVars}} {{>data_class_req_var}}{{^-last}}, {{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}}, {{/hasOptional}}{{/hasRequired}}{{#optionalVars}}{{>data_class_opt_var}}{{^-last}}, {{/-last}}{{/optionalVars}} -) { +){{#parcelizeModels}} : Parcelable{{/parcelizeModels}} { {{#hasEnums}}{{#vars}}{{#isEnum}} /** * {{{description}}}