Skip to content

Commit 3c038ca

Browse files
committed
improve example and tests
1 parent 5575174 commit 3c038ca

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

examples/src/main/kotlin/io/github/smiley4/ktoropenapi/examples/TypesafeRouting.kt

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ private fun Application.myModule() {
5151

5252
routing {
5353

54+
// add the routes for the api-spec, swagger-ui and redoc
5455
route("api.json") {
5556
openApi()
5657
}
@@ -61,19 +62,21 @@ private fun Application.myModule() {
6162
redoc("/api.json")
6263
}
6364

64-
get<PetsRoute.All>({
65-
description = "custom description"
66-
request {
67-
queryParameter<String>("tags") {
68-
description = "sample description"
69-
}
70-
}
71-
}) { request ->
65+
// query and path parameters are picked up automatically and added to the schema with the correct name and type
66+
get<PetsRoute.All> { request ->
7267
println("..${request.tags}, ${request.limit}")
7368
call.respond(HttpStatusCode.NotImplemented, Unit)
7469
}
7570

76-
get<PetsRoute.Id> { request ->
71+
// additional information can be added to the route manually as usual.
72+
// automatically added information can also be overwritten this way.
73+
get<PetsRoute.Id>({
74+
request {
75+
pathParameter<Long>("id") {
76+
description = "the id of the pet"
77+
}
78+
}
79+
}) { request ->
7780
println("..${request.id}")
7881
call.respond(HttpStatusCode.NotImplemented, Unit)
7982
}
@@ -99,7 +102,7 @@ class PetsRoute {
99102
@Resource("/")
100103
class All(
101104
val parent: PetsRoute,
102-
val tags: List<String> = emptyList(),
105+
val tags: List<String>?,
103106
val limit: Int = 100
104107
)
105108

ktor-openapi/src/main/kotlin/io/github/smiley4/ktoropenapi/resources/documentationExtractor.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,17 @@ private fun collectParameters(descriptor: SerialDescriptor, path: String): List<
4848
if (!elementDescriptor.isInline && elementDescriptor.kind is StructureKind.CLASS) {
4949
parameters.addAll(collectParameters(elementDescriptor, path))
5050
} else {
51+
val location = getLocation(name, path)
5152
parameters.add(
5253
ParameterData(
5354
name = name,
5455
descriptor = elementDescriptor,
55-
optional = path.contains("{$name?}"),
56-
location = getLocation(name, path)
56+
optional = when(location) {
57+
ParameterLocation.PATH -> path.contains("{$name?}")
58+
ParameterLocation.QUERY -> elementDescriptor.isNullable || descriptor.isElementOptional(index)
59+
else -> false
60+
},
61+
location = location
5762
)
5863
)
5964
}

ktor-openapi/src/test/kotlin/io/github/smiley4/ktorswaggerui/misc/RouteDocumentationMergerTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ class RouteDocumentationMergerTest : StringSpec({
103103
"query",
104104
"pathA1",
105105
"pathA2",
106-
"query",
107106
"pathB1",
108107
"pathB2"
109108
)

0 commit comments

Comments
 (0)