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 2fc9871b9f59..5f5601968e98 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 @@ -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 { @@ -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 diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/data_class.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/data_class.mustache index ea72ac7c116d..0f8615de28d9 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-server/data_class.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-server/data_class.mustache @@ -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}} diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/ApiKeyAuth.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/ApiKeyAuth.kt.mustache index c2cad7fe7a7f..542744384a71 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/ApiKeyAuth.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/ApiKeyAuth.kt.mustache @@ -1,4 +1,4 @@ -package org.openapitools.server.infrastructure +package {{packageName}}.infrastructure import io.ktor.http.auth.* import io.ktor.server.application.* diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/AppMain.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/AppMain.kt.mustache index 5ba3775c92cf..870ec720e139 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/AppMain.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/AppMain.kt.mustache @@ -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.* @@ -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}} @@ -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 @@ -125,7 +121,7 @@ fun Application.main() { {{/authMethods}} } {{/hasAuthMethods}} - install(Routing) { + routing { {{#apiInfo}} {{#apis}} {{#operations}} @@ -134,6 +130,5 @@ fun Application.main() { {{/apis}} {{/apiInfo}} } - {{/generateApis}} } diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/Paths.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/Paths.kt.mustache index a575ea678772..48c8a7e49d88 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/Paths.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/Paths.kt.mustache @@ -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}} diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/_principal.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/_principal.mustache index 17c32a4c40a2..ad01dd85aa75 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/_principal.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/_principal.mustache @@ -1,10 +1,10 @@ {{#authMethods}} {{#isBasicBasic}} -val principal = call.authentication.principal()!! +val principal = call.authentication.principal() {{/isBasicBasic}}{{^isBasicBasic}}{{#isApiKey}} -val principal = call.authentication.principal()!! +val principal = call.authentication.principal() {{/isApiKey}}{{^isApiKey}}{{#isOAuth}} -val principal = call.authentication.principal()!! +val principal = call.authentication.principal() {{/isOAuth}}{{^isOAuth}} val principal = null!! {{/isOAuth}}{{/isApiKey}}{{/isBasicBasic}} diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/_response.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/_response.mustache index 931dad98f5e1..323f266e8256 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/_response.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/_response.mustache @@ -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) } \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/api.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/api.mustache index eb1da3bd527d..885ba6ae50dc 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/api.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/api.mustache @@ -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.* @@ -23,7 +22,6 @@ import {{packageName}}.infrastructure.ApiPrincipal {{#operations}} fun Route.{{classname}}() { - val gson = Gson() val empty = mutableMapOf() {{#operation}} diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/build.gradle.kts.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/build.gradle.kts.mustache index 3dc3dd38f043..268c54c5093a 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/build.gradle.kts.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/build.gradle.kts.mustache @@ -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 { @@ -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}} diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/gradle.properties b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/gradle.properties index d507b58c1c87..fb877a9edd28 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/gradle.properties +++ b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/gradle.properties @@ -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 \ No newline at end of file diff --git a/samples/server/petstore/kotlin-server-modelMutable/build.gradle.kts b/samples/server/petstore/kotlin-server-modelMutable/build.gradle.kts index fb35b77164a8..ac3166e2f3a2 100644 --- a/samples/server/petstore/kotlin-server-modelMutable/build.gradle.kts +++ b/samples/server/petstore/kotlin-server-modelMutable/build.gradle.kts @@ -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 { @@ -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") diff --git a/samples/server/petstore/kotlin-server-modelMutable/gradle.properties b/samples/server/petstore/kotlin-server-modelMutable/gradle.properties index d507b58c1c87..fb877a9edd28 100644 --- a/samples/server/petstore/kotlin-server-modelMutable/gradle.properties +++ b/samples/server/petstore/kotlin-server-modelMutable/gradle.properties @@ -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 \ No newline at end of file diff --git a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/AppMain.kt b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/AppMain.kt index 2de9d58ce937..5ea257e8c473 100644 --- a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/AppMain.kt +++ b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/AppMain.kt @@ -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.* @@ -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) { @@ -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 @@ -66,10 +58,9 @@ fun Application.main() { } } } - install(Routing) { + routing { PetApi() StoreApi() UserApi() } - } diff --git a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/Paths.kt b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/Paths.kt index effea4faa7aa..96b4afcaa488 100644 --- a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/Paths.kt +++ b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/Paths.kt @@ -21,7 +21,7 @@ object Paths { * * @param body Pet object that needs to be added to the store */ - @Serializable @Resource("/pet") class addPet(val body: Pet) + @Resource("/pet") class addPet() /** * Deletes a pet @@ -29,35 +29,35 @@ object Paths { * @param petId Pet id to delete * @param apiKey (optional) */ - @Serializable @Resource("/pet/{petId}") class deletePet(val petId: kotlin.Long, val apiKey: kotlin.String? = null) + @Resource("/pet/{petId}") class deletePet(val petId: kotlin.Long) /** * Finds Pets by status * Multiple status values can be provided with comma separated strings * @param status Status values that need to be considered for filter */ - @Serializable @Resource("/pet/findByStatus") class findPetsByStatus(val status: kotlin.collections.MutableList) + @Resource("/pet/findByStatus") class findPetsByStatus() /** * Finds Pets by tags * Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. * @param tags Tags to filter by */ - @Serializable @Resource("/pet/findByTags") class findPetsByTags(val tags: kotlin.collections.MutableList) + @Resource("/pet/findByTags") class findPetsByTags() /** * Find pet by ID * Returns a single pet * @param petId ID of pet to return */ - @Serializable @Resource("/pet/{petId}") class getPetById(val petId: kotlin.Long) + @Resource("/pet/{petId}") class getPetById(val petId: kotlin.Long) /** * Update an existing pet * * @param body Pet object that needs to be added to the store */ - @Serializable @Resource("/pet") class updatePet(val body: Pet) + @Resource("/pet") class updatePet() /** * Updates a pet in the store with form data @@ -66,7 +66,7 @@ object Paths { * @param name Updated name of the pet (optional) * @param status Updated status of the pet (optional) */ - @Serializable @Resource("/pet/{petId}") class updatePetWithForm(val petId: kotlin.Long, val name: kotlin.String? = null, val status: kotlin.String? = null) + @Resource("/pet/{petId}") class updatePetWithForm(val petId: kotlin.Long) /** * uploads an image @@ -75,69 +75,69 @@ object Paths { * @param additionalMetadata Additional data to pass to server (optional) * @param file file to upload (optional) */ - @Serializable @Resource("/pet/{petId}/uploadImage") class uploadFile(val petId: kotlin.Long, val additionalMetadata: kotlin.String? = null, val file: java.io.File? = null) + @Resource("/pet/{petId}/uploadImage") class uploadFile(val petId: kotlin.Long) /** * Delete purchase order by ID * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors * @param orderId ID of the order that needs to be deleted */ - @Serializable @Resource("/store/order/{orderId}") class deleteOrder(val orderId: kotlin.String) + @Resource("/store/order/{orderId}") class deleteOrder(val orderId: kotlin.String) /** * Returns pet inventories by status * Returns a map of status codes to quantities */ - @Serializable @Resource("/store/inventory") class getInventory + @Resource("/store/inventory") class getInventory /** * Find purchase order by ID * For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions * @param orderId ID of pet that needs to be fetched */ - @Serializable @Resource("/store/order/{orderId}") class getOrderById(val orderId: kotlin.Long) + @Resource("/store/order/{orderId}") class getOrderById(val orderId: kotlin.Long) /** * Place an order for a pet * * @param body order placed for purchasing the pet */ - @Serializable @Resource("/store/order") class placeOrder(val body: Order) + @Resource("/store/order") class placeOrder() /** * Create user * This can only be done by the logged in user. * @param body Created user object */ - @Serializable @Resource("/user") class createUser(val body: User) + @Resource("/user") class createUser() /** * Creates list of users with given input array * * @param body List of user object */ - @Serializable @Resource("/user/createWithArray") class createUsersWithArrayInput(val body: kotlin.collections.MutableList) + @Resource("/user/createWithArray") class createUsersWithArrayInput() /** * Creates list of users with given input array * * @param body List of user object */ - @Serializable @Resource("/user/createWithList") class createUsersWithListInput(val body: kotlin.collections.MutableList) + @Resource("/user/createWithList") class createUsersWithListInput() /** * Delete user * This can only be done by the logged in user. * @param username The name that needs to be deleted */ - @Serializable @Resource("/user/{username}") class deleteUser(val username: kotlin.String) + @Resource("/user/{username}") class deleteUser(val username: kotlin.String) /** * Get user by user name * * @param username The name that needs to be fetched. Use user1 for testing. */ - @Serializable @Resource("/user/{username}") class getUserByName(val username: kotlin.String) + @Resource("/user/{username}") class getUserByName(val username: kotlin.String) /** * Logs user into the system @@ -145,13 +145,13 @@ object Paths { * @param username The user name for login * @param password The password for login in clear text */ - @Serializable @Resource("/user/login") class loginUser(val username: kotlin.String, val password: kotlin.String) + @Resource("/user/login") class loginUser() /** * Logs out current logged in user session * */ - @Serializable @Resource("/user/logout") class logoutUser + @Resource("/user/logout") class logoutUser /** * Updated user @@ -159,6 +159,6 @@ object Paths { * @param username name that need to be deleted * @param body Updated user object */ - @Serializable @Resource("/user/{username}") class updateUser(val username: kotlin.String, val body: User) + @Resource("/user/{username}") class updateUser(val username: kotlin.String) } diff --git a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/apis/PetApi.kt b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/apis/PetApi.kt index d95a4b4d0883..b72ec0d72cec 100644 --- a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/apis/PetApi.kt +++ b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/apis/PetApi.kt @@ -11,7 +11,6 @@ */ package org.openapitools.server.apis -import com.google.gson.Gson import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.auth.* @@ -30,13 +29,12 @@ import org.openapitools.server.models.ModelApiResponse import org.openapitools.server.models.Pet fun Route.PetApi() { - val gson = Gson() val empty = mutableMapOf() authenticate("petstore_auth") { post { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() call.respond(HttpStatusCode.NotImplemented) @@ -47,7 +45,7 @@ fun Route.PetApi() { authenticate("petstore_auth") { delete { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() call.respond(HttpStatusCode.NotImplemented) @@ -58,7 +56,7 @@ fun Route.PetApi() { authenticate("petstore_auth") { get { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() val exampleContentType = "application/json" @@ -97,7 +95,7 @@ fun Route.PetApi() { } ]""" 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) } @@ -108,7 +106,7 @@ fun Route.PetApi() { authenticate("petstore_auth") { get { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() val exampleContentType = "application/json" @@ -147,7 +145,7 @@ fun Route.PetApi() { } ]""" 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) } @@ -158,7 +156,7 @@ fun Route.PetApi() { authenticate("api_key") { get { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() val exampleContentType = "application/json" @@ -181,7 +179,7 @@ fun Route.PetApi() { }""" 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) } @@ -192,7 +190,7 @@ fun Route.PetApi() { authenticate("petstore_auth") { put { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() call.respond(HttpStatusCode.NotImplemented) @@ -203,7 +201,7 @@ fun Route.PetApi() { authenticate("petstore_auth") { post { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() call.respond(HttpStatusCode.NotImplemented) @@ -214,7 +212,7 @@ fun Route.PetApi() { authenticate("petstore_auth") { post { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() val exampleContentType = "application/json" @@ -225,7 +223,7 @@ fun Route.PetApi() { }""" 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) } diff --git a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/apis/StoreApi.kt b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/apis/StoreApi.kt index fe780f7feb46..4a4395778401 100644 --- a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/apis/StoreApi.kt +++ b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/apis/StoreApi.kt @@ -11,7 +11,6 @@ */ package org.openapitools.server.apis -import com.google.gson.Gson import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.auth.* @@ -29,7 +28,6 @@ import org.openapitools.server.infrastructure.ApiPrincipal import org.openapitools.server.models.Order fun Route.StoreApi() { - val gson = Gson() val empty = mutableMapOf() delete { @@ -40,7 +38,7 @@ fun Route.StoreApi() { authenticate("api_key") { get { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() call.respond(HttpStatusCode.NotImplemented) @@ -60,7 +58,7 @@ fun Route.StoreApi() { }""" 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) } @@ -79,7 +77,7 @@ fun Route.StoreApi() { }""" 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) } diff --git a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/apis/UserApi.kt b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/apis/UserApi.kt index ccf0509fca2e..299f743e141d 100644 --- a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/apis/UserApi.kt +++ b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/apis/UserApi.kt @@ -11,7 +11,6 @@ */ package org.openapitools.server.apis -import com.google.gson.Gson import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.auth.* @@ -29,7 +28,6 @@ import org.openapitools.server.infrastructure.ApiPrincipal import org.openapitools.server.models.User fun Route.UserApi() { - val gson = Gson() val empty = mutableMapOf() post { @@ -66,7 +64,7 @@ fun Route.UserApi() { }""" 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) } diff --git a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/Category.kt b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/Category.kt index 949cf3a7a5a0..82813cdbff3e 100644 --- a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/Category.kt +++ b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/Category.kt @@ -12,13 +12,15 @@ package org.openapitools.server.models +import kotlinx.serialization.Serializable /** * A category for a pet * @param id * @param name */ +@Serializable data class Category( var id: kotlin.Long? = null, var name: kotlin.String? = null -) +) diff --git a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt index 7a0ac1f246df..dcc1ed13510b 100644 --- a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt +++ b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt @@ -12,15 +12,17 @@ package org.openapitools.server.models +import kotlinx.serialization.Serializable /** * Describes the result of uploading an image resource * @param code * @param type * @param message */ +@Serializable data class ModelApiResponse( var code: kotlin.Int? = null, var type: kotlin.String? = null, var message: kotlin.String? = null -) +) diff --git a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/Order.kt b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/Order.kt index c691b645c43d..d5a9b56748f7 100644 --- a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/Order.kt +++ b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/Order.kt @@ -12,6 +12,7 @@ package org.openapitools.server.models +import kotlinx.serialization.Serializable /** * An order for a pets from the pet store * @param id @@ -21,15 +22,16 @@ package org.openapitools.server.models * @param status Order Status * @param complete */ +@Serializable data class Order( var id: kotlin.Long? = null, var petId: kotlin.Long? = null, var quantity: kotlin.Int? = null, - var shipDate: java.time.OffsetDateTime? = null, + var shipDate: kotlin.String? = null, /* Order Status */ var status: Order.Status? = null, var complete: kotlin.Boolean? = false -) +) { /** * Order Status diff --git a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/Pet.kt b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/Pet.kt index e12a818c1b57..e9095e0a7c60 100644 --- a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/Pet.kt +++ b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/Pet.kt @@ -14,6 +14,7 @@ package org.openapitools.server.models import org.openapitools.server.models.Category import org.openapitools.server.models.Tag +import kotlinx.serialization.Serializable /** * A pet for sale in the pet store * @param name @@ -23,6 +24,7 @@ import org.openapitools.server.models.Tag * @param tags * @param status pet status in the store */ +@Serializable data class Pet( var name: kotlin.String, var photoUrls: kotlin.collections.MutableList, @@ -31,7 +33,7 @@ data class Pet( var tags: kotlin.collections.MutableList? = null, /* pet status in the store */ var status: Pet.Status? = null -) +) { /** * pet status in the store diff --git a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/Tag.kt b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/Tag.kt index 99e770cd7b9f..26a16d0acc37 100644 --- a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/Tag.kt +++ b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/Tag.kt @@ -12,13 +12,15 @@ package org.openapitools.server.models +import kotlinx.serialization.Serializable /** * A tag for a pet * @param id * @param name */ +@Serializable data class Tag( var id: kotlin.Long? = null, var name: kotlin.String? = null -) +) diff --git a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/User.kt b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/User.kt index e63e2596d4bf..57f4e607386a 100644 --- a/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/User.kt +++ b/samples/server/petstore/kotlin-server-modelMutable/src/main/kotlin/org/openapitools/server/models/User.kt @@ -12,6 +12,7 @@ package org.openapitools.server.models +import kotlinx.serialization.Serializable /** * A User who is purchasing from the pet store * @param id @@ -23,6 +24,7 @@ package org.openapitools.server.models * @param phone * @param userStatus User Status */ +@Serializable data class User( var id: kotlin.Long? = null, var username: kotlin.String? = null, @@ -33,5 +35,5 @@ data class User( var phone: kotlin.String? = null, /* User Status */ var userStatus: kotlin.Int? = null -) +) diff --git a/samples/server/petstore/kotlin-server-required-and-nullable-properties/src/main/kotlin/org/openapitools/server/models/Pet.kt b/samples/server/petstore/kotlin-server-required-and-nullable-properties/src/main/kotlin/org/openapitools/server/models/Pet.kt index 8dddb90062b1..5266d4aaa2f1 100644 --- a/samples/server/petstore/kotlin-server-required-and-nullable-properties/src/main/kotlin/org/openapitools/server/models/Pet.kt +++ b/samples/server/petstore/kotlin-server-required-and-nullable-properties/src/main/kotlin/org/openapitools/server/models/Pet.kt @@ -12,6 +12,7 @@ package org.openapitools.server.models +import kotlinx.serialization.Serializable /** * * @param notNullableRequired @@ -19,10 +20,11 @@ package org.openapitools.server.models * @param nullableNotRequired * @param notNullableNotRequired */ +@Serializable data class Pet( val notNullableRequired: kotlin.String, val nullableRequired: kotlin.String?, val nullableNotRequired: kotlin.String? = null, val notNullableNotRequired: kotlin.String? = null -) +) diff --git a/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/Category.kt b/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/Category.kt index 71098339bc80..0c3ccfcfe850 100644 --- a/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/Category.kt +++ b/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/Category.kt @@ -12,13 +12,15 @@ package org.openapitools.server.models +import kotlinx.serialization.Serializable /** * A category for a pet * @param id * @param name */ +@Serializable data class Category( val id: kotlin.Long? = null, val name: kotlin.String? = null -) +) diff --git a/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt b/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt index 9e80c50c6604..5ecc51991934 100644 --- a/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt +++ b/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt @@ -12,15 +12,17 @@ package org.openapitools.server.models +import kotlinx.serialization.Serializable /** * Describes the result of uploading an image resource * @param code * @param type * @param message */ +@Serializable data class ModelApiResponse( val code: kotlin.Int? = null, val type: kotlin.String? = null, val message: kotlin.String? = null -) +) diff --git a/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/Order.kt b/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/Order.kt index be802beb8d80..d015eb6b780a 100644 --- a/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/Order.kt +++ b/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/Order.kt @@ -12,6 +12,7 @@ package org.openapitools.server.models +import kotlinx.serialization.Serializable /** * An order for a pets from the pet store * @param id @@ -21,6 +22,7 @@ package org.openapitools.server.models * @param status Order Status * @param complete */ +@Serializable data class Order( val id: kotlin.Long? = null, val petId: kotlin.Long? = null, @@ -29,7 +31,7 @@ data class Order( /* Order Status */ val status: Order.Status? = null, val complete: kotlin.Boolean? = false -) +) { /** * Order Status diff --git a/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/Pet.kt b/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/Pet.kt index e5723fedd633..929d729b09bb 100644 --- a/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/Pet.kt +++ b/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/Pet.kt @@ -14,6 +14,7 @@ package org.openapitools.server.models import org.openapitools.server.models.Category import org.openapitools.server.models.Tag +import kotlinx.serialization.Serializable /** * A pet for sale in the pet store * @param name @@ -23,6 +24,7 @@ import org.openapitools.server.models.Tag * @param tags * @param status pet status in the store */ +@Serializable data class Pet( val name: kotlin.String, val photoUrls: kotlin.collections.List, @@ -31,7 +33,7 @@ data class Pet( val tags: kotlin.collections.List? = null, /* pet status in the store */ val status: Pet.Status? = null -) +) { /** * pet status in the store diff --git a/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/Tag.kt b/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/Tag.kt index 0a975290485e..76749aca3f4d 100644 --- a/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/Tag.kt +++ b/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/Tag.kt @@ -12,13 +12,15 @@ package org.openapitools.server.models +import kotlinx.serialization.Serializable /** * A tag for a pet * @param id * @param name */ +@Serializable data class Tag( val id: kotlin.Long? = null, val name: kotlin.String? = null -) +) diff --git a/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/User.kt b/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/User.kt index a4742582f463..26757f921f43 100644 --- a/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/User.kt +++ b/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/models/User.kt @@ -12,6 +12,7 @@ package org.openapitools.server.models +import kotlinx.serialization.Serializable /** * A User who is purchasing from the pet store * @param id @@ -23,6 +24,7 @@ package org.openapitools.server.models * @param phone * @param userStatus User Status */ +@Serializable data class User( val id: kotlin.Long? = null, val username: kotlin.String? = null, @@ -33,5 +35,5 @@ data class User( val phone: kotlin.String? = null, /* User Status */ val userStatus: kotlin.Int? = null -) +) diff --git a/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/Category.kt b/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/Category.kt index 71098339bc80..0c3ccfcfe850 100644 --- a/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/Category.kt +++ b/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/Category.kt @@ -12,13 +12,15 @@ package org.openapitools.server.models +import kotlinx.serialization.Serializable /** * A category for a pet * @param id * @param name */ +@Serializable data class Category( val id: kotlin.Long? = null, val name: kotlin.String? = null -) +) diff --git a/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt b/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt index 9e80c50c6604..5ecc51991934 100644 --- a/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt +++ b/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt @@ -12,15 +12,17 @@ package org.openapitools.server.models +import kotlinx.serialization.Serializable /** * Describes the result of uploading an image resource * @param code * @param type * @param message */ +@Serializable data class ModelApiResponse( val code: kotlin.Int? = null, val type: kotlin.String? = null, val message: kotlin.String? = null -) +) diff --git a/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/Order.kt b/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/Order.kt index be802beb8d80..d015eb6b780a 100644 --- a/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/Order.kt +++ b/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/Order.kt @@ -12,6 +12,7 @@ package org.openapitools.server.models +import kotlinx.serialization.Serializable /** * An order for a pets from the pet store * @param id @@ -21,6 +22,7 @@ package org.openapitools.server.models * @param status Order Status * @param complete */ +@Serializable data class Order( val id: kotlin.Long? = null, val petId: kotlin.Long? = null, @@ -29,7 +31,7 @@ data class Order( /* Order Status */ val status: Order.Status? = null, val complete: kotlin.Boolean? = false -) +) { /** * Order Status diff --git a/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/Pet.kt b/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/Pet.kt index e5723fedd633..929d729b09bb 100644 --- a/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/Pet.kt +++ b/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/Pet.kt @@ -14,6 +14,7 @@ package org.openapitools.server.models import org.openapitools.server.models.Category import org.openapitools.server.models.Tag +import kotlinx.serialization.Serializable /** * A pet for sale in the pet store * @param name @@ -23,6 +24,7 @@ import org.openapitools.server.models.Tag * @param tags * @param status pet status in the store */ +@Serializable data class Pet( val name: kotlin.String, val photoUrls: kotlin.collections.List, @@ -31,7 +33,7 @@ data class Pet( val tags: kotlin.collections.List? = null, /* pet status in the store */ val status: Pet.Status? = null -) +) { /** * pet status in the store diff --git a/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/Tag.kt b/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/Tag.kt index 0a975290485e..76749aca3f4d 100644 --- a/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/Tag.kt +++ b/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/Tag.kt @@ -12,13 +12,15 @@ package org.openapitools.server.models +import kotlinx.serialization.Serializable /** * A tag for a pet * @param id * @param name */ +@Serializable data class Tag( val id: kotlin.Long? = null, val name: kotlin.String? = null -) +) diff --git a/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/User.kt b/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/User.kt index a4742582f463..26757f921f43 100644 --- a/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/User.kt +++ b/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/models/User.kt @@ -12,6 +12,7 @@ package org.openapitools.server.models +import kotlinx.serialization.Serializable /** * A User who is purchasing from the pet store * @param id @@ -23,6 +24,7 @@ package org.openapitools.server.models * @param phone * @param userStatus User Status */ +@Serializable data class User( val id: kotlin.Long? = null, val username: kotlin.String? = null, @@ -33,5 +35,5 @@ data class User( val phone: kotlin.String? = null, /* User Status */ val userStatus: kotlin.Int? = null -) +) diff --git a/samples/server/petstore/kotlin-server/ktor/build.gradle.kts b/samples/server/petstore/kotlin-server/ktor/build.gradle.kts index fb35b77164a8..ac3166e2f3a2 100644 --- a/samples/server/petstore/kotlin-server/ktor/build.gradle.kts +++ b/samples/server/petstore/kotlin-server/ktor/build.gradle.kts @@ -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 { @@ -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") diff --git a/samples/server/petstore/kotlin-server/ktor/gradle.properties b/samples/server/petstore/kotlin-server/ktor/gradle.properties index d507b58c1c87..fb877a9edd28 100644 --- a/samples/server/petstore/kotlin-server/ktor/gradle.properties +++ b/samples/server/petstore/kotlin-server/ktor/gradle.properties @@ -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 \ No newline at end of file diff --git a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/AppMain.kt b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/AppMain.kt index 2de9d58ce937..5ea257e8c473 100644 --- a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/AppMain.kt +++ b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/AppMain.kt @@ -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.* @@ -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) { @@ -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 @@ -66,10 +58,9 @@ fun Application.main() { } } } - install(Routing) { + routing { PetApi() StoreApi() UserApi() } - } diff --git a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/Paths.kt b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/Paths.kt index 4a9c3bb01944..96b4afcaa488 100644 --- a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/Paths.kt +++ b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/Paths.kt @@ -21,7 +21,7 @@ object Paths { * * @param body Pet object that needs to be added to the store */ - @Serializable @Resource("/pet") class addPet(val body: Pet) + @Resource("/pet") class addPet() /** * Deletes a pet @@ -29,35 +29,35 @@ object Paths { * @param petId Pet id to delete * @param apiKey (optional) */ - @Serializable @Resource("/pet/{petId}") class deletePet(val petId: kotlin.Long, val apiKey: kotlin.String? = null) + @Resource("/pet/{petId}") class deletePet(val petId: kotlin.Long) /** * Finds Pets by status * Multiple status values can be provided with comma separated strings * @param status Status values that need to be considered for filter */ - @Serializable @Resource("/pet/findByStatus") class findPetsByStatus(val status: kotlin.collections.List) + @Resource("/pet/findByStatus") class findPetsByStatus() /** * Finds Pets by tags * Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. * @param tags Tags to filter by */ - @Serializable @Resource("/pet/findByTags") class findPetsByTags(val tags: kotlin.collections.List) + @Resource("/pet/findByTags") class findPetsByTags() /** * Find pet by ID * Returns a single pet * @param petId ID of pet to return */ - @Serializable @Resource("/pet/{petId}") class getPetById(val petId: kotlin.Long) + @Resource("/pet/{petId}") class getPetById(val petId: kotlin.Long) /** * Update an existing pet * * @param body Pet object that needs to be added to the store */ - @Serializable @Resource("/pet") class updatePet(val body: Pet) + @Resource("/pet") class updatePet() /** * Updates a pet in the store with form data @@ -66,7 +66,7 @@ object Paths { * @param name Updated name of the pet (optional) * @param status Updated status of the pet (optional) */ - @Serializable @Resource("/pet/{petId}") class updatePetWithForm(val petId: kotlin.Long, val name: kotlin.String? = null, val status: kotlin.String? = null) + @Resource("/pet/{petId}") class updatePetWithForm(val petId: kotlin.Long) /** * uploads an image @@ -75,69 +75,69 @@ object Paths { * @param additionalMetadata Additional data to pass to server (optional) * @param file file to upload (optional) */ - @Serializable @Resource("/pet/{petId}/uploadImage") class uploadFile(val petId: kotlin.Long, val additionalMetadata: kotlin.String? = null, val file: java.io.File? = null) + @Resource("/pet/{petId}/uploadImage") class uploadFile(val petId: kotlin.Long) /** * Delete purchase order by ID * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors * @param orderId ID of the order that needs to be deleted */ - @Serializable @Resource("/store/order/{orderId}") class deleteOrder(val orderId: kotlin.String) + @Resource("/store/order/{orderId}") class deleteOrder(val orderId: kotlin.String) /** * Returns pet inventories by status * Returns a map of status codes to quantities */ - @Serializable @Resource("/store/inventory") class getInventory + @Resource("/store/inventory") class getInventory /** * Find purchase order by ID * For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions * @param orderId ID of pet that needs to be fetched */ - @Serializable @Resource("/store/order/{orderId}") class getOrderById(val orderId: kotlin.Long) + @Resource("/store/order/{orderId}") class getOrderById(val orderId: kotlin.Long) /** * Place an order for a pet * * @param body order placed for purchasing the pet */ - @Serializable @Resource("/store/order") class placeOrder(val body: Order) + @Resource("/store/order") class placeOrder() /** * Create user * This can only be done by the logged in user. * @param body Created user object */ - @Serializable @Resource("/user") class createUser(val body: User) + @Resource("/user") class createUser() /** * Creates list of users with given input array * * @param body List of user object */ - @Serializable @Resource("/user/createWithArray") class createUsersWithArrayInput(val body: kotlin.collections.List) + @Resource("/user/createWithArray") class createUsersWithArrayInput() /** * Creates list of users with given input array * * @param body List of user object */ - @Serializable @Resource("/user/createWithList") class createUsersWithListInput(val body: kotlin.collections.List) + @Resource("/user/createWithList") class createUsersWithListInput() /** * Delete user * This can only be done by the logged in user. * @param username The name that needs to be deleted */ - @Serializable @Resource("/user/{username}") class deleteUser(val username: kotlin.String) + @Resource("/user/{username}") class deleteUser(val username: kotlin.String) /** * Get user by user name * * @param username The name that needs to be fetched. Use user1 for testing. */ - @Serializable @Resource("/user/{username}") class getUserByName(val username: kotlin.String) + @Resource("/user/{username}") class getUserByName(val username: kotlin.String) /** * Logs user into the system @@ -145,13 +145,13 @@ object Paths { * @param username The user name for login * @param password The password for login in clear text */ - @Serializable @Resource("/user/login") class loginUser(val username: kotlin.String, val password: kotlin.String) + @Resource("/user/login") class loginUser() /** * Logs out current logged in user session * */ - @Serializable @Resource("/user/logout") class logoutUser + @Resource("/user/logout") class logoutUser /** * Updated user @@ -159,6 +159,6 @@ object Paths { * @param username name that need to be deleted * @param body Updated user object */ - @Serializable @Resource("/user/{username}") class updateUser(val username: kotlin.String, val body: User) + @Resource("/user/{username}") class updateUser(val username: kotlin.String) } diff --git a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/apis/PetApi.kt b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/apis/PetApi.kt index d95a4b4d0883..b72ec0d72cec 100644 --- a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/apis/PetApi.kt +++ b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/apis/PetApi.kt @@ -11,7 +11,6 @@ */ package org.openapitools.server.apis -import com.google.gson.Gson import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.auth.* @@ -30,13 +29,12 @@ import org.openapitools.server.models.ModelApiResponse import org.openapitools.server.models.Pet fun Route.PetApi() { - val gson = Gson() val empty = mutableMapOf() authenticate("petstore_auth") { post { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() call.respond(HttpStatusCode.NotImplemented) @@ -47,7 +45,7 @@ fun Route.PetApi() { authenticate("petstore_auth") { delete { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() call.respond(HttpStatusCode.NotImplemented) @@ -58,7 +56,7 @@ fun Route.PetApi() { authenticate("petstore_auth") { get { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() val exampleContentType = "application/json" @@ -97,7 +95,7 @@ fun Route.PetApi() { } ]""" 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) } @@ -108,7 +106,7 @@ fun Route.PetApi() { authenticate("petstore_auth") { get { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() val exampleContentType = "application/json" @@ -147,7 +145,7 @@ fun Route.PetApi() { } ]""" 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) } @@ -158,7 +156,7 @@ fun Route.PetApi() { authenticate("api_key") { get { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() val exampleContentType = "application/json" @@ -181,7 +179,7 @@ fun Route.PetApi() { }""" 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) } @@ -192,7 +190,7 @@ fun Route.PetApi() { authenticate("petstore_auth") { put { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() call.respond(HttpStatusCode.NotImplemented) @@ -203,7 +201,7 @@ fun Route.PetApi() { authenticate("petstore_auth") { post { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() call.respond(HttpStatusCode.NotImplemented) @@ -214,7 +212,7 @@ fun Route.PetApi() { authenticate("petstore_auth") { post { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() val exampleContentType = "application/json" @@ -225,7 +223,7 @@ fun Route.PetApi() { }""" 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) } diff --git a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/apis/StoreApi.kt b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/apis/StoreApi.kt index fe780f7feb46..4a4395778401 100644 --- a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/apis/StoreApi.kt +++ b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/apis/StoreApi.kt @@ -11,7 +11,6 @@ */ package org.openapitools.server.apis -import com.google.gson.Gson import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.auth.* @@ -29,7 +28,6 @@ import org.openapitools.server.infrastructure.ApiPrincipal import org.openapitools.server.models.Order fun Route.StoreApi() { - val gson = Gson() val empty = mutableMapOf() delete { @@ -40,7 +38,7 @@ fun Route.StoreApi() { authenticate("api_key") { get { - val principal = call.authentication.principal()!! + val principal = call.authentication.principal() call.respond(HttpStatusCode.NotImplemented) @@ -60,7 +58,7 @@ fun Route.StoreApi() { }""" 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) } @@ -79,7 +77,7 @@ fun Route.StoreApi() { }""" 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) } diff --git a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/apis/UserApi.kt b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/apis/UserApi.kt index ccf0509fca2e..299f743e141d 100644 --- a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/apis/UserApi.kt +++ b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/apis/UserApi.kt @@ -11,7 +11,6 @@ */ package org.openapitools.server.apis -import com.google.gson.Gson import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.auth.* @@ -29,7 +28,6 @@ import org.openapitools.server.infrastructure.ApiPrincipal import org.openapitools.server.models.User fun Route.UserApi() { - val gson = Gson() val empty = mutableMapOf() post { @@ -66,7 +64,7 @@ fun Route.UserApi() { }""" 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) } diff --git a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/Category.kt b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/Category.kt index 9d8571921384..7647c2d227e0 100644 --- a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/Category.kt +++ b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/Category.kt @@ -12,19 +12,17 @@ package org.openapitools.server.models -import java.io.Serializable +import kotlinx.serialization.Serializable /** * A category for a pet * @param id * @param name */ +@Serializable data class Category( val id: kotlin.Long? = null, val name: kotlin.String? = null -) : Serializable +) { - companion object { - private const val serialVersionUID: Long = 123 - } } diff --git a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt index 5548175d9048..e16d42e86a4e 100644 --- a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt +++ b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt @@ -12,21 +12,19 @@ package org.openapitools.server.models -import java.io.Serializable +import kotlinx.serialization.Serializable /** * Describes the result of uploading an image resource * @param code * @param type * @param message */ +@Serializable data class ModelApiResponse( val code: kotlin.Int? = null, val type: kotlin.String? = null, val message: kotlin.String? = null -) : Serializable +) { - companion object { - private const val serialVersionUID: Long = 123 - } } diff --git a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/Order.kt b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/Order.kt index 79dce95e1e47..b1a711510fc6 100644 --- a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/Order.kt +++ b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/Order.kt @@ -12,7 +12,7 @@ package org.openapitools.server.models -import java.io.Serializable +import kotlinx.serialization.Serializable /** * An order for a pets from the pet store * @param id @@ -22,19 +22,17 @@ import java.io.Serializable * @param status Order Status * @param complete */ +@Serializable data class Order( val id: kotlin.Long? = null, val petId: kotlin.Long? = null, val quantity: kotlin.Int? = null, - val shipDate: java.time.OffsetDateTime? = null, + val shipDate: kotlin.String? = null, /* Order Status */ val status: Order.Status? = null, val complete: kotlin.Boolean? = false -) : Serializable +) { - companion object { - private const val serialVersionUID: Long = 123 - } /** * Order Status * Values: placed,approved,delivered diff --git a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/Pet.kt b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/Pet.kt index 8b98ce0469c5..8fe934cc3ff3 100644 --- a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/Pet.kt +++ b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/Pet.kt @@ -14,7 +14,7 @@ package org.openapitools.server.models import org.openapitools.server.models.Category import org.openapitools.server.models.Tag -import java.io.Serializable +import kotlinx.serialization.Serializable /** * A pet for sale in the pet store * @param name @@ -24,6 +24,7 @@ import java.io.Serializable * @param tags * @param status pet status in the store */ +@Serializable data class Pet( val name: kotlin.String, val photoUrls: kotlin.collections.List, @@ -32,11 +33,8 @@ data class Pet( val tags: kotlin.collections.List? = null, /* pet status in the store */ val status: Pet.Status? = null -) : Serializable +) { - companion object { - private const val serialVersionUID: Long = 123 - } /** * pet status in the store * Values: available,pending,sold diff --git a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/Tag.kt b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/Tag.kt index e9626b25c6d1..fae4d9d0ba3c 100644 --- a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/Tag.kt +++ b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/Tag.kt @@ -12,19 +12,17 @@ package org.openapitools.server.models -import java.io.Serializable +import kotlinx.serialization.Serializable /** * A tag for a pet * @param id * @param name */ +@Serializable data class Tag( val id: kotlin.Long? = null, val name: kotlin.String? = null -) : Serializable +) { - companion object { - private const val serialVersionUID: Long = 123 - } } diff --git a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/User.kt b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/User.kt index fdbdb4e1cff4..af04fd61c1cf 100644 --- a/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/User.kt +++ b/samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/models/User.kt @@ -12,7 +12,7 @@ package org.openapitools.server.models -import java.io.Serializable +import kotlinx.serialization.Serializable /** * A User who is purchasing from the pet store * @param id @@ -24,6 +24,7 @@ import java.io.Serializable * @param phone * @param userStatus User Status */ +@Serializable data class User( val id: kotlin.Long? = null, val username: kotlin.String? = null, @@ -34,10 +35,7 @@ data class User( val phone: kotlin.String? = null, /* User Status */ val userStatus: kotlin.Int? = null -) : Serializable +) { - companion object { - private const val serialVersionUID: Long = 123 - } }