Skip to content

Commit

Permalink
[Kotlin Server] Update Ktor to the next major version 3 (#20245)
Browse files Browse the repository at this point in the history
* [Kotlin Server] Update Ktor to version 3

* [Kotlin Server] Clean up

* [Kotlin Server] Fix problems

* [Kotlin Server] Fix Ktor and modelMutable samples

* [Kotlin Server] Fix the string value for serialization key

* [Kotlin Server] Remove setting Kotlin serialization flag to true

* [Kotlin Server] Remove setting Kotlin serialization flag to true

* [Kotlin Server] Remove the option to enable Kotlin serialization

* [Kotlin Server] Use query parameters
  • Loading branch information
Stexxe authored Dec 14, 2024
1 parent 3e6f875 commit f766f44
Show file tree
Hide file tree
Showing 50 changed files with 183 additions and 271 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ public void processOpts() {
LOGGER.info("`library` option is empty. Default to {}", DEFAULT_LIBRARY);
}

if (isKtor()) {
typeMapping.put("date-time", "kotlin.String");
typeMapping.put("DateTime", "kotlin.String");
}

if (additionalProperties.containsKey(Constants.AUTOMATIC_HEAD_REQUESTS)) {
setAutoHeadFeatureEnabled(convertPropertyToBooleanAndWriteBack(Constants.AUTOMATIC_HEAD_REQUESTS));
} else {
Expand Down Expand Up @@ -298,6 +303,8 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("gradle.properties", "", "gradle.properties"));

if (isKtor()) {
additionalProperties.put(Constants.IS_KTOR, true);

supportingFiles.add(new SupportingFile("AppMain.kt.mustache", packageFolder, "AppMain.kt"));
supportingFiles.add(new SupportingFile("Configuration.kt.mustache", packageFolder, "Configuration.kt"));

Expand Down Expand Up @@ -365,6 +372,7 @@ public static class Constants {
public static final String USE_MUTINY_DESC = "Whether to use Mutiny (should not be used with useCoroutines). This option is currently supported only when using jaxrs-spec library.";
public static final String OMIT_GRADLE_WRAPPER = "omitGradleWrapper";
public static final String OMIT_GRADLE_WRAPPER_DESC = "Whether to omit Gradle wrapper for creating a sub project.";
public static final String IS_KTOR = "isKtor";
}

@Override
Expand All @@ -386,6 +394,13 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
if (operations != null && !Objects.equals(library, Constants.JAXRS_SPEC)) {
List<CodegenOperation> ops = operations.getOperation();
ops.forEach(operation -> {
if (isKtor()) {
ArrayList<CodegenParameter> params = new ArrayList<>();
params.addAll(operation.pathParams);
params.addAll(operation.queryParams);
operation.vendorExtensions.put("ktor-params", params);
}

List<CodegenResponse> responses = operation.responses;
if (responses != null) {
responses.forEach(resp -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{{#parcelizeModels}}
{{#isKtor}}
import kotlinx.serialization.Serializable
{{/isKtor}}
{{^isKtor}}
{{#parcelizeModels}}
import android.os.Parcelable
import kotlinx.parcelize.Parcelize

{{/parcelizeModels}}
{{#serializableModel}}
{{/parcelizeModels}}
{{#serializableModel}}
import java.io.Serializable
{{/serializableModel}}
{{/serializableModel}}
{{/isKtor}}
/**
* {{{description}}}
{{#vars}}
Expand All @@ -15,21 +19,19 @@ import java.io.Serializable
{{#parcelizeModels}}
@Parcelize
{{/parcelizeModels}}
{{#isKtor}}
@Serializable
{{/isKtor}}
{{#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}}
){{^isKtor}}{{^serializableModel}}{{#parcelizeModels}} : Parcelable{{/parcelizeModels}}{{/serializableModel}}{{^parcelizeModels}}{{#serializableModel}}: Serializable {{/serializableModel}}{{/parcelizeModels}}{{#parcelizeModels}}{{#serializableModel}} : Parcelable, Serializable {{/serializableModel}}{{/parcelizeModels}}{{/isKtor}}
{{#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}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openapitools.server.infrastructure
package {{packageName}}.infrastructure

import io.ktor.http.auth.*
import io.ktor.server.application.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package {{packageName}}

import io.ktor.server.application.*
import io.ktor.serialization.gson.*
import io.ktor.http.*
{{#featureResources}}
import io.ktor.server.resources.*
Expand Down Expand Up @@ -30,24 +29,17 @@ import java.util.concurrent.TimeUnit
{{/featureMetrics}}
import io.ktor.server.routing.*
{{#hasAuthMethods}}
import io.ktor.serialization.kotlinx.json.json
import com.typesafe.config.ConfigFactory
import io.ktor.client.HttpClient
import io.ktor.client.engine.apache.Apache
import io.ktor.server.config.HoconApplicationConfig
import io.ktor.server.auth.*
import org.openapitools.server.infrastructure.*
import {{packageName}}.infrastructure.*
{{/hasAuthMethods}}
{{#generateApis}}{{#apiInfo}}{{#apis}}import {{apiPackage}}.{{classname}}
{{/apis}}{{/apiInfo}}{{/generateApis}}

{{#hasAuthMethods}}
internal val settings = HoconApplicationConfig(ConfigFactory.defaultApplication(HTTP::class.java.classLoader))

object HTTP {
val client = HttpClient(Apache)
}
{{/hasAuthMethods}}

fun Application.main() {
install(DefaultHeaders)
{{#featureMetrics}}
Expand All @@ -62,7 +54,7 @@ fun Application.main() {
{{/featureMetrics}}
{{#generateApis}}
install(ContentNegotiation) {
register(ContentType.Application.Json, GsonConverter())
json()
}
{{#featureAutoHead}}
install(AutoHeadResponse) // see https://ktor.io/docs/autoheadresponse.html
Expand Down Expand Up @@ -125,7 +117,7 @@ fun Application.main() {
{{/authMethods}}
}
{{/hasAuthMethods}}
install(Routing) {
routing {
{{#apiInfo}}
{{#apis}}
{{#operations}}
Expand All @@ -134,6 +126,5 @@ fun Application.main() {
{{/apis}}
{{/apiInfo}}
}

{{/generateApis}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ object Paths {
{{#allParams}}* @param {{paramName}} {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}*/
{{#hasParams}}
@Serializable @Resource("{{{path}}}") class {{operationId}}({{#allParams}}val {{paramName}}: {{{dataType}}}{{^required}}? = null{{/required}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}})
@Resource("{{{path}}}") class {{operationId}}({{#vendorExtensions.ktor-params}}val {{paramName}}: {{{dataType}}}{{^required}}? = null{{/required}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^-last}}, {{/-last}}{{/vendorExtensions.ktor-params}})
{{/hasParams}}
{{^hasParams}}
@Serializable @Resource("{{{path}}}") class {{operationId}}
@Resource("{{{path}}}") class {{operationId}}
{{/hasParams}}

{{/operation}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{{#authMethods}}
{{#isBasicBasic}}
val principal = call.authentication.principal<UserIdPrincipal>()!!
val principal = call.authentication.principal<UserIdPrincipal>()
{{/isBasicBasic}}{{^isBasicBasic}}{{#isApiKey}}
val principal = call.authentication.principal<ApiPrincipal>()!!
val principal = call.authentication.principal<ApiPrincipal>()
{{/isApiKey}}{{^isApiKey}}{{#isOAuth}}
val principal = call.authentication.principal<OAuthAccessTokenResponse>()!!
val principal = call.authentication.principal<OAuthAccessTokenResponse>()
{{/isOAuth}}{{^isOAuth}}
val principal = null!!
{{/isOAuth}}{{/isApiKey}}{{/isBasicBasic}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ val exampleContentType = "{{{contentType}}}"
val exampleContentString = """{{&example}}"""

when (exampleContentType) {
"application/json" -> call.respond(gson.fromJson(exampleContentString, empty::class.java))
"application/json" -> call.respondText(exampleContentType, ContentType.Application.Json)
"application/xml" -> call.respondText(exampleContentString, ContentType.Text.Xml)
else -> call.respondText(exampleContentString)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{{>licenseInfo}}
package {{apiPackage}}

import com.google.gson.Gson
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.auth.*
Expand All @@ -23,7 +22,6 @@ import {{packageName}}.infrastructure.ApiPrincipal

{{#operations}}
fun Route.{{classname}}() {
val gson = Gson()
val empty = mutableMapOf<String, Any?>()
{{#operation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ version = "{{artifactVersion}}"

plugins {
kotlin("jvm") version "2.0.20"
id("io.ktor.plugin") version "2.3.12"
application
kotlin("plugin.serialization") version "2.0.20"
}

application {
Expand All @@ -22,6 +23,7 @@ repositories {
}

dependencies {
implementation(platform("io.ktor:ktor-bom:3.0.2"))
implementation("ch.qos.logback:logback-classic:$logback_version")
{{#hasAuthMethods}}
implementation("com.typesafe:config:1.4.1")
Expand All @@ -35,7 +37,7 @@ dependencies {
{{/featureAutoHead}}
implementation("io.ktor:ktor-server-default-headers")
implementation("io.ktor:ktor-server-content-negotiation")
implementation("io.ktor:ktor-serialization-gson")
implementation("io.ktor:ktor-serialization-kotlinx-json")
{{#featureResources}}
implementation("io.ktor:ktor-server-resources")
{{/featureResources}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
kotlin.code.style=official
ktor_version=2.3.12
ktor_version=3.0.2
kotlin_version=2.0.20
logback_version=1.4.14
74 changes: 0 additions & 74 deletions samples/server/petstore/kotlin-server-modelMutable/build.gradle

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ version = "1.0.0"

plugins {
kotlin("jvm") version "2.0.20"
id("io.ktor.plugin") version "2.3.12"
application
kotlin("plugin.serialization") version "2.0.20"
}

application {
Expand All @@ -22,14 +23,15 @@ repositories {
}

dependencies {
implementation(platform("io.ktor:ktor-bom:3.0.2"))
implementation("ch.qos.logback:logback-classic:$logback_version")
implementation("com.typesafe:config:1.4.1")
implementation("io.ktor:ktor-server-auth")
implementation("io.ktor:ktor-client-apache")
implementation("io.ktor:ktor-server-auto-head-response")
implementation("io.ktor:ktor-server-default-headers")
implementation("io.ktor:ktor-server-content-negotiation")
implementation("io.ktor:ktor-serialization-gson")
implementation("io.ktor:ktor-serialization-kotlinx-json")
implementation("io.ktor:ktor-server-resources")
implementation("io.ktor:ktor-server-hsts")
implementation("io.ktor:ktor-server-compression")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
kotlin.code.style=official
ktor_version=2.3.12
ktor_version=3.0.2
kotlin_version=2.0.20
logback_version=1.4.14
Loading

0 comments on commit f766f44

Please sign in to comment.