Skip to content

Commit

Permalink
[kotlin-client][kotlin-server] feature: allow creation of kotlin data…
Browse files Browse the repository at this point in the history
… classes that implement java.io.Serializable (OpenAPITools#3997)

* feat: allow creation of kotlin data classes that implement java.io.Serializable, by adding the config option serializableModel=(true|false)

* docs: add markdown docu for new configOption serializableModel

* fix: do not use parcelize in script

* update kotlin samples

* add the option, update the doc
  • Loading branch information
janweinschenker authored and Jesse Michael committed Oct 3, 2019
1 parent 4f2eaca commit 7c5aefe
Show file tree
Hide file tree
Showing 96 changed files with 398 additions and 117 deletions.
2 changes: 1 addition & 1 deletion bin/kotlin-client-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -t modules/openapi-generator/src/main/resources/kotlin-client -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g kotlin --artifact-id kotlin-petstore-client --additional-properties dateLibrary=java8 -o samples/client/petstore/kotlin $@"
ags="generate -t modules/openapi-generator/src/main/resources/kotlin-client -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g kotlin --artifact-id kotlin-petstore-client --additional-properties dateLibrary=java8,serializableModel=true -o samples/client/petstore/kotlin $@"

java ${JAVA_OPTS} -jar ${executable} ${ags}
2 changes: 1 addition & 1 deletion bin/kotlin-client-string.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -t modules/openapi-generator/src/main/resources/kotlin-client -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g kotlin --artifact-id kotlin-petstore-string --additional-properties dateLibrary=string -o samples/client/petstore/kotlin-string $@"
ags="generate -t modules/openapi-generator/src/main/resources/kotlin-client -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g kotlin --artifact-id kotlin-petstore-string --additional-properties dateLibrary=string,serializableModel=true -o samples/client/petstore/kotlin-string $@"

java ${JAVA_OPTS} -jar ${executable} ${ags}

Expand Down
2 changes: 1 addition & 1 deletion bin/kotlin-server-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -t modules/openapi-generator/src/main/resources/kotlin-server -g kotlin-server --library=ktor -o samples/server/petstore/kotlin-server/ktor --additional-properties hideGenerationTimestamp=true $@"
ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -t modules/openapi-generator/src/main/resources/kotlin-server -g kotlin-server --library=ktor -o samples/server/petstore/kotlin-server/ktor --additional-properties hideGenerationTimestamp=true,serializableModel=true $@"

java ${JAVA_OPTS} -jar ${executable} ${ags}

Expand Down
2 changes: 1 addition & 1 deletion bin/kotlin-springboot-petstore-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ then
fi

export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -t modules/openapi-generator/src/main/resources/kotlin-spring -g kotlin-spring -o samples/server/petstore/kotlin-springboot --additional-properties=library=spring-boot,beanValidations=true,swaggerAnnotations=true,serviceImplementation=true"
ags="$@ generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -t modules/openapi-generator/src/main/resources/kotlin-spring -g kotlin-spring -o samples/server/petstore/kotlin-springboot --additional-properties=library=spring-boot,beanValidations=true,swaggerAnnotations=true,serviceImplementation=true,serializableModel=true"

echo "Cleaning previously generated files if any from samples/server/petstore/kotlin-springboot"
rm -rf samples/server/petstore/kotlin-springboot
Expand Down
2 changes: 1 addition & 1 deletion bin/openapi3/kotlin-client-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ then
fi

export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -t modules/openapi-generator/src/main/resources/kotlin-client -g kotlin --artifact-id kotlin-petstore-client --additional-properties dateLibrary=java8 -o samples/openapi3/client/petstore/kotlin $@"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -t modules/openapi-generator/src/main/resources/kotlin-client -g kotlin --artifact-id kotlin-petstore-client --additional-properties dateLibrary=java8,serializableModel=true -o samples/openapi3/client/petstore/kotlin $@"

echo "Cleaning previously generated files if any from samples/openapi3/client/petstore/kotlin"
rm -rf samples/openapi3/client/petstore/kotlin
Expand Down
2 changes: 1 addition & 1 deletion bin/openapi3/kotlin-springboot-petstore-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ then
fi

export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -t modules/openapi-generator/src/main/resources/kotlin-spring -g kotlin-spring -o samples/openapi3/server/petstore/kotlin-springboot --additional-properties=library=spring-boot,beanValidations=true,swaggerAnnotations=true,serviceImplementation=true"
ags="$@ generate -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -t modules/openapi-generator/src/main/resources/kotlin-spring -g kotlin-spring -o samples/openapi3/server/petstore/kotlin-springboot --additional-properties=library=spring-boot,beanValidations=true,swaggerAnnotations=true,serviceImplementation=true,serializableModel=true"

echo "Cleaning previously generated files if any from samples/server/openapi3/petstore/kotlin-springboot"
rm -rf samples/server/openapi3/petstore/kotlin-springboot
Expand Down
1 change: 1 addition & 0 deletions docs/generators/kotlin-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ sidebar_label: kotlin-server
|enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |camelCase|
|serializationLibrary|What serialization library to use: 'moshi' (default), or 'gson'| |moshi|
|parcelizeModels|toggle "@Parcelize" for generated models| |null|
|serializableModel|boolean - toggle "implements Serializable" for generated models| |null|
|library|library template (sub-template)|<dl><dt>**ktor**</dt><dd>ktor framework</dd><dl>|ktor|
|featureAutoHead|Automatically provide responses to HEAD requests for existing routes that have the GET verb defined.| |true|
|featureConditionalHeaders|Avoid sending content if client already has same content, by checking ETag or LastModified properties.| |false|
Expand Down
1 change: 1 addition & 0 deletions docs/generators/kotlin-spring.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ sidebar_label: kotlin-spring
|enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |camelCase|
|serializationLibrary|What serialization library to use: 'moshi' (default), or 'gson'| |moshi|
|parcelizeModels|toggle &quot;@Parcelize&quot; for generated models| |null|
|serializableModel|boolean - toggle &quot;implements Serializable&quot; for generated models| |null|
|title|server title name or client service name| |OpenAPI Kotlin Spring|
|basePackage|base package (invokerPackage) for generated code| |org.openapitools|
|serverPort|configuration the port in which the sever is to run on| |8080|
Expand Down
1 change: 1 addition & 0 deletions docs/generators/kotlin-vertx.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ sidebar_label: kotlin-vertx
|enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |camelCase|
|serializationLibrary|What serialization library to use: 'moshi' (default), or 'gson'| |moshi|
|parcelizeModels|toggle &quot;@Parcelize&quot; for generated models| |null|
|serializableModel|boolean - toggle &quot;implements Serializable&quot; for generated models| |null|
1 change: 1 addition & 0 deletions docs/generators/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ sidebar_label: kotlin
|enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |camelCase|
|serializationLibrary|What serialization library to use: 'moshi' (default), or 'gson'| |moshi|
|parcelizeModels|toggle &quot;@Parcelize&quot; for generated models| |null|
|serializableModel|boolean - toggle &quot;implements Serializable&quot; for generated models| |null|
|dateLibrary|Option. Date library to use|<dl><dt>**string**</dt><dd>String</dd><dt>**java8**</dt><dd>Java 8 native JSR310 (jvm only)</dd><dt>**threetenbp**</dt><dd>Threetenbp (jvm only)</dd><dl>|java8|
|collectionType|Option. Collection type to use|<dl><dt>**array**</dt><dd>kotlin.Array</dd><dt>**list**</dt><dd>kotlin.collections.List</dd><dl>|array|
|library|Library template (sub-template) to use|<dl><dt>**jvm**</dt><dd>Platform: Java Virtual Machine. HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.8.1.</dd><dt>**multiplatform**</dt><dd>Platform: Kotlin multiplatform. HTTP client: Ktor 1.2.4. JSON processing: Kotlinx Serialization: 0.12.0.</dd><dl>|jvm|
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public enum SERIALIZATION_LIBRARY_TYPE {moshi, gson}
protected String modelDocPath = "docs/";
protected boolean parcelizeModels = false;

protected boolean serializableModel = false;

protected CodegenConstants.ENUM_PROPERTY_NAMING_TYPE enumPropertyNaming = CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.camelCase;
protected SERIALIZATION_LIBRARY_TYPE serializationLibrary = SERIALIZATION_LIBRARY_TYPE.moshi;

Expand Down Expand Up @@ -215,6 +217,7 @@ public AbstractKotlinCodegen() {
cliOptions.add(serializationLibraryOpt.defaultValue(serializationLibrary.name()));

cliOptions.add(new CliOption(CodegenConstants.PARCELIZE_MODELS, CodegenConstants.PARCELIZE_MODELS_DESC));
cliOptions.add(new CliOption(CodegenConstants.SERIALIZABLE_MODEL, CodegenConstants.SERIALIZABLE_MODEL_DESC));
}

@Override
Expand Down Expand Up @@ -411,6 +414,12 @@ public void processOpts() {
LOGGER.warn(CodegenConstants.INVOKER_PACKAGE + " with " + this.getName() + " generator is ignored. Use " + CodegenConstants.PACKAGE_NAME + ".");
}

if (additionalProperties.containsKey(CodegenConstants.SERIALIZABLE_MODEL)) {
this.setSerializableModel(Boolean.valueOf((String) additionalProperties.get(CodegenConstants.SERIALIZABLE_MODEL)));
} else {
additionalProperties.put(CodegenConstants.SERIALIZABLE_MODEL, serializableModel);
}

if (additionalProperties.containsKey(CodegenConstants.PARCELIZE_MODELS)) {
this.setParcelizeModels(Boolean.valueOf((String) additionalProperties.get(CodegenConstants.PARCELIZE_MODELS)));
} else {
Expand Down Expand Up @@ -460,6 +469,13 @@ public void setParcelizeModels(Boolean parcelizeModels) {
this.parcelizeModels = parcelizeModels;
}

public boolean isSerializableModel() {
return serializableModel;
}

public void setSerializableModel(boolean serializableModel) {
this.serializableModel = serializableModel;
}
/**
* Return the sanitized variable name for enum
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import kotlinx.android.parcel.Parcelize
import kotlinx.serialization.*
import kotlinx.serialization.internal.CommonEnumSerializer
{{/multiplatform}}
{{#serializableModel}}
import java.io.Serializable
{{/serializableModel}}
/**
* {{{description}}}
{{#vars}}
Expand All @@ -31,7 +34,9 @@ data class {{classname}} (
{{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}},
{{/hasOptional}}{{/hasRequired}}{{#optionalVars}}{{>data_class_opt_var}}{{^-last}},
{{/-last}}{{/optionalVars}}
){{#parcelizeModels}} : Parcelable{{/parcelizeModels}}
) {{^serializableModel}}{{#parcelizeModels}} : Parcelable{{/parcelizeModels}}{{/serializableModel}}
{{^parcelizeModels}}{{#serializableModel}}: Serializable {{/serializableModel}}{{/parcelizeModels}}
{{#parcelizeModels}}{{#serializableModel}} : Parcelable, Serializable {{/serializableModel}}{{/parcelizeModels}}
{{#hasEnums}}
{
{{#vars}}{{#isEnum}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

{{/parcelizeModels}}
{{#serializableModel}}
import java.io.Serializable
{{/serializableModel}}
/**
* {{{description}}}
{{#vars}}
Expand All @@ -18,7 +21,10 @@ data class {{classname}} (
{{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}},
{{/hasOptional}}{{/hasRequired}}{{#optionalVars}}{{>data_class_opt_var}}{{^-last}},
{{/-last}}{{/optionalVars}}
){{#parcelizeModels}} : Parcelable{{/parcelizeModels}} {
) {{^serializableModel}}{{#parcelizeModels}} : Parcelable{{/parcelizeModels}}{{/serializableModel}}
{{^parcelizeModels}}{{#serializableModel}}: Serializable {{/serializableModel}}{{/parcelizeModels}}
{{#parcelizeModels}}{{#serializableModel}} : Parcelable, Serializable {{/serializableModel}}{{/parcelizeModels}}
{
{{#hasEnums}}{{#vars}}{{#isEnum}}
/**
* {{{description}}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ data class ApiResponse (
@SerialName(value = "code") val code: kotlin.Int? = null,
@SerialName(value = "type") val type: kotlin.String? = null,
@SerialName(value = "message") val message: kotlin.String? = null
)
)



Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ import kotlinx.serialization.internal.CommonEnumSerializer
data class Category (
@SerialName(value = "id") val id: kotlin.Long? = null,
@SerialName(value = "name") val name: kotlin.String? = null
)
)



Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ data class Order (
/* Order Status */
@SerialName(value = "status") val status: Order.Status? = null,
@SerialName(value = "complete") val complete: kotlin.Boolean? = null
)
)


