Skip to content

Commit 5575174

Browse files
committed
add 'hidden'-property to request parameters and responses
1 parent 212cea8 commit 5575174

File tree

8 files changed

+41
-12
lines changed

8 files changed

+41
-12
lines changed

ktor-openapi/src/main/kotlin/io/github/smiley4/ktoropenapi/builder/openapi/OperationBuilder.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ internal class OperationBuilder(
2424
it.operationId = route.documentation.operationId
2525
it.deprecated = route.documentation.deprecated
2626
it.tags = operationTagsBuilder.build(route)
27-
it.parameters = route.documentation.request.parameters.map { param -> parameterBuilder.build(param) }
27+
it.parameters = route.documentation.request.parameters
28+
.filter { param -> !param.hidden }
29+
.map { param -> parameterBuilder.build(param) }
2830
route.documentation.request.body?.let { body ->
2931
it.requestBody = requestBodyBuilder.build(body)
3032
}

ktor-openapi/src/main/kotlin/io/github/smiley4/ktoropenapi/builder/openapi/ResponsesBuilder.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal class ResponsesBuilder(
1717
fun build(responses: List<ResponseData>, isProtected: Boolean): ApiResponses =
1818
ApiResponses().also {
1919
responses
20+
.filter { !it.hidden }
2021
.map { response -> responseBuilder.build(response) }
2122
.forEach { (name, response) -> it.addApiResponse(name, response) }
2223
if (shouldAddUnauthorized(responses, isProtected)) {
@@ -29,6 +30,7 @@ internal class ResponsesBuilder(
2930
private fun shouldAddUnauthorized(responses: List<ResponseData>, isProtected: Boolean): Boolean {
3031
val unauthorizedCode = HttpStatusCode.Unauthorized.value.toString()
3132
return config.securityConfig.defaultUnauthorizedResponse != null
33+
&& !config.securityConfig.defaultUnauthorizedResponse.hidden
3234
&& isProtected
3335
&& responses.count { it.statusCode == unauthorizedCode } == 0
3436
}

ktor-openapi/src/main/kotlin/io/github/smiley4/ktoropenapi/builder/route/RouteDocumentationMerger.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.smiley4.ktoropenapi.builder.route
22

3+
import io.github.smiley4.ktoropenapi.config.RequestConfig
4+
import io.github.smiley4.ktoropenapi.config.RequestParameterConfig
35
import io.github.smiley4.ktoropenapi.config.RouteConfig
46

57
internal class RouteDocumentationMerger {
@@ -10,7 +12,7 @@ internal class RouteDocumentationMerger {
1012
fun merge(a: RouteConfig, b: RouteConfig): RouteConfig {
1113
return RouteConfig().apply {
1214
specName = a.specName ?: b.specName
13-
tags = mutableListOf<String>().also {
15+
tags = mutableSetOf<String>().also {
1416
it.addAll(a.tags)
1517
it.addAll(b.tags)
1618
}
@@ -25,15 +27,17 @@ internal class RouteDocumentationMerger {
2527
hidden = a.hidden || b.hidden
2628
protected = a.protected ?: b.protected
2729
request {
28-
parameters.also {
29-
it.addAll(a.getRequest().parameters)
30-
it.addAll(b.getRequest().parameters)
31-
}
30+
buildMap {
31+
b.getRequest().parameters.forEach { this[it.name] = it }
32+
a.getRequest().parameters.forEach { this[it.name] = it }
33+
}.values.forEach { parameters.add(it) }
3234
setBody(a.getRequest().getBody() ?: b.getRequest().getBody())
3335
}
3436
response {
35-
b.getResponses().getResponses().forEach { response -> addResponse(response) }
36-
a.getResponses().getResponses().forEach { response -> addResponse(response) }
37+
buildMap {
38+
b.getResponses().getResponses().forEach { this[it.statusCode] = it }
39+
a.getResponses().getResponses().forEach { this[it.statusCode] = it }
40+
}.values.forEach { addResponse(it) }
3741
}
3842
}
3943
}

ktor-openapi/src/main/kotlin/io/github/smiley4/ktoropenapi/config/RequestParameterConfig.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,21 @@ class RequestParameterConfig(
3434
*/
3535
var example: ExampleDescriptor? = null
3636

37+
3738
/**
3839
* An example value for this parameter
3940
*/
4041
fun example(example: ExampleDescriptor) {
4142
this.example = example
4243
}
4344

45+
4446
/**
4547
* An example value for this parameter
4648
*/
4749
fun example(name: String, example: Example) = example(SwaggerExampleDescriptor(name, example))
4850

51+
4952
/**
5053
* An example value for this parameter
5154
*/
@@ -101,6 +104,13 @@ class RequestParameterConfig(
101104
*/
102105
var style: Parameter.StyleEnum? = null
103106

107+
108+
/**
109+
* Don't include this parameter in the openapi-spec
110+
*/
111+
var hidden: Boolean = false
112+
113+
104114
/**
105115
* Build the data object for this config.
106116
*/
@@ -115,7 +125,8 @@ class RequestParameterConfig(
115125
allowEmptyValue = allowEmptyValue,
116126
explode = explode ?: false,
117127
allowReserved = allowReserved,
118-
style = style
128+
style = style,
129+
hidden = hidden
119130
)
120131

121132
}

ktor-openapi/src/main/kotlin/io/github/smiley4/ktoropenapi/config/ResponseConfig.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,37 +59,46 @@ class ResponseConfig(val statusCode: String) {
5959
body = SimpleBodyConfig(type).apply(block)
6060
}
6161

62+
6263
/**
6364
* The body returned with this response
6465
*/
6566
fun body(type: Schema<*>, block: SimpleBodyConfig.() -> Unit = {}) = body(SwaggerTypeDescriptor(type), block)
6667

68+
6769
/**
6870
* The body returned with this response
6971
*/
7072
fun body(type: KType, block: SimpleBodyConfig.() -> Unit = {}) = body(KTypeDescriptor(type), block)
7173

74+
7275
/**
7376
* The body returned with this response
7477
*/
7578
inline fun <reified T> body(noinline block: SimpleBodyConfig.() -> Unit = {}) = body(KTypeDescriptor(typeOf<T>()), block)
7679

7780

78-
79-
8081
/**
8182
* The multipart-body returned with this response
8283
*/
8384
fun multipartBody(block: MultipartBodyConfig.() -> Unit) {
8485
body = MultipartBodyConfig().apply(block)
8586
}
8687

88+
89+
/**
90+
* Don't include this response in the openapi-spec
91+
*/
92+
var hidden: Boolean = false
93+
94+
8795
/**
8896
* Build the data object for this config.
8997
*/
9098
internal fun build() = ResponseData(
9199
statusCode = statusCode,
92100
description = description,
101+
hidden = hidden,
93102
headers = headers.mapValues { it.value.build() },
94103
body = body?.build()
95104
)

ktor-openapi/src/main/kotlin/io/github/smiley4/ktoropenapi/data/RequestParameterData.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ internal data class RequestParameterData(
2020
val explode: Boolean,
2121
val allowReserved: Boolean?,
2222
val style: Parameter.StyleEnum?,
23+
val hidden: Boolean,
2324
)

ktor-openapi/src/main/kotlin/io/github/smiley4/ktoropenapi/data/ResponseData.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package io.github.smiley4.ktoropenapi.data
66
internal data class ResponseData(
77
val statusCode: String,
88
val description: String?,
9+
val hidden: Boolean,
910
val headers: Map<String, HeaderData>,
1011
val body: BaseBodyData?,
1112
)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ fun <T> extractTypesafeDocumentation(serializer: KSerializer<T>, resourcesFormat
2424
if(!OpenApiPlugin.config.autoDocumentResourcesRoutes) {
2525
return {}
2626
}
27-
2827
// Note: typesafe routing only defines information about path & query parameters - no other information is available
2928
val path = resourcesFormat.encodeToPathPattern(serializer)
3029
return {

0 commit comments

Comments
 (0)