From b16144094decfc7d144f10f644c40c23d22c5b7e Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sat, 14 Dec 2024 16:11:57 +0800 Subject: [PATCH] [kotlin-server][ktor2] fix isKtor, update samples (#20327) * fix isKtor, update samples * fi dockerfile * fix dataclass * add new files * fix template * regenerate samples --- .../languages/KotlinServerCodegen.java | 21 ++++++-- .../libraries/ktor2/_api_body.mustache | 6 +-- .../libraries/ktor2/api.mustache | 4 +- .../libraries/ktor2/data_class.mustache | 52 +++++++++++++++++++ .../ktor2/data_class_opt_var.mustache | 4 ++ .../ktor2/data_class_req_var.mustache | 5 ++ 6 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/data_class.mustache create mode 100644 modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/data_class_opt_var.mustache create mode 100644 modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/data_class_req_var.mustache diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinServerCodegen.java index 885449a51354..57f8a1961ef3 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinServerCodegen.java @@ -288,13 +288,13 @@ public void processOpts() { supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); - if (isKtor()) { + if (isKtor2Or3()) { supportingFiles.add(new SupportingFile("Dockerfile.mustache", "", "Dockerfile")); } String gradleBuildFile = "build.gradle"; - if (isJavalin() || isKtor()) { + if (isJavalin() || isKtor2Or3()) { gradleBuildFile = "build.gradle.kts"; } @@ -302,7 +302,7 @@ public void processOpts() { supportingFiles.add(new SupportingFile("settings.gradle.mustache", "", "settings.gradle")); supportingFiles.add(new SupportingFile("gradle.properties", "", "gradle.properties")); - if (isKtor()) { + if (isKtor2Or3()) { additionalProperties.put(Constants.IS_KTOR, true); supportingFiles.add(new SupportingFile("AppMain.kt.mustache", packageFolder, "AppMain.kt")); @@ -445,7 +445,20 @@ private boolean isJavalin() { return Constants.JAVALIN5.equals(library) || Constants.JAVALIN6.equals(library); } - private boolean isKtor() { + private boolean isKtor2Or3() { return Constants.KTOR.equals(library) || Constants.KTOR2.equals(library); } + + /** + * Returns true if latest version of ktor is used. + * + * @return true if latest veresion of ktor is used. + */ + private boolean isKtor() { + return Constants.KTOR.equals(library); + } + + private boolean isKtor2() { + return Constants.KTOR2.equals(library); + } } diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/_api_body.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/_api_body.mustache index b29afdb0adf2..f5a368e589d9 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/_api_body.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/_api_body.mustache @@ -1,5 +1,5 @@ {{#hasAuthMethods}} -{{>libraries/ktor/_principal}} +{{>libraries/ktor2/_principal}} {{#examples}} {{#-first}} {{#lambda.indented}}{{>_response}}{{/lambda.indented}} @@ -12,10 +12,10 @@ call.respond(HttpStatusCode.NotImplemented) {{^hasAuthMethods}} {{#examples}} {{#-first}} -{{>libraries/ktor/_response}} +{{>libraries/ktor2/_response}} {{/-first}} {{/examples}} {{^examples}} call.respond(HttpStatusCode.NotImplemented) {{/examples}} -{{/hasAuthMethods}} \ No newline at end of file +{{/hasAuthMethods}} diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/api.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/api.mustache index eb1da3bd527d..5c139b28effb 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/api.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/api.mustache @@ -35,13 +35,13 @@ fun Route.{{classname}}() { {{^featureResources}} route("{{path}}") { {{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}} { - {{#lambda.indented_12}}{{>libraries/ktor/_api_body}}{{/lambda.indented_12}} + {{#lambda.indented_12}}{{>libraries/ktor2/_api_body}}{{/lambda.indented_12}} } } {{/featureResources}} {{#featureResources}} {{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}} { - {{#lambda.indented_8}}{{>libraries/ktor/_api_body}}{{/lambda.indented_8}} + {{#lambda.indented_8}}{{>libraries/ktor2/_api_body}}{{/lambda.indented_8}} } {{/featureResources}} {{#hasAuthMethods}} diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/data_class.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/data_class.mustache new file mode 100644 index 000000000000..ea72ac7c116d --- /dev/null +++ b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/data_class.mustache @@ -0,0 +1,52 @@ +{{#parcelizeModels}} +import android.os.Parcelable +import kotlinx.parcelize.Parcelize + +{{/parcelizeModels}} +{{#serializableModel}} +import java.io.Serializable +{{/serializableModel}} +/** + * {{{description}}} +{{#vars}} + * @param {{{name}}} {{{description}}} +{{/vars}} + */ +{{#parcelizeModels}} +@Parcelize +{{/parcelizeModels}} +{{#hasVars}}data {{/hasVars}}class {{classname}}( +{{#requiredVars}} +{{>data_class_req_var}}{{^-last}}, +{{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}}, +{{/hasOptional}}{{/hasRequired}}{{#optionalVars}}{{>data_class_opt_var}}{{^-last}}, +{{/-last}}{{/optionalVars}} +) {{^serializableModel}}{{#parcelizeModels}} : Parcelable{{/parcelizeModels}}{{/serializableModel}}{{^parcelizeModels}}{{#serializableModel}}: Serializable {{/serializableModel}}{{/parcelizeModels}}{{#parcelizeModels}}{{#serializableModel}} : Parcelable, Serializable {{/serializableModel}}{{/parcelizeModels}} +{{#vendorExtensions.x-has-data-class-body}} +{ +{{/vendorExtensions.x-has-data-class-body}} +{{#serializableModel}} + companion object { + private const val serialVersionUID: Long = 123 + } +{{/serializableModel}} +{{#hasEnums}} + {{#vars}} + {{#isEnum}} + /** + * {{{description}}} + * Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} + */ + enum class {{{nameInPascalCase}}}(val value: {{{dataType}}}){ + {{#allowableValues}} + {{#enumVars}} + {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} + {{/enumVars}} + {{/allowableValues}} + } +{{/isEnum}} +{{/vars}} +{{/hasEnums}} +{{#vendorExtensions.x-has-data-class-body}} +} +{{/vendorExtensions.x-has-data-class-body}} diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/data_class_opt_var.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/data_class_opt_var.mustache new file mode 100644 index 000000000000..a5885bca9b72 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/data_class_opt_var.mustache @@ -0,0 +1,4 @@ +{{#description}} + /* {{{.}}} */ +{{/description}} + {{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/data_class_req_var.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/data_class_req_var.mustache new file mode 100644 index 000000000000..4adcedf78cba --- /dev/null +++ b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/data_class_req_var.mustache @@ -0,0 +1,5 @@ +{{#description}} + /* {{{.}}} */ +{{/description}} + {{! Note that required properties may be nullable according to the OpenAPI specification. }} + {{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}{{#defaultValue}} = {{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}} \ No newline at end of file