-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split into openapi and swagger modules
- Loading branch information
Showing
127 changed files
with
756 additions
and
558 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import com.vanniktech.maven.publish.JavadocJar | ||
import com.vanniktech.maven.publish.KotlinJvm | ||
import com.vanniktech.maven.publish.SonatypeHost | ||
import io.gitlab.arturbosch.detekt.Detekt | ||
|
||
val projectGroupId: String by project | ||
val projectVersion: String by project | ||
group = projectGroupId | ||
version = projectVersion | ||
|
||
plugins { | ||
kotlin("jvm") | ||
id("org.owasp.dependencycheck") | ||
id("io.gitlab.arturbosch.detekt") | ||
id("com.vanniktech.maven.publish") | ||
id("org.jetbrains.dokka") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
val versionKtor: String by project | ||
val versionSwaggerParser: String by project | ||
val versionSchemaKenerator: String by project | ||
val versionKotlinLogging: String by project | ||
val versionKotest: String by project | ||
val versionKotlinTest: String by project | ||
val versionMockk: String by project | ||
val versionJackson: String by project | ||
|
||
implementation("io.ktor:ktor-server-core-jvm:$versionKtor") | ||
implementation("io.ktor:ktor-server-auth:$versionKtor") | ||
implementation("io.ktor:ktor-server-resources:$versionKtor") | ||
|
||
implementation("io.swagger.parser.v3:swagger-parser:$versionSwaggerParser") | ||
|
||
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$versionJackson") | ||
|
||
implementation("io.github.smiley4:schema-kenerator-core:$versionSchemaKenerator") | ||
implementation("io.github.smiley4:schema-kenerator-reflection:$versionSchemaKenerator") | ||
implementation("io.github.smiley4:schema-kenerator-swagger:$versionSchemaKenerator") | ||
|
||
implementation("io.github.oshai:kotlin-logging-jvm:$versionKotlinLogging") | ||
|
||
testImplementation("io.ktor:ktor-server-netty-jvm:$versionKtor") | ||
testImplementation("io.ktor:ktor-server-content-negotiation:$versionKtor") | ||
testImplementation("io.ktor:ktor-serialization-jackson:$versionKtor") | ||
testImplementation("io.ktor:ktor-server-auth:$versionKtor") | ||
testImplementation("io.ktor:ktor-server-call-logging:$versionKtor") | ||
testImplementation("io.ktor:ktor-server-test-host:$versionKtor") | ||
testImplementation("io.kotest:kotest-runner-junit5:$versionKotest") | ||
testImplementation("io.kotest:kotest-assertions-core:$versionKotest") | ||
testImplementation("org.jetbrains.kotlin:kotlin-test:$versionKotlinTest") | ||
testImplementation("io.mockk:mockk:$versionMockk") | ||
|
||
} | ||
|
||
kotlin { | ||
jvmToolchain(11) | ||
} | ||
|
||
tasks.withType<Test>().configureEach { | ||
useJUnitPlatform() | ||
} | ||
|
||
detekt { | ||
ignoreFailures = false | ||
buildUponDefaultConfig = true | ||
allRules = false | ||
config.setFrom("$projectDir/../detekt/detekt.yml") | ||
} | ||
tasks.withType<Detekt>().configureEach { | ||
reports { | ||
html.required.set(true) | ||
md.required.set(true) | ||
xml.required.set(false) | ||
txt.required.set(false) | ||
sarif.required.set(false) | ||
} | ||
} | ||
|
||
mavenPublishing { | ||
val projectGroupId: String by project | ||
val projectVersion: String by project | ||
val projectArtifactIdBase: String by project | ||
val projectNameBase: String by project | ||
val projectDescriptionBase: String by project | ||
val projectScmUrl: String by project | ||
val projectScmConnection: String by project | ||
val projectLicenseName: String by project | ||
val projectLicenseUrl: String by project | ||
val projectDeveloperName: String by project | ||
val projectDeveloperUrl: String by project | ||
|
||
configure(KotlinJvm(JavadocJar.Dokka("dokkaHtml"), true)) | ||
publishToMavenCentral(SonatypeHost.S01) | ||
signAllPublications() | ||
coordinates(projectGroupId, projectArtifactIdBase, projectVersion) | ||
pom { | ||
name.set(projectNameBase) | ||
description.set(projectDescriptionBase) | ||
url.set(projectScmUrl) | ||
licenses { | ||
license { | ||
name.set(projectLicenseName) | ||
url.set(projectLicenseUrl) | ||
distribution.set(projectLicenseUrl) | ||
} | ||
} | ||
scm { | ||
url.set(projectScmUrl) | ||
connection.set(projectScmConnection) | ||
} | ||
developers { | ||
developer { | ||
id.set(projectDeveloperName) | ||
name.set(projectDeveloperName) | ||
url.set(projectDeveloperUrl) | ||
} | ||
} | ||
} | ||
} |
169 changes: 169 additions & 0 deletions
169
ktor-openapi/src/main/kotlin/io/github/smiley4/ktoropenapi/OpenApiPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package io.github.smiley4.ktoropenapi | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import io.github.smiley4.ktoropenapi.builder.example.ExampleContext | ||
import io.github.smiley4.ktoropenapi.builder.example.ExampleContextImpl | ||
import io.github.smiley4.ktoropenapi.builder.openapi.ComponentsBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.ContactBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.ContentBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.ExternalDocumentationBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.HeaderBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.InfoBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.LicenseBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.OAuthFlowsBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.OpenApiBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.OperationBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.OperationTagsBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.ParameterBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.PathBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.PathsBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.RequestBodyBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.ResponseBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.ResponsesBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.SecurityRequirementsBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.SecuritySchemesBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.ServerBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.TagBuilder | ||
import io.github.smiley4.ktoropenapi.builder.openapi.TagExternalDocumentationBuilder | ||
import io.github.smiley4.ktoropenapi.builder.route.RouteCollector | ||
import io.github.smiley4.ktoropenapi.builder.route.RouteDocumentationMerger | ||
import io.github.smiley4.ktoropenapi.builder.route.RouteMeta | ||
import io.github.smiley4.ktoropenapi.builder.schema.SchemaContextImpl | ||
import io.github.smiley4.ktoropenapi.builder.schema.SchemaContext | ||
import io.github.smiley4.ktoropenapi.data.OutputFormat | ||
import io.github.smiley4.ktoropenapi.data.PluginConfigData | ||
import io.github.smiley4.ktoropenapi.dsl.config.OpenApiPluginConfigDsl | ||
import io.github.smiley4.ktoropenapi.routing.ApiSpec | ||
import io.ktor.server.application.Application | ||
import io.ktor.server.application.ApplicationStarted | ||
import io.ktor.server.application.createApplicationPlugin | ||
import io.ktor.server.application.hooks.MonitoringEvent | ||
import io.ktor.server.application.plugin | ||
import io.ktor.server.routing.RoutingRoot | ||
import io.swagger.v3.core.util.Json31 | ||
import io.swagger.v3.core.util.Yaml31 | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
val OpenApi = createApplicationPlugin(name = "OpenApi", createConfiguration = ::OpenApiPluginConfigDsl) { | ||
|
||
val config = pluginConfig.build(PluginConfigData.DEFAULT) | ||
|
||
on(MonitoringEvent(ApplicationStarted)) { application -> | ||
|
||
try { | ||
val routes = routes(application, config) | ||
ApiSpec.setAll(buildOpenApiSpecs(config, routes)) | ||
} catch (e: Exception) { | ||
logger.error(e) { "Error during application startup in openapi-plugin" } | ||
} | ||
|
||
} | ||
} | ||
|
||
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()) | ||
computeIfAbsent(specName) { mutableListOf() }.add(route) | ||
} | ||
} | ||
return buildMap { | ||
routesBySpec.forEach { (specName, routes) -> | ||
val specConfig = config.specConfigs[specName] ?: config | ||
this[specName] = buildOpenApiSpec(specName, specConfig, routes) | ||
} | ||
} | ||
} | ||
|
||
private fun buildOpenApiSpec(specName: String, pluginConfig: PluginConfigData, routes: List<RouteMeta>): Pair<String, OutputFormat> { | ||
return try { | ||
val schemaContext = SchemaContextImpl(pluginConfig.schemaConfig).also { | ||
it.addGlobal(pluginConfig.schemaConfig) | ||
it.add(routes) | ||
} | ||
val exampleContext = ExampleContextImpl(pluginConfig.exampleConfig.exampleEncoder).also { | ||
it.addShared(pluginConfig.exampleConfig) | ||
it.add(routes) | ||
} | ||
val openApi = builder(pluginConfig, schemaContext, exampleContext).build(routes) | ||
pluginConfig.postBuild?.let { it(openApi, specName) } | ||
when (pluginConfig.outputFormat) { | ||
OutputFormat.JSON -> Json31.pretty(openApi) to pluginConfig.outputFormat | ||
OutputFormat.YAML -> Yaml31.pretty(openApi) to pluginConfig.outputFormat | ||
} | ||
} catch (e: Exception) { | ||
logger.error(e) { "Error during openapi-spec generation" } | ||
return pluginConfig.outputFormat.empty to pluginConfig.outputFormat | ||
} | ||
} | ||
|
||
private fun routes(application: Application, config: PluginConfigData): List<RouteMeta> { | ||
return RouteCollector(RouteDocumentationMerger()) | ||
.collectRoutes({ application.plugin(RoutingRoot) }, config) | ||
.map { it.copy(path = "${application.rootPath()}${it.path}") } | ||
.toList() | ||
} | ||
|
||
private fun Application.rootPath(): String = | ||
environment.config.propertyOrNull("ktor.deployment.rootPath")?.getString() ?: "" | ||
|
||
private fun builder( | ||
config: PluginConfigData, | ||
schemaContext: SchemaContext, | ||
exampleContext: ExampleContext, | ||
): OpenApiBuilder { | ||
return OpenApiBuilder( | ||
config = config, | ||
schemaContext = schemaContext, | ||
exampleContext = exampleContext, | ||
infoBuilder = InfoBuilder( | ||
contactBuilder = ContactBuilder(), | ||
licenseBuilder = LicenseBuilder() | ||
), | ||
externalDocumentationBuilder = ExternalDocumentationBuilder(), | ||
serverBuilder = ServerBuilder(), | ||
tagBuilder = TagBuilder( | ||
tagExternalDocumentationBuilder = TagExternalDocumentationBuilder() | ||
), | ||
pathsBuilder = PathsBuilder( | ||
pathBuilder = PathBuilder( | ||
operationBuilder = OperationBuilder( | ||
operationTagsBuilder = OperationTagsBuilder(config), | ||
parameterBuilder = ParameterBuilder( | ||
schemaContext = schemaContext, | ||
exampleContext = exampleContext, | ||
), | ||
requestBodyBuilder = RequestBodyBuilder( | ||
contentBuilder = ContentBuilder( | ||
schemaContext = schemaContext, | ||
exampleContext = exampleContext, | ||
headerBuilder = HeaderBuilder(schemaContext) | ||
) | ||
), | ||
responsesBuilder = ResponsesBuilder( | ||
responseBuilder = ResponseBuilder( | ||
headerBuilder = HeaderBuilder(schemaContext), | ||
contentBuilder = ContentBuilder( | ||
schemaContext = schemaContext, | ||
exampleContext = exampleContext, | ||
headerBuilder = HeaderBuilder(schemaContext) | ||
) | ||
), | ||
config = config | ||
), | ||
securityRequirementsBuilder = SecurityRequirementsBuilder(config), | ||
externalDocumentationBuilder = ExternalDocumentationBuilder(), | ||
serverBuilder = ServerBuilder() | ||
) | ||
) | ||
), | ||
componentsBuilder = ComponentsBuilder( | ||
config = config, | ||
securitySchemesBuilder = SecuritySchemesBuilder( | ||
oAuthFlowsBuilder = OAuthFlowsBuilder() | ||
) | ||
) | ||
) | ||
} |
4 changes: 2 additions & 2 deletions
4
...aggerui/builder/example/ExampleContext.kt → ...openapi/builder/example/ExampleContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...rui/builder/example/ExampleContextImpl.kt → ...api/builder/example/ExampleContextImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...erui/builder/openapi/ComponentsBuilder.kt → ...napi/builder/openapi/ComponentsBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...aggerui/builder/openapi/ContactBuilder.kt → ...openapi/builder/openapi/ContactBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
...aggerui/builder/openapi/ContentBuilder.kt → ...openapi/builder/openapi/ContentBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...r/openapi/ExternalDocumentationBuilder.kt → ...r/openapi/ExternalDocumentationBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...waggerui/builder/openapi/HeaderBuilder.kt → ...ropenapi/builder/openapi/HeaderBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...rswaggerui/builder/openapi/InfoBuilder.kt → ...toropenapi/builder/openapi/InfoBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...aggerui/builder/openapi/LicenseBuilder.kt → ...openapi/builder/openapi/LicenseBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...erui/builder/openapi/OAuthFlowsBuilder.kt → ...napi/builder/openapi/OAuthFlowsBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.