Skip to content

Commit 2bacaaa

Browse files
committed
Merge branch 'bugfix/array-element-type-name' into release
2 parents 199baa6 + 22f2246 commit 2bacaaa

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plugins {
1010
object Meta {
1111
const val groupId = "io.github.smiley4"
1212
const val artifactId = "ktor-swagger-ui"
13-
const val version = "2.2.0"
13+
const val version = "2.2.1"
1414
const val name = "Ktor Swagger-UI"
1515
const val description = "Ktor plugin to document routes and provide Swagger UI"
1616
const val licenseName = "The Apache License, Version 2.0"
@@ -27,6 +27,7 @@ version = Meta.version
2727

2828
repositories {
2929
mavenCentral()
30+
maven(url = "https://raw.githubusercontent.com/glureau/json-schema-serialization/mvn-repo")
3031
}
3132

3233
dependencies {

src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/SchemaType.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@ fun SchemaType.getTypeName() = this.toString()
1616

1717
fun SchemaType.getSimpleTypeName(): String {
1818
val rawName = getTypeName()
19-
if(rawName.contains("<") || rawName.contains(">")) {
20-
return rawName
19+
return if (rawName.contains("<") || rawName.contains(">")) {
20+
rawName
2121
} else {
22-
return (this.classifier as KClass<*>).simpleName ?: rawName
22+
(this.classifier as KClass<*>).simpleName ?: rawName
23+
}
24+
}
25+
26+
fun SchemaType.getSimpleArrayElementTypeName(): String {
27+
if (this.arguments.size != 1 || this.arguments.first().type == null) {
28+
throw IllegalArgumentException("Could not determine type of array-elements")
29+
} else {
30+
return this.arguments[0].type!!.getSimpleTypeName()
2331
}
2432
}
2533

src/main/kotlin/io/github/smiley4/ktorswaggerui/spec/schema/SchemaContext.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.github.smiley4.ktorswaggerui.spec.schema
33
import io.github.smiley4.ktorswaggerui.dsl.CustomSchemaRef
44
import io.github.smiley4.ktorswaggerui.dsl.SchemaType
55
import io.github.smiley4.ktorswaggerui.dsl.getSchemaType
6+
import io.github.smiley4.ktorswaggerui.dsl.getSimpleArrayElementTypeName
67
import io.github.smiley4.ktorswaggerui.dsl.getSimpleTypeName
78
import io.swagger.v3.oas.models.media.Schema
89
import kotlin.collections.set
@@ -176,7 +177,7 @@ class SchemaContext {
176177
schemaDefinitions.definitions.forEach { (name, schema) ->
177178
addToComponentsSection(name, schema)
178179
}
179-
val rootName = schemaName(key)
180+
val rootName = getWrappedSchemaName(key)
180181
addToComponentsSection(rootName, schemaDefinitions.root.items)
181182
addInline(key, Schema<Any>().also { array ->
182183
array.type = "array"
@@ -194,6 +195,14 @@ class SchemaContext {
194195
}
195196
}
196197

198+
private fun getWrappedSchemaName(key: SchemaKeyWrapper): String {
199+
return if (key.isCustom) {
200+
key.schemaId
201+
} else {
202+
key.type.getSimpleArrayElementTypeName()
203+
}
204+
}
205+
197206
private fun addToComponentsSection(name: String, schema: Schema<*>) {
198207
componentsSection[name] = schema
199208
}

src/test/kotlin/io/github/smiley4/ktorswaggerui/tests/schema/SchemaContextTest.kt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package io.github.smiley4.ktorswaggerui.tests.schema
22

3+
import com.github.victools.jsonschema.generator.Option
4+
import com.github.victools.jsonschema.generator.OptionPreset
5+
import com.github.victools.jsonschema.generator.SchemaGenerator
6+
import com.github.victools.jsonschema.generator.SchemaGeneratorConfig
7+
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder
8+
import com.github.victools.jsonschema.generator.SchemaVersion
39
import io.github.smiley4.ktorswaggerui.SwaggerUIPluginConfig
410
import io.github.smiley4.ktorswaggerui.dsl.OpenApiRoute
511
import io.github.smiley4.ktorswaggerui.dsl.array
@@ -18,6 +24,7 @@ import io.kotest.matchers.shouldBe
1824
import io.kotest.matchers.shouldNotBe
1925
import io.ktor.http.HttpMethod
2026
import io.swagger.v3.oas.models.media.Schema
27+
import kotlin.reflect.jvm.javaType
2128

2229
class SchemaContextTest : StringSpec({
2330

@@ -360,6 +367,51 @@ class SchemaContextTest : StringSpec({
360367
}
361368
}
362369

370+
"unwrap inlined array schema" {
371+
val generator = SchemaGenerator(
372+
SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON)
373+
.without(Option.DEFINITIONS_FOR_ALL_OBJECTS)
374+
.with(Option.INLINE_ALL_SCHEMAS)
375+
.with(Option.EXTRA_OPEN_API_FORMAT_VALUES)
376+
.with(Option.ALLOF_CLEANUP_AT_THE_END)
377+
.build()
378+
)
379+
val config = SwaggerUIPluginConfig().also {
380+
it.encoding {
381+
schemaEncoder { type ->
382+
generator.generateSchema(type.javaType).toString()
383+
}
384+
}
385+
}
386+
val routes = listOf(
387+
route {
388+
request {
389+
body<List<SimpleDataClass>>()
390+
}
391+
}
392+
)
393+
val schemaContext = schemaContext(routes, config)
394+
schemaContext.getSchema(getSchemaType<List<SimpleDataClass>>()).also { schema ->
395+
schema.type shouldBe "array"
396+
schema.`$ref` shouldBe null
397+
schema.items
398+
.also { it shouldNotBe null }
399+
?.also { items ->
400+
items.`$ref` shouldBe "#/components/schemas/SimpleDataClass"
401+
}
402+
}
403+
schemaContext.getComponentsSection().also { components ->
404+
components.keys shouldContainExactlyInAnyOrder listOf(
405+
"SimpleDataClass",
406+
)
407+
components["SimpleDataClass"]?.also { schema ->
408+
schema.type shouldBe "object"
409+
schema.properties.keys shouldContainExactlyInAnyOrder listOf("number", "text")
410+
}
411+
}
412+
}
413+
414+
363415
}) {
364416

365417
companion object {

0 commit comments

Comments
 (0)