{

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ data class Pet (
@SerialName(value = "tags") val tags: kotlin.Array<Tag>? = null,
/* pet status in the store */
@SerialName(value = "status") val status: Pet.Status? = null
)
)


{

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ import kotlinx.serialization.internal.CommonEnumSerializer
data class Tag (
@SerialName(value = "id") val id: kotlin.Long? = null,
@SerialName(value = "name") val name: kotlin.String? = null
)
)



Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ data class User (
@SerialName(value = "phone") val phone: kotlin.String? = null,
/* User Status */
@SerialName(value = "userStatus") val userStatus: kotlin.Int? = null
)
)



Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package org.openapitools.client.models


import com.squareup.moshi.Json
import java.io.Serializable
/**
* Describes the result of uploading an image resource
* @param code
Expand All @@ -27,5 +28,7 @@ data class ApiResponse (
val type: kotlin.String? = null,
@Json(name = "message")
val message: kotlin.String? = null
)
)
: Serializable


Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package org.openapitools.client.models


import com.squareup.moshi.Json
import java.io.Serializable
/**
* A category for a pet
* @param id
Expand All @@ -24,5 +25,7 @@ data class Category (
val id: kotlin.Long? = null,
@Json(name = "name")
val name: kotlin.String? = null
)
)
: Serializable


Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package org.openapitools.client.models


import com.squareup.moshi.Json
import java.io.Serializable
/**
* An order for a pets from the pet store
* @param id
Expand All @@ -37,7 +38,9 @@ data class Order (
val status: Order.Status? = null,
@Json(name = "complete")
val complete: kotlin.Boolean? = null
)
)
: Serializable

{

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.openapitools.client.models.Category
import org.openapitools.client.models.Tag

import com.squareup.moshi.Json
import java.io.Serializable
/**
* A pet for sale in the pet store
* @param id
Expand All @@ -39,7 +40,9 @@ data class Pet (
/* pet status in the store */
@Json(name = "status")
val status: Pet.Status? = null
)
)
: Serializable

