Skip to content

Commit c3130ed

Browse files
committed
update sample project, compiler config options descriptions and invalid deps
1 parent 2422d37 commit c3130ed

File tree

7 files changed

+90
-31
lines changed

7 files changed

+90
-31
lines changed

codeSnippets/snippets/openapi-spec-gen/README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ extension of the Ktor Gradle plugin.
55

66
> This sample is a part of the [`codeSnippets`](../../README.md) Gradle project.
77
8-
## Generate the OpenAPI specification
9-
10-
```bash
11-
./gradlew :openapi-spec-gen:buildOpenApi
12-
```
13-
14-
The generated OpenAPI specification is located in `build/ktor/openapi/generated.json`.
15-
168
## Run the application
179

1810
To run the application, execute the following command in the repository's root directory:
@@ -21,4 +13,8 @@ To run the application, execute the following command in the repository's root d
2113
./gradlew :openapi-spec-gen:run
2214
```
2315

24-
Navigate to [http://0.0.0.0:8080/docs](http://0.0.0.0:8080/docs) to access the OpenAPI documentation.
16+
To view the OpenAPI documentation, navigate to the following URLs:
17+
18+
- [http://0.0.0.0:8080/docs.json](http://0.0.0.0:8080/docs.json)
19+
- [http://0.0.0.0:8080/openApi](http://0.0.0.0:8080/openApi) to view the OpenAPI UI for the API spec.
20+
- [http://0.0.0.0:8080/swaggerUI](http://0.0.0.0:8080/swaggerUI) to view the Swagger UI for the API spec.

codeSnippets/snippets/openapi-spec-gen/build.gradle.kts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
val ktor_version = "3.4.0-eap-1505" //TODO: replace with project version
1+
val ktor_version = "3.4.0-eap-1511"
22
val kotlin_version: String by project
33
val logback_version: String by project
44

@@ -14,27 +14,25 @@ application {
1414

1515
repositories {
1616
mavenCentral()
17-
maven { url = uri("https://maven.pkg.jetbrains.space/public/p/ktor/eap") }
17+
maven("https://oss.sonatype.org/content/repositories/snapshots")
1818
}
1919

2020
ktor {
21-
@OptIn(io.ktor.plugin.OpenApiPreview::class)
2221
openApi {
23-
title = "OpenAPI example"
24-
version = "2.1"
25-
summary = "This is a sample API"
22+
enabled = true
23+
codeInferenceEnabled = true
24+
onlyCommented = false
2625
}
2726
}
2827

29-
// Builds OpenAPI specification automatically
30-
tasks.processResources {
31-
dependsOn("buildOpenApi")
32-
}
3328

3429
dependencies {
3530
implementation("io.ktor:ktor-server-core:$ktor_version")
36-
implementation("io.ktor:ktor-server-routing-annotate:${ktor_version}")
37-
implementation("io.ktor:ktor-server-openapi:${ktor_version}")
31+
implementation("io.ktor:ktor-server-routing-annotate:$ktor_version")
32+
implementation("io.ktor:ktor-server-openapi:$ktor_version")
33+
implementation("io.ktor:ktor-server-content-negotiation:${ktor_version}")
34+
implementation("io.ktor:ktor-serialization-kotlinx-json:${ktor_version}")
35+
implementation("io.ktor:ktor-server-swagger:${ktor_version}")
3836
implementation("io.ktor:ktor-server-netty:$ktor_version")
3937
implementation("ch.qos.logback:logback-classic:$logback_version")
4038
testImplementation("io.ktor:ktor-server-test-host-jvm:$ktor_version")

codeSnippets/snippets/openapi-spec-gen/src/main/kotlin/com/example/Application.kt

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,82 @@
11
package com.example
22

3+
import io.ktor.annotate.OpenApiDocSource
34
import io.ktor.annotate.annotate
5+
import io.ktor.annotate.generateOpenApiDoc
46
import io.ktor.http.*
7+
import io.ktor.openapi.OpenApiDoc
8+
import io.ktor.openapi.OpenApiInfo
59
import io.ktor.openapi.jsonSchema
10+
import io.ktor.serialization.kotlinx.json.json
611
import io.ktor.server.application.*
12+
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
713
import io.ktor.server.plugins.openapi.*
14+
import io.ktor.server.plugins.swagger.swaggerUI
815
import io.ktor.server.request.*
916
import io.ktor.server.response.*
1017
import io.ktor.server.routing.*
1118
import kotlinx.serialization.Serializable
19+
import kotlinx.serialization.json.Json
1220

1321
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
1422

1523
fun Application.module() {
24+
install(ContentNegotiation) {
25+
json()
26+
json(Json {
27+
encodeDefaults = false
28+
})
29+
}
30+
routing {
31+
// Main page for marketing
32+
get("/") {
33+
call.respondText("<html><body><h1>Hello, World</h1></body></html>", ContentType.Text.Html)
34+
}
35+
36+
/**
37+
* API endpoints for users.
38+
*
39+
* These will appear in the resulting OpenAPI document.
40+
*/
41+
val apiRoute = userCrud()
42+
43+
get("/docs.json") {
44+
val docs = generateOpenApiDoc(
45+
OpenApiDoc(info = OpenApiInfo("My API", "1.0")),
46+
apiRoute.descendants()
47+
)
48+
call.respond(docs)
49+
}
50+
51+
/**
52+
* View the generated UI for the API spec.
53+
*/
54+
openAPI("/openApi"){
55+
outputPath = "docs/routes"
56+
// title, version, etc.
57+
info = OpenApiInfo("My API from routes", "1.0.0")
58+
// which routes to read from to build the model
59+
// by default, it will check for `openapi/documentation.yaml` then use the routing root as a fallback
60+
source = OpenApiDocSource.RoutingSource(ContentType.Application.Json) {
61+
apiRoute.descendants()
62+
}
63+
}
64+
65+
/**
66+
* View the Swagger flavor of the UI for the API spec.
67+
*/
68+
swaggerUI("/swaggerUI") {
69+
info = OpenApiInfo("My API", "1.0")
70+
source = OpenApiDocSource.RoutingSource(ContentType.Application.Json) {
71+
apiRoute.descendants()
72+
}
73+
}
74+
}
75+
}
76+
77+
private fun Unit.descendants(): Sequence<Route> {}
78+
79+
fun Application.userCrud() {
1680
routing {
1781
/**
1882
* Hello, world.
@@ -119,6 +183,5 @@ fun Application.module() {
119183
)
120184
}
121185
}
122-
123186
@Serializable
124187
data class User(val id: ULong, val name: String)

codeSnippets/snippets/tutorial-server-db-integration/gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "lo
2323
ktor-server-config-yaml = { module = "io.ktor:ktor-server-config-yaml-jvm", version.ref = "ktor-version" }
2424
ktor-server-test-host = { module = "io.ktor:ktor-server-test-host-jvm", version.ref = "ktor-version" }
2525
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin-version" }
26-
ktor-client-content-negotiation = { module = "io.ktor:ktor-client-content-negotiation-jvm", version.ref = "ktor-version" }
26+
ktor-client-content-negotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor-version" }
2727

2828
[plugins]
2929
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin-version" }

codeSnippets/snippets/tutorial-server-docker-compose/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ dependencies {
3939
implementation("ch.qos.logback:logback-classic:$logback_version")
4040
implementation("io.ktor:ktor-server-config-yaml:$ktor_version")
4141
testImplementation("io.ktor:ktor-server-test-host-jvm")
42-
testImplementation("io.ktor:ktor-client-content-negotiation-jvm:$ktor_version")
42+
testImplementation("io.ktor:ktor-client-content-negotiation:$ktor_version")
4343
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
4444
}

codeSnippets/snippets/tutorial-server-websockets/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ dependencies {
3232
implementation("ch.qos.logback:logback-classic:$logback_version")
3333
testImplementation("io.ktor:ktor-server-test-host-jvm")
3434
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
35-
testImplementation("io.ktor:ktor-client-content-negotiation-jvm:$ktor_version")
35+
testImplementation("io.ktor:ktor-client-content-negotiation:$ktor_version")
3636
}

topics/openapi-spec-generation.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,19 @@ ktor {
7676
<deflist>
7777
<def>
7878
<title><code>enabled</code></title>
79-
Enables or disables OpenAPI metadata generation during compilation.
79+
Enables or disables OpenAPI route annotation code generation. Defaults to <code>false</code>.
8080
</def>
8181
<def>
8282
<title><code>codeInferenceEnabled</code></title>
83-
Controls whether the compiler attempts to infer OpenAPI metadata from routing code. Disable this option if inference
84-
produces incorrect results or if you prefer to define metadata explicitly using annotations.
85-
For more details, see <a href="#code-inference">Code inference rules</a>.
83+
Controls whether the compiler attempts to infer OpenAPI metadata from routing code. Defaults to <code>true</code>.
84+
Disable this option if inference produces incorrect results or if you prefer to define metadata explicitly using
85+
annotations.
86+
For more details, see <a href="#code-inference">the code inference rules</a>.
8687
</def>
8788
<def>
8889
<title><code>onlyCommented</code></title>
89-
Limits metadata generation to routes that contain comment annotations. By default, all routes are processed.
90+
Limits metadata generation to routes that contain comment annotations. Defaults to <code>false</code>, meaning all
91+
routing calls are processed except those explicitly marked with <code>@ignore</code>.
9092
</def>
9193
</deflist>
9294

@@ -95,7 +97,7 @@ Limits metadata generation to routes that contain comment annotations. By defaul
9597
The Ktor compiler plugin analyzes your server routing DSL to determine the structural shape of your API. This analysis
9698
is based solely on route declarations and does not inspect the contents of route handlers.
9799

98-
From routing declarations, the compiler determines:
100+
The following is automatically inferred from the selectors in the routing API tree:
99101
- Merged paths (for example, `/api/v1/users/{id}`).
100102
- HTTP methods (such as `GET` and `POST`).
101103
- Path parameters.

0 commit comments

Comments
 (0)