Skip to content

Commit

Permalink
Merge branch 'fix-plugin-config-merging' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
SMILEY4 committed Jan 15, 2025
2 parents e193ca6 + 6cc355b commit d7299f7
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -39,7 +38,7 @@ private fun Application.myModule() {
}
}
// assign all unassigned routes to spec "v2" (here only route '/greet')
specAssigner = {_, _ -> "version2"}
specAssigner = { _, _ -> "version2" }
}

routing {
Expand Down Expand Up @@ -68,7 +67,6 @@ private fun Application.myModule() {
}
}


// version 1.0 routes
route("v1", {
specId = "version1"
Expand All @@ -77,6 +75,9 @@ private fun Application.myModule() {
// "hello"-route in version 1.0
get("hello", {
description = "Version 1 'Hello World'"
request {
queryParameter<List<String>>("name")
}
}) {
call.respondText("Hello World!")
}
Expand All @@ -91,6 +92,9 @@ private fun Application.myModule() {
// "hello"-route in version 2.0
get("hello", {
description = "Version 2 'Hello World'"
request {
queryParameter<List<String>>("name")
}
}) {
call.respondText("Hello World! (improved)")
}
Expand All @@ -100,6 +104,9 @@ private fun Application.myModule() {
// unassigned route
get("greet", {
description = "Alternative route not manually assigned to any spec."
request {
queryParameter<List<String>>("name")
}
}) {
call.respondText("Alternative Hello World!")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ val SwaggerUI = createApplicationPlugin(name = "SwaggerUI", createConfiguration
private fun buildOpenApiSpecs(config: PluginConfigData, routes: List<RouteMeta>): Map<String, Pair<String, OutputFormat>> {
val routesBySpec = buildMap<String, MutableList<RouteMeta>> {
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)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
)

}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<String, TypeDescriptor>().apply {
this.putAll(base.schemas)
this.putAll(schemas)
},
overwrite = mutableMapOf<KType, TypeDescriptor>().apply {
this.putAll(base.overwrite)
this.putAll(overwrite)
},
securitySchemas = securityConfig.defaultUnauthorizedResponse?.body?.let { body ->
when (body) {
is OpenApiSimpleBodyData -> listOf(body.type)
Expand Down

0 comments on commit d7299f7

Please sign in to comment.