Skip to content

Commit b473958

Browse files
committed
updated kotlin-server sample
1 parent 5ca80c5 commit b473958

File tree

18 files changed

+463
-552
lines changed

18 files changed

+463
-552
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.4.3-SNAPSHOT
1+
2.4.31-SNAPSHOT

samples/server/petstore/kotlin-server/ktor/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.
44

5-
Generated by Swagger Codegen 2.4.3-SNAPSHOT (2019-03-06T09:14:32.633-05:00).
5+
Generated by Swagger Codegen 2.4.31-SNAPSHOT (2023-03-28T23:54:30.861-05:00).
66

77
## Requires
88

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
group 'io.swagger'
22
version '1.0.0'
33

4-
task wrapper(type: Wrapper) {
5-
gradleVersion = '4.3'
4+
wrapper {
5+
gradleVersion = '7.5'
66
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
77
}
88

99
buildscript {
10-
ext.kotlin_version = '1.2.10'
11-
ext.ktor_version = '0.9.1-alpha-9'
12-
ext.shadow_version = '2.0.2'
10+
ext.kotlin_version = '1.8.0'
11+
ext.ktor_version = "1.5.4"
12+
ext.shadow_version = "6.1.0"
1313

1414
repositories {
15-
mavenCentral()
15+
maven {
16+
url "https://repo1.maven.org/maven2"
17+
}
1618
maven {
1719
url "https://plugins.gradle.org/m2/"
1820
}
@@ -29,8 +31,6 @@ apply plugin: 'application'
2931

3032
mainClassName = "io.ktor.server.netty.DevelopmentEngine"
3133

32-
// Initialization order with shadow 2.0.1 and Gradle 4.3 is weird.
33-
// See https://github.com/johnrengelman/shadow/issues/336#issuecomment-355402508
3434
apply plugin: 'com.github.johnrengelman.shadow'
3535

3636
sourceCompatibility = 1.8
@@ -43,12 +43,6 @@ compileTestKotlin {
4343
kotlinOptions.jvmTarget = "1.8"
4444
}
4545

46-
kotlin {
47-
experimental {
48-
coroutines "enable"
49-
}
50-
}
51-
5246
shadowJar {
5347
baseName = 'kotlin-server'
5448
classifier = null
@@ -62,13 +56,13 @@ repositories {
6256
}
6357

6458
dependencies {
65-
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
66-
compile "io.ktor:ktor-server-netty:$ktor_version"
67-
compile "io.ktor:ktor-metrics:$ktor_version"
68-
compile "io.ktor:ktor-locations:$ktor_version"
69-
compile "io.ktor:ktor-gson:$ktor_version"
70-
compile "io.ktor:ktor-client-core:$ktor_version"
71-
compile "io.ktor:ktor-client-apache:$ktor_version"
72-
compile "ch.qos.logback:logback-classic:1.2.9"
73-
testCompile group: 'junit', name: 'junit', version: '4.12'
59+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
60+
implementation "io.ktor:ktor-server-netty:$ktor_version"
61+
implementation "io.ktor:ktor-metrics:$ktor_version"
62+
implementation "io.ktor:ktor-locations:$ktor_version"
63+
implementation "io.ktor:ktor-gson:$ktor_version"
64+
implementation "io.ktor:ktor-client-core:$ktor_version"
65+
implementation "io.ktor:ktor-client-apache:$ktor_version"
66+
implementation "ch.qos.logback:logback-classic:1.2.9"
67+
testImplementation group: 'junit', name: 'junit', version: '4.12'
7468
}
Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
11
package io.swagger.server
22

3-
import com.codahale.metrics.*
3+
import com.codahale.metrics.Slf4jReporter
44
import com.typesafe.config.ConfigFactory
5-
import io.ktor.application.*
5+
import io.ktor.application.Application
6+
import io.ktor.application.ApplicationStopping
7+
import io.ktor.application.install
8+
import io.ktor.application.log
69
import io.ktor.client.HttpClient
710
import io.ktor.client.engine.apache.Apache
811
import io.ktor.config.HoconApplicationConfig
9-
import io.ktor.features.*
12+
import io.ktor.features.AutoHeadResponse
13+
import io.ktor.features.Compression
14+
import io.ktor.features.ContentNegotiation
15+
import io.ktor.features.DefaultHeaders
16+
import io.ktor.features.HSTS
1017
import io.ktor.gson.GsonConverter
1118
import io.ktor.http.ContentType
12-
import io.ktor.locations.*
13-
import io.ktor.metrics.*
14-
import io.ktor.routing.*
15-
import java.util.concurrent.*
16-
import io.swagger.server.apis.*
19+
import io.ktor.locations.KtorExperimentalLocationsAPI
20+
import io.ktor.locations.Locations
21+
import io.ktor.routing.Routing
22+
import java.util.concurrent.TimeUnit
23+
import io.ktor.util.KtorExperimentalAPI
24+
import io.ktor.auth.Authentication
25+
import io.ktor.auth.oauth
26+
import io.ktor.auth.basic
27+
import io.ktor.auth.UserIdPrincipal
28+
import io.ktor.metrics.dropwizard.DropwizardMetrics
29+
import io.swagger.server.infrastructure.ApiKeyCredential
30+
import io.swagger.server.infrastructure.ApiPrincipal
31+
import io.swagger.server.infrastructure.apiKeyAuth
32+
import io.swagger.server.apis.PetApi
33+
import io.swagger.server.apis.StoreApi
34+
import io.swagger.server.apis.UserApi
1735

1836

37+
@KtorExperimentalAPI
1938
internal val settings = HoconApplicationConfig(ConfigFactory.defaultApplication(HTTP::class.java.classLoader))
2039

2140
object HTTP {
2241
val client = HttpClient(Apache)
2342
}
2443

44+
@KtorExperimentalAPI
45+
@KtorExperimentalLocationsAPI
2546
fun Application.main() {
2647
install(DefaultHeaders)
27-
install(Metrics) {
48+
install(DropwizardMetrics) {
2849
val reporter = Slf4jReporter.forRegistry(registry)
2950
.outputTo(log)
3051
.convertRatesTo(TimeUnit.SECONDS)
@@ -39,14 +60,34 @@ fun Application.main() {
3960
install(HSTS, ApplicationHstsConfiguration()) // see http://ktor.io/features/hsts.html
4061
install(Compression, ApplicationCompressionConfiguration()) // see http://ktor.io/features/compression.html
4162
install(Locations) // see http://ktor.io/features/locations.html
63+
install(Authentication) {
64+
// "Implement API key auth (api_key) for parameter name 'api_key'."
65+
apiKeyAuth("api_key") {
66+
validate { apikeyCredential: ApiKeyCredential ->
67+
when {
68+
apikeyCredential.value == "keyboardcat" -> ApiPrincipal(apikeyCredential)
69+
else -> null
70+
}
71+
}
72+
}
73+
oauth("petstore_auth") {
74+
client = HttpClient(Apache)
75+
providerLookup = { ApplicationAuthProviders["petstore_auth"] }
76+
urlProvider = { _ ->
77+
// TODO: define a callback url here.
78+
"/"
79+
}
80+
}
81+
}
4282
install(Routing) {
4383
PetApi()
4484
StoreApi()
4585
UserApi()
4686
}
4787

88+
4889
environment.monitor.subscribe(ApplicationStopping)
4990
{
5091
HTTP.client.close()
5192
}
52-
}
93+
}

samples/server/petstore/kotlin-server/ktor/src/main/kotlin/io/swagger/server/Configuration.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ package io.swagger.server
22

33
// Use this file to hold package-level internal functions that return receiver object passed to the `install` method.
44
import io.ktor.auth.OAuthServerSettings
5-
import io.ktor.features.*
6-
import io.ktor.http.*
5+
import io.ktor.features.Compression
6+
import io.ktor.features.HSTS
7+
import io.ktor.features.deflate
8+
import io.ktor.features.gzip
9+
import io.ktor.features.maxAge
10+
import io.ktor.features.minimumSize
11+
import io.ktor.http.HttpMethod
12+
import io.ktor.util.KtorExperimentalAPI
713
import java.time.Duration
814
import java.util.concurrent.Executors
915

@@ -13,7 +19,7 @@ import io.swagger.server.settings
1319
/**
1420
* Application block for [HSTS] configuration.
1521
*
16-
* This file may be excluded in .swagger-codegen-ignore,
22+
* This file may be excluded in .openapi-generator-ignore,
1723
* and application specific configuration can be applied in this function.
1824
*
1925
* See http://ktor.io/features/hsts.html
@@ -32,7 +38,7 @@ internal fun ApplicationHstsConfiguration(): HSTS.Configuration.() -> Unit {
3238
/**
3339
* Application block for [Compression] configuration.
3440
*
35-
* This file may be excluded in .swagger-codegen-ignore,
41+
* This file may be excluded in .openapi-generator-ignore,
3642
* and application specific configuration can be applied in this function.
3743
*
3844
* See http://ktor.io/features/compression.html
@@ -50,6 +56,7 @@ internal fun ApplicationCompressionConfiguration(): Compression.Configuration.()
5056
}
5157

5258
// Defines authentication mechanisms used throughout the application.
59+
@KtorExperimentalAPI
5360
val ApplicationAuthProviders: Map<String, OAuthServerSettings> = listOf<OAuthServerSettings>(
5461
OAuthServerSettings.OAuth2ServerSettings(
5562
name = "petstore_auth",

samples/server/petstore/kotlin-server/ktor/src/main/kotlin/io/swagger/server/Paths.kt

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ package io.swagger.server
1313

1414
import io.ktor.application.ApplicationCall
1515
import io.ktor.http.HttpMethod
16-
import io.ktor.locations.*
17-
import io.ktor.pipeline.PipelineContext
16+
import io.ktor.locations.handle
17+
import io.ktor.locations.location
18+
import io.ktor.locations.Location
19+
import io.ktor.util.pipeline.PipelineContext
1820
import io.ktor.routing.Route
1921
import io.ktor.routing.method
20-
import io.swagger.server.models.*
22+
2123

2224

2325
// NOTE: [email protected] is missing extension for Route.delete. This includes it.
@@ -30,81 +32,104 @@ inline fun <reified T : Any> Route.delete(noinline body: suspend PipelineContext
3032
}
3133

3234
object Paths {
35+
/**
36+
* Add a new pet to the store
37+
*
38+
*/
39+
@Location("/pet") class addPet()
3340
/**
3441
* Deletes a pet
3542
*
36-
* @param petId Pet id to delete
37-
* @param apiKey (optional)
3843
*/
39-
@Location("/pet/{petId}") class deletePet(val petId: kotlin.Long, val apiKey: kotlin.String)
40-
44+
@Location("/pet/{petId}") class deletePet()
4145
/**
4246
* Finds Pets by status
4347
* Multiple status values can be provided with comma separated strings
44-
* @param status Status values that need to be considered for filter
4548
*/
46-
@Location("/pet/findByStatus") class findPetsByStatus(val status: kotlin.Array<kotlin.String>)
47-
49+
@Location("/pet/findByStatus") class findPetsByStatus()
4850
/**
4951
* Finds Pets by tags
5052
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
51-
* @param tags Tags to filter by
5253
*/
53-
@Location("/pet/findByTags") class findPetsByTags(val tags: kotlin.Array<kotlin.String>)
54-
54+
@Location("/pet/findByTags") class findPetsByTags()
5555
/**
5656
* Find pet by ID
5757
* Returns a single pet
58-
* @param petId ID of pet to return
5958
*/
60-
@Location("/pet/{petId}") class getPetById(val petId: kotlin.Long)
61-
59+
@Location("/pet/{petId}") class getPetById()
60+
/**
61+
* Update an existing pet
62+
*
63+
*/
64+
@Location("/pet") class updatePet()
65+
/**
66+
* Updates a pet in the store with form data
67+
*
68+
*/
69+
@Location("/pet/{petId}") class updatePetWithForm()
70+
/**
71+
* uploads an image
72+
*
73+
*/
74+
@Location("/pet/{petId}/uploadImage") class uploadFile()
6275
/**
6376
* Delete purchase order by ID
6477
* For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors
65-
* @param orderId ID of the order that needs to be deleted
6678
*/
67-
@Location("/store/order/{orderId}") class deleteOrder(val orderId: kotlin.String)
68-
79+
@Location("/store/order/{orderId}") class deleteOrder()
6980
/**
7081
* Returns pet inventories by status
7182
* Returns a map of status codes to quantities
7283
*/
7384
@Location("/store/inventory") class getInventory()
74-
7585
/**
7686
* Find purchase order by ID
7787
* For valid response try integer IDs with value &lt;&#x3D; 5 or &gt; 10. Other values will generated exceptions
78-
* @param orderId ID of pet that needs to be fetched
7988
*/
80-
@Location("/store/order/{orderId}") class getOrderById(val orderId: kotlin.Long)
81-
89+
@Location("/store/order/{orderId}") class getOrderById()
90+
/**
91+
* Place an order for a pet
92+
*
93+
*/
94+
@Location("/store/order") class placeOrder()
95+
/**
96+
* Create user
97+
* This can only be done by the logged in user.
98+
*/
99+
@Location("/user") class createUser()
100+
/**
101+
* Creates list of users with given input array
102+
*
103+
*/
104+
@Location("/user/createWithArray") class createUsersWithArrayInput()
105+
/**
106+
* Creates list of users with given input array
107+
*
108+
*/
109+
@Location("/user/createWithList") class createUsersWithListInput()
82110
/**
83111
* Delete user
84112
* This can only be done by the logged in user.
85-
* @param username The name that needs to be deleted
86113
*/
87-
@Location("/user/{username}") class deleteUser(val username: kotlin.String)
88-
114+
@Location("/user/{username}") class deleteUser()
89115
/**
90116
* Get user by user name
91117
*
92-
* @param username The name that needs to be fetched. Use user1 for testing.
93118
*/
94-
@Location("/user/{username}") class getUserByName(val username: kotlin.String)
95-
119+
@Location("/user/{username}") class getUserByName()
96120
/**
97121
* Logs user into the system
98122
*
99-
* @param username The user name for login
100-
* @param password The password for login in clear text
101123
*/
102-
@Location("/user/login") class loginUser(val username: kotlin.String, val password: kotlin.String)
103-
124+
@Location("/user/login") class loginUser()
104125
/**
105126
* Logs out current logged in user session
106127
*
107128
*/
108129
@Location("/user/logout") class logoutUser()
109-
130+
/**
131+
* Updated user
132+
* This can only be done by the logged in user.
133+
*/
134+
@Location("/user/{username}") class updateUser()
110135
}

0 commit comments

Comments
 (0)