diff --git a/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/MultipleSpecs.kt b/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/MultipleSpecs.kt index 00d0bc9d..baf84577 100644 --- a/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/MultipleSpecs.kt +++ b/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/MultipleSpecs.kt @@ -6,7 +6,6 @@ import io.github.smiley4.ktorswaggerui.dsl.routing.route import io.github.smiley4.ktorswaggerui.routing.openApiSpec import io.github.smiley4.ktorswaggerui.routing.swaggerUI import io.ktor.server.application.Application -import io.ktor.server.application.call import io.ktor.server.application.install import io.ktor.server.engine.embeddedServer import io.ktor.server.netty.Netty @@ -39,7 +38,7 @@ private fun Application.myModule() { } } // assign all unassigned routes to spec "v2" (here only route '/greet') - specAssigner = {_, _ -> "version2"} + specAssigner = { _, _ -> "version2" } } routing { @@ -68,7 +67,6 @@ private fun Application.myModule() { } } - // version 1.0 routes route("v1", { specId = "version1" @@ -77,6 +75,9 @@ private fun Application.myModule() { // "hello"-route in version 1.0 get("hello", { description = "Version 1 'Hello World'" + request { + queryParameter>("name") + } }) { call.respondText("Hello World!") } @@ -91,6 +92,9 @@ private fun Application.myModule() { // "hello"-route in version 2.0 get("hello", { description = "Version 2 'Hello World'" + request { + queryParameter>("name") + } }) { call.respondText("Hello World! (improved)") } @@ -100,6 +104,9 @@ private fun Application.myModule() { // unassigned route get("greet", { description = "Alternative route not manually assigned to any spec." + request { + queryParameter>("name") + } }) { call.respondText("Alternative Hello World!") } diff --git a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/SwaggerPlugin.kt b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/SwaggerPlugin.kt index b4fe4769..34db4d00 100644 --- a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/SwaggerPlugin.kt +++ b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/SwaggerPlugin.kt @@ -70,8 +70,7 @@ val SwaggerUI = createApplicationPlugin(name = "SwaggerUI", createConfiguration private fun buildOpenApiSpecs(config: PluginConfigData, routes: List): Map> { val routesBySpec = buildMap> { routes.forEach { route -> - val specName = - route.documentation.specId ?: config.specAssigner(route.path, route.documentation.tags.toList()) + val specName = route.documentation.specId ?: config.specAssigner(route.path, route.documentation.tags.toList()) computeIfAbsent(specName) { mutableListOf() }.add(route) } } diff --git a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/data/SchemaConfigData.kt b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/data/SchemaConfigData.kt index 29c254e6..2aff20e2 100644 --- a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/data/SchemaConfigData.kt +++ b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/data/SchemaConfigData.kt @@ -1,6 +1,5 @@ package io.github.smiley4.ktorswaggerui.data -import io.github.smiley4.schemakenerator.core.addDiscriminatorProperty import io.github.smiley4.schemakenerator.core.connectSubTypes import io.github.smiley4.schemakenerator.core.handleNameAnnotation import io.github.smiley4.schemakenerator.reflection.collectSubTypes diff --git a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/config/OpenApiTags.kt b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/config/OpenApiTags.kt index 1d93ace6..3752810a 100644 --- a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/config/OpenApiTags.kt +++ b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/config/OpenApiTags.kt @@ -1,6 +1,7 @@ package io.github.smiley4.ktorswaggerui.dsl.config import io.github.smiley4.ktorswaggerui.data.DataUtils.merge +import io.github.smiley4.ktorswaggerui.data.DataUtils.mergeDefault import io.github.smiley4.ktorswaggerui.data.PluginConfigData import io.github.smiley4.ktorswaggerui.data.TagData import io.github.smiley4.ktorswaggerui.data.TagGenerator @@ -39,7 +40,7 @@ class OpenApiTags { addAll(base.tags) addAll(tags.map { it.build(TagData.DEFAULT) }) }, - generator = merge(base.generator, tagGenerator) ?: TagsData.DEFAULT.generator, + generator = mergeDefault(base.generator, tagGenerator, TagsData.DEFAULT.generator) ) } diff --git a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/config/PluginConfigDsl.kt b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/config/PluginConfigDsl.kt index 81a9bd64..cc360514 100644 --- a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/config/PluginConfigDsl.kt +++ b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/config/PluginConfigDsl.kt @@ -1,6 +1,7 @@ package io.github.smiley4.ktorswaggerui.dsl.config import io.github.smiley4.ktorswaggerui.data.DataUtils.merge +import io.github.smiley4.ktorswaggerui.data.DataUtils.mergeDefault import io.github.smiley4.ktorswaggerui.data.OutputFormat import io.github.smiley4.ktorswaggerui.data.PathFilter import io.github.smiley4.ktorswaggerui.data.PluginConfigData @@ -166,7 +167,7 @@ class PluginConfigDsl { swagger = swaggerUI.build(base.swagger), securityConfig = securityConfig, tagsConfig = tags.build(base.tagsConfig), - schemaConfig = schemaConfig.build(securityConfig), + schemaConfig = schemaConfig.build(base.schemaConfig, securityConfig), exampleConfig = exampleConfig.build(securityConfig), specAssigner = merge(base.specAssigner, specAssigner) ?: PluginConfigData.DEFAULT.specAssigner, pathFilter = merge(base.pathFilter, pathFilter) ?: PluginConfigData.DEFAULT.pathFilter, @@ -180,7 +181,7 @@ class PluginConfigDsl { }, specConfigs = mutableMapOf(), postBuild = merge(base.postBuild, postBuild), - outputFormat = outputFormat + outputFormat = mergeDefault(base.outputFormat, outputFormat, PluginConfigData.DEFAULT.outputFormat) ).also { specConfigs.forEach { (specId, config) -> it.specConfigs[specId] = config.build(it) diff --git a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/config/SchemaConfig.kt b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/config/SchemaConfig.kt index 1c80d9b4..591448c3 100644 --- a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/config/SchemaConfig.kt +++ b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/config/SchemaConfig.kt @@ -1,6 +1,7 @@ package io.github.smiley4.ktorswaggerui.dsl.config import io.github.smiley4.ktorswaggerui.data.* +import io.github.smiley4.ktorswaggerui.data.DataUtils.mergeDefault import io.github.smiley4.ktorswaggerui.dsl.OpenApiDslMarker import io.github.smiley4.schemakenerator.swagger.data.CompiledSwaggerSchema import io.swagger.v3.oas.models.media.Schema @@ -87,10 +88,16 @@ class SchemaConfig { * Build the data object for this config. * @param securityConfig configuration that might contain additional schemas */ - fun build(securityConfig: SecurityData) = SchemaConfigData( - generator = generator, - schemas = schemas, - overwrite = overwrite, + fun build(base: SchemaConfigData, securityConfig: SecurityData) = SchemaConfigData( + generator = mergeDefault(base.generator, generator, SchemaConfigData.DEFAULT.generator), + schemas = mutableMapOf().apply { + this.putAll(base.schemas) + this.putAll(schemas) + }, + overwrite = mutableMapOf().apply { + this.putAll(base.overwrite) + this.putAll(overwrite) + }, securitySchemas = securityConfig.defaultUnauthorizedResponse?.body?.let { body -> when (body) { is OpenApiSimpleBodyData -> listOf(body.type)