Skip to content

Commit b161440

Browse files
authored
[kotlin-server][ktor2] fix isKtor, update samples (#20327)
* fix isKtor, update samples * fi dockerfile * fix dataclass * add new files * fix template * regenerate samples
1 parent f766f44 commit b161440

File tree

6 files changed

+83
-9
lines changed

6 files changed

+83
-9
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinServerCodegen.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,21 +288,21 @@ public void processOpts() {
288288

289289
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
290290

291-
if (isKtor()) {
291+
if (isKtor2Or3()) {
292292
supportingFiles.add(new SupportingFile("Dockerfile.mustache", "", "Dockerfile"));
293293
}
294294

295295
String gradleBuildFile = "build.gradle";
296296

297-
if (isJavalin() || isKtor()) {
297+
if (isJavalin() || isKtor2Or3()) {
298298
gradleBuildFile = "build.gradle.kts";
299299
}
300300

301301
supportingFiles.add(new SupportingFile(gradleBuildFile + ".mustache", "", gradleBuildFile));
302302
supportingFiles.add(new SupportingFile("settings.gradle.mustache", "", "settings.gradle"));
303303
supportingFiles.add(new SupportingFile("gradle.properties", "", "gradle.properties"));
304304

305-
if (isKtor()) {
305+
if (isKtor2Or3()) {
306306
additionalProperties.put(Constants.IS_KTOR, true);
307307

308308
supportingFiles.add(new SupportingFile("AppMain.kt.mustache", packageFolder, "AppMain.kt"));
@@ -445,7 +445,20 @@ private boolean isJavalin() {
445445
return Constants.JAVALIN5.equals(library) || Constants.JAVALIN6.equals(library);
446446
}
447447

448-
private boolean isKtor() {
448+
private boolean isKtor2Or3() {
449449
return Constants.KTOR.equals(library) || Constants.KTOR2.equals(library);
450450
}
451+
452+
/**
453+
* Returns true if latest version of ktor is used.
454+
*
455+
* @return true if latest veresion of ktor is used.
456+
*/
457+
private boolean isKtor() {
458+
return Constants.KTOR.equals(library);
459+
}
460+
461+
private boolean isKtor2() {
462+
return Constants.KTOR2.equals(library);
463+
}
451464
}

modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/_api_body.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{#hasAuthMethods}}
2-
{{>libraries/ktor/_principal}}
2+
{{>libraries/ktor2/_principal}}
33
{{#examples}}
44
{{#-first}}
55
{{#lambda.indented}}{{>_response}}{{/lambda.indented}}
@@ -12,10 +12,10 @@ call.respond(HttpStatusCode.NotImplemented)
1212
{{^hasAuthMethods}}
1313
{{#examples}}
1414
{{#-first}}
15-
{{>libraries/ktor/_response}}
15+
{{>libraries/ktor2/_response}}
1616
{{/-first}}
1717
{{/examples}}
1818
{{^examples}}
1919
call.respond(HttpStatusCode.NotImplemented)
2020
{{/examples}}
21-
{{/hasAuthMethods}}
21+
{{/hasAuthMethods}}

modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ fun Route.{{classname}}() {
3535
{{^featureResources}}
3636
route("{{path}}") {
3737
{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}} {
38-
{{#lambda.indented_12}}{{>libraries/ktor/_api_body}}{{/lambda.indented_12}}
38+
{{#lambda.indented_12}}{{>libraries/ktor2/_api_body}}{{/lambda.indented_12}}
3939
}
4040
}
4141
{{/featureResources}}
4242
{{#featureResources}}
4343
{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}<Paths.{{operationId}}> {
44-
{{#lambda.indented_8}}{{>libraries/ktor/_api_body}}{{/lambda.indented_8}}
44+
{{#lambda.indented_8}}{{>libraries/ktor2/_api_body}}{{/lambda.indented_8}}
4545
}
4646
{{/featureResources}}
4747
{{#hasAuthMethods}}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{{#parcelizeModels}}
2+
import android.os.Parcelable
3+
import kotlinx.parcelize.Parcelize
4+
5+
{{/parcelizeModels}}
6+
{{#serializableModel}}
7+
import java.io.Serializable
8+
{{/serializableModel}}
9+
/**
10+
* {{{description}}}
11+
{{#vars}}
12+
* @param {{{name}}} {{{description}}}
13+
{{/vars}}
14+
*/
15+
{{#parcelizeModels}}
16+
@Parcelize
17+
{{/parcelizeModels}}
18+
{{#hasVars}}data {{/hasVars}}class {{classname}}(
19+
{{#requiredVars}}
20+
{{>data_class_req_var}}{{^-last}},
21+
{{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}},
22+
{{/hasOptional}}{{/hasRequired}}{{#optionalVars}}{{>data_class_opt_var}}{{^-last}},
23+
{{/-last}}{{/optionalVars}}
24+
) {{^serializableModel}}{{#parcelizeModels}} : Parcelable{{/parcelizeModels}}{{/serializableModel}}{{^parcelizeModels}}{{#serializableModel}}: Serializable {{/serializableModel}}{{/parcelizeModels}}{{#parcelizeModels}}{{#serializableModel}} : Parcelable, Serializable {{/serializableModel}}{{/parcelizeModels}}
25+
{{#vendorExtensions.x-has-data-class-body}}
26+
{
27+
{{/vendorExtensions.x-has-data-class-body}}
28+
{{#serializableModel}}
29+
companion object {
30+
private const val serialVersionUID: Long = 123
31+
}
32+
{{/serializableModel}}
33+
{{#hasEnums}}
34+
{{#vars}}
35+
{{#isEnum}}
36+
/**
37+
* {{{description}}}
38+
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
39+
*/
40+
enum class {{{nameInPascalCase}}}(val value: {{{dataType}}}){
41+
{{#allowableValues}}
42+
{{#enumVars}}
43+
{{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
44+
{{/enumVars}}
45+
{{/allowableValues}}
46+
}
47+
{{/isEnum}}
48+
{{/vars}}
49+
{{/hasEnums}}
50+
{{#vendorExtensions.x-has-data-class-body}}
51+
}
52+
{{/vendorExtensions.x-has-data-class-body}}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{{#description}}
2+
/* {{{.}}} */
3+
{{/description}}
4+
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{{#description}}
2+
/* {{{.}}} */
3+
{{/description}}
4+
{{! Note that required properties may be nullable according to the OpenAPI specification. }}
5+
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}{{#defaultValue}} = {{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}}

0 commit comments

Comments
 (0)