{

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package org.openapitools.client.models


import com.squareup.moshi.Json
import java.io.Serializable
/**
* A tag for a pet
* @param id
Expand All @@ -24,5 +25,7 @@ data class Tag (
val id: kotlin.Long? = null,
@Json(name = "name")
val name: kotlin.String? = null
)
)
: Serializable


Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package org.openapitools.client.models


import com.squareup.moshi.Json
import java.io.Serializable
/**
* A User who is purchasing from the pet store
* @param id
Expand Down Expand Up @@ -43,5 +44,7 @@ data class User (
/* User Status */
@Json(name = "userStatus")
val userStatus: kotlin.Int? = null
)
)
: Serializable


Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ data class ApiResponse (
val type: kotlin.String? = null,
@Json(name = "message")
val message: kotlin.String? = null
)
)



Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ data class Category (
val id: kotlin.Long? = null,
@Json(name = "name")
val name: kotlin.String? = null
)
)



Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ data class Order (
val status: Order.Status? = null,
@Json(name = "complete")
val complete: kotlin.Boolean? = null
)
)


{

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ data class Pet (
/* pet status in the store */
@Json(name = "status")
val status: Pet.Status? = null
)
)


{

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ data class Tag (
val id: kotlin.Long? = null,
@Json(name = "name")
val name: kotlin.String? = null
)
)



Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@ data class User (
/* User Status */
@Json(name = "userStatus")
val userStatus: kotlin.Int? = null
)
)



Loading

0 comments on commit 7c5aefe

Please sign in to comment.