Skip to content
This repository was archived by the owner on Dec 15, 2024. It is now read-only.

Commit 27409da

Browse files
committed
Do not include identical response bodies; only use example key if size > 1
1 parent f7f2a00 commit 27409da

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

src/main/kotlin/io/kotest/extensions/ktor/openapi/OpenApiBuilder.kt

+10-5
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,16 @@ class OpenApiBuilder(private val config: OpenApiConfig) {
7777

7878
// for each content type that is the same, they are added as multiple examples
7979
// to the same MediaType in the response content
80-
tracesByContentType.withIndex().forEach { (index, value) ->
81-
mediaType.addExamples(
82-
"Example ${index + 1}",
83-
Example().value(value.responseBody)
84-
)
80+
val bodies = tracesByContentType.mapNotNull { it.responseBody }.distinct()
81+
if (bodies.size == 1) {
82+
mediaType.example = bodies.first()
83+
} else if (bodies.size > 1) {
84+
bodies.withIndex().forEach { (index, value) ->
85+
mediaType.addExamples(
86+
"Example ${index + 1}",
87+
Example().value(value)
88+
)
89+
}
8590
}
8691

8792
if (resp.content == null) resp.content = Content()

src/test/kotlin/io/kotest/extensions/ktor/openapi/OpenApiWriterTest.kt

+53
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.kotest.extensions.ktor.openapi
22

33
import io.kotest.core.spec.style.FunSpec
44
import io.kotest.matchers.string.shouldInclude
5+
import io.kotest.matchers.string.shouldNotInclude
56
import io.ktor.http.ContentType
67
import io.ktor.http.HttpMethod
78
import io.ktor.http.HttpStatusCode
@@ -114,4 +115,56 @@ class OpenApiWriterTest : FunSpec({
114115
file.readText() shouldInclude "beam me up scotty"
115116
file.readText() shouldInclude "text/css"
116117
}
118+
119+
test("builder should not set example key if only one response") {
120+
val file = Files.createTempFile("openapi", "test")
121+
val builder = OpenApiBuilder(OpenApiConfig(path = file))
122+
builder.addTraces(
123+
"parth path",
124+
listOf(
125+
Trace.default(HttpMethod.Get, "parth path")
126+
.copy(
127+
responseBody = "beam me up scotty",
128+
contentType = ContentType.Text.CSS,
129+
status = HttpStatusCode.MovedPermanently,
130+
)
131+
)
132+
)
133+
OpenApiWriter(file).write(builder)
134+
file.readText() shouldInclude "beam me up scotty"
135+
file.readText() shouldNotInclude "Example 1"
136+
}
137+
138+
test("builder should not include identical response bodies") {
139+
val file = Files.createTempFile("openapi", "test")
140+
val builder = OpenApiBuilder(OpenApiConfig(path = file))
141+
builder.addTraces(
142+
"parth path",
143+
listOf(
144+
Trace.default(HttpMethod.Get, "parth path")
145+
.copy(
146+
responseBody = "beam me up scotty",
147+
contentType = ContentType.Text.CSS,
148+
status = HttpStatusCode.MovedPermanently,
149+
),
150+
Trace.default(HttpMethod.Get, "parth path")
151+
.copy(
152+
responseBody = "beam me up jimmy",
153+
contentType = ContentType.Text.CSS,
154+
status = HttpStatusCode.MovedPermanently,
155+
),
156+
Trace.default(HttpMethod.Get, "parth path")
157+
.copy(
158+
responseBody = "beam me up scotty",
159+
contentType = ContentType.Text.CSS,
160+
status = HttpStatusCode.MovedPermanently,
161+
)
162+
)
163+
)
164+
OpenApiWriter(file).write(builder)
165+
file.readText() shouldInclude "beam me up scotty"
166+
file.readText() shouldInclude "Example 1"
167+
file.readText() shouldInclude "Example 2"
168+
file.readText() shouldNotInclude "Example 3"
169+
}
117170
})

0 commit comments

Comments
 (0)