Skip to content

Commit

Permalink
[Kotlin Server] Update Ktor to version 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Stexxe committed Dec 4, 2024
1 parent 71ccc88 commit aa45441
Show file tree
Hide file tree
Showing 49 changed files with 187 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,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 @@ -353,6 +358,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 KOTLIN_SERIALIZATION = "kotlinSerialization";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
{{#parcelizeModels}}
import android.os.Parcelable
import kotlinx.parcelize.Parcelize

{{/parcelizeModels}}
{{#serializableModel}}
import java.io.Serializable
{{/serializableModel}}
import kotlinx.serialization.Serializable
/**
* {{{description}}}
{{#vars}}
* @param {{{name}}} {{{description}}}
{{/vars}}
*/
{{#parcelizeModels}}
@Parcelize
{{/parcelizeModels}}
@Serializable
{{#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}}
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,19 @@ import java.util.concurrent.TimeUnit
{{/featureMetrics}}
import io.ktor.server.routing.*
{{#hasAuthMethods}}
{{#kotlinSerialization}}
import io.ktor.serialization.kotlinx.json.json
{{/kotlinSerialization}}
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 +56,9 @@ fun Application.main() {
{{/featureMetrics}}
{{#generateApis}}
install(ContentNegotiation) {
register(ContentType.Application.Json, GsonConverter())
{{#kotlinSerialization}}
json()
{{/kotlinSerialization}}
}
{{#featureAutoHead}}
install(AutoHeadResponse) // see https://ktor.io/docs/autoheadresponse.html
Expand Down Expand Up @@ -125,7 +121,7 @@ fun Application.main() {
{{/authMethods}}
}
{{/hasAuthMethods}}
install(Routing) {
routing {
{{#apiInfo}}
{{#apis}}
{{#operations}}
Expand All @@ -134,6 +130,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}}({{#pathParams}}val {{paramName}}: {{{dataType}}}{{^required}}? = null{{/required}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^-last}}, {{/-last}}{{/pathParams}})
{{/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"
id("io.ktor.plugin") version "3.0.2"
kotlin("plugin.serialization") version "2.0.20"
}

application {
Expand Down Expand Up @@ -35,7 +36,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
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"
id("io.ktor.plugin") version "3.0.2"
kotlin("plugin.serialization") version "2.0.20"
}

application {
Expand All @@ -29,7 +30,7 @@ dependencies {
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.openapitools.server

import io.ktor.server.application.*
import io.ktor.serialization.gson.*
import io.ktor.http.*
import io.ktor.server.resources.*
import io.ktor.server.plugins.autohead.*
Expand All @@ -24,12 +23,6 @@ import org.openapitools.server.apis.StoreApi
import org.openapitools.server.apis.UserApi


internal val settings = HoconApplicationConfig(ConfigFactory.defaultApplication(HTTP::class.java.classLoader))

object HTTP {
val client = HttpClient(Apache)
}

fun Application.main() {
install(DefaultHeaders)
install(DropwizardMetrics) {
Expand All @@ -41,7 +34,6 @@ fun Application.main() {
reporter.start(10, TimeUnit.SECONDS)
}
install(ContentNegotiation) {
register(ContentType.Application.Json, GsonConverter())
}
install(AutoHeadResponse) // see https://ktor.io/docs/autoheadresponse.html
install(Compression, ApplicationCompressionConfiguration()) // see https://ktor.io/docs/compression.html
Expand All @@ -66,10 +58,9 @@ fun Application.main() {
}
}
}
install(Routing) {
routing {
PetApi()
StoreApi()
UserApi()
}

}
Loading

0 comments on commit aa45441

Please sign in to comment.