Skip to content

Commit

Permalink
Add parcelizeModels cli option (OpenAPITools#1289)
Browse files Browse the repository at this point in the history
* Add parcelizeModels cli option

* Add info log to clarify the parcelization requirements.

* Update docs
  • Loading branch information
absimas authored and wing328 committed Nov 9, 2018
1 parent b798306 commit accddd6
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 19 deletions.
3 changes: 0 additions & 3 deletions docs/generators/elm.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 0 additions & 3 deletions docs/generators/erlang-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 3 additions & 0 deletions docs/generators/kotlin-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docs/generators/kotlin-spring.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions docs/generators/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions docs/generators/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 0 additions & 6 deletions docs/generators/spring.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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;

Expand Down Expand Up @@ -199,6 +200,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
Expand Down Expand Up @@ -356,6 +358,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());

Expand Down Expand Up @@ -383,6 +399,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
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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}}}
Expand Down
Original file line number Diff line number Diff line change
@@ -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}}}
Expand Down

0 comments on commit accddd6

Please sign in to comment.