Skip to content

Commit

Permalink
major: ktor 3.0.0 support - fixes #644 (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
knmueller authored Oct 30, 2024
1 parent fa7b1e5 commit 21f922a
Show file tree
Hide file tree
Showing 44 changed files with 151 additions and 777 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Updates for Ktor 3.x support
- Remove deprecated `NotarizedLocations`

### Added

### Changed
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
kotlin("jvm") version "1.9.25" apply false
kotlin("plugin.serialization") version "1.9.25" apply false
kotlin("jvm") version "2.0.21" apply false
kotlin("plugin.serialization") version "2.0.21" apply false
id("io.bkbn.sourdough.library.jvm") version "0.12.2" apply false
id("io.bkbn.sourdough.application.jvm") version "0.12.2" apply false
id("io.bkbn.sourdough.root") version "0.12.2"
Expand Down
5 changes: 3 additions & 2 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ sourdoughLibrary {
dependencies {
// VERSIONS
val kotestVersion: String by project
val kotlinSerializeVersion: String by project
val ktorVersion: String by project
val detektVersion: String by project

Expand All @@ -43,7 +44,7 @@ dependencies {
testFixturesApi("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
testFixturesApi("io.kotest:kotest-property-jvm:$kotestVersion")
testFixturesApi("io.kotest:kotest-assertions-json-jvm:$kotestVersion")
testFixturesApi("io.kotest:kotest-assertions-ktor-jvm:4.4.3")
testFixturesApi("io.kotest.extensions:kotest-assertions-ktor:2.0.0")

testFixturesApi("io.ktor:ktor-server-core:$ktorVersion")
testFixturesApi("io.ktor:ktor-server-test-host:$ktorVersion")
Expand All @@ -57,7 +58,7 @@ dependencies {

testFixturesApi("dev.forst:ktor-api-key:2.2.4")

testFixturesApi("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
testFixturesApi("org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinSerializeVersion")
}

testing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import io.bkbn.kompendium.json.schema.SchemaConfigurator
import io.bkbn.kompendium.json.schema.definition.JsonSchema
import io.bkbn.kompendium.json.schema.util.Helpers.getSimpleSlug
import io.bkbn.kompendium.oas.OpenApiSpec
import io.ktor.server.application.call
import io.ktor.server.application.createApplicationPlugin
import io.ktor.server.response.respond
import io.ktor.server.routing.Routing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ object NotarizedRoute {

fun Route.calculateRoutePath() = toString()
.let {
application.environment.rootPath.takeIf { root -> root.isNotEmpty() }
application.rootPath.takeIf { root -> root.isNotEmpty() }
?.let { root ->
val sanitizedRoute = if (root.startsWith("/")) root else "/$root"
it.replace(sanitizedRoute, "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import io.ktor.http.HttpMethod
import io.ktor.server.application.install
import io.ktor.server.auth.Authentication
import io.ktor.server.auth.OAuthServerSettings
import io.ktor.server.auth.Principal
import io.ktor.server.auth.UserIdPrincipal
import io.ktor.server.auth.basic
import io.ktor.server.auth.jwt.jwt
Expand Down Expand Up @@ -92,14 +93,18 @@ class KompendiumAuthenticationTest : DescribeSpec({
) { customAuthConfig() }
}
it("Can provide multiple authentication strategies") {
data class TestAppPrincipal(val key: String) : Principal
TestHelpers.openApiTestAllSerializers(
snapshotName = "T0047__multiple_auth_strategies.json",
applicationSetup = {
install(Authentication) {
apiKey("api-key") {
headerName = "X-API-KEY"
validate {
UserIdPrincipal("Placeholder")
validate { key ->
// api key library (dev.forst.ktor.apikey) is using the deprecated `Principal` class
key
.takeIf { it == "api-key" }
?.let { TestAppPrincipal(it) }
}
}
jwt("jwt") {
Expand Down
16 changes: 8 additions & 8 deletions core/src/test/kotlin/io/bkbn/kompendium/core/KompendiumTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import io.bkbn.kompendium.oas.security.BasicAuth
import io.bkbn.kompendium.oas.serialization.KompendiumSerializersModule
import io.kotest.core.spec.style.DescribeSpec
import io.ktor.serialization.kotlinx.json.json
import io.ktor.server.application.call
import io.ktor.server.application.install
import io.ktor.server.auth.Authentication
import io.ktor.server.auth.UserIdPrincipal
Expand Down Expand Up @@ -182,13 +181,16 @@ class KompendiumTest : DescribeSpec({
it("Can generate paths without application root-path") {
openApiTestAllSerializers(
"T0054__app_with_rootpath.json",
applicationEnvironmentBuilder = {
applicationSetup = {
rootPath = "/example"
},
specOverrides = {
copy(
servers = servers.map { it.copy(url = URI("${it.url}/example")) }.toMutableList()
)
},
serverConfigSetup = {
rootPath = "/example"
}
) { notarizedGet() }
}
Expand All @@ -202,12 +204,10 @@ class KompendiumTest : DescribeSpec({
snapshotName = "T0072__custom_serialization_strategy.json",
notarizedApplicationConfigOverrides = {
specRoute = { spec, routing ->
routing {
route("/openapi.json") {
get {
call.response.headers.append("Content-Type", "application/json")
call.respondText { customJsonEncoder.encodeToString(spec) }
}
routing.route("/openapi.json") {
get {
call.response.headers.append("Content-Type", "application/json")
call.respondText { customJsonEncoder.encodeToString(spec) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@ import io.bkbn.kompendium.core.util.TestModules.defaultPathSummary
import io.bkbn.kompendium.core.util.TestModules.defaultResponseDescription
import io.bkbn.kompendium.core.util.TestModules.rootPath
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.install
import io.ktor.server.auth.authenticate
import io.ktor.server.routing.Routing
import io.ktor.server.routing.post
import io.ktor.server.routing.Route
import io.ktor.server.routing.route

fun Routing.defaultAuthConfig() {
fun Route.defaultAuthConfig() {
authenticate("basic") {
route(rootPath) {
basicGetGenerator<TestResponse>()
}
}
}

fun Routing.customAuthConfig() {
fun Route.customAuthConfig() {
authenticate("auth-oauth-google") {
route(rootPath) {
install(NotarizedRoute()) {
Expand All @@ -44,7 +42,7 @@ fun Routing.customAuthConfig() {
}
}

fun Routing.customScopesOnSiblingPathOperations() {
fun Route.customScopesOnSiblingPathOperations() {
authenticate("auth-oauth-google") {
route(rootPath) {
install(NotarizedRoute()) {
Expand Down Expand Up @@ -81,7 +79,7 @@ fun Routing.customScopesOnSiblingPathOperations() {
}
}

fun Routing.multipleAuthStrategies() {
fun Route.multipleAuthStrategies() {
authenticate("jwt", "api-key") {
route(rootPath) {
basicGetGenerator<TestResponse>()
Expand Down
15 changes: 7 additions & 8 deletions core/src/test/kotlin/io/bkbn/kompendium/core/util/Constraints.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import io.bkbn.kompendium.enrichment.NumberEnrichment
import io.bkbn.kompendium.enrichment.ObjectEnrichment
import io.bkbn.kompendium.enrichment.StringEnrichment
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.install
import io.ktor.server.routing.Routing
import io.ktor.server.routing.Route
import io.ktor.server.routing.route

fun Routing.intConstraints() {
fun Route.intConstraints() {
route(defaultPath) {
install(NotarizedRoute()) {
get = GetInfo.builder {
Expand All @@ -43,7 +42,7 @@ fun Routing.intConstraints() {
}
}

fun Routing.doubleConstraints() {
fun Route.doubleConstraints() {
route(defaultPath) {
install(NotarizedRoute()) {
get = GetInfo.builder {
Expand All @@ -70,7 +69,7 @@ fun Routing.doubleConstraints() {
}
}

fun Routing.stringConstraints() {
fun Route.stringConstraints() {
route(defaultPath) {
install(NotarizedRoute()) {
get = GetInfo.builder {
Expand All @@ -96,7 +95,7 @@ fun Routing.stringConstraints() {
}
}

fun Routing.stringPatternConstraints() {
fun Route.stringPatternConstraints() {
route(defaultPath) {
install(NotarizedRoute()) {
get = GetInfo.builder {
Expand All @@ -121,7 +120,7 @@ fun Routing.stringPatternConstraints() {
}
}

fun Routing.stringContentEncodingConstraints() {
fun Route.stringContentEncodingConstraints() {
route(defaultPath) {
install(NotarizedRoute()) {
get = GetInfo.builder {
Expand All @@ -147,7 +146,7 @@ fun Routing.stringContentEncodingConstraints() {
}
}

fun Routing.arrayConstraints() {
fun Route.arrayConstraints() {
route(defaultPath) {
install(NotarizedRoute()) {
get = GetInfo.builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package io.bkbn.kompendium.core.util
import io.bkbn.kompendium.core.fixtures.SerialNameObject
import io.bkbn.kompendium.core.fixtures.TransientObject
import io.bkbn.kompendium.core.fixtures.UnbackedObject
import io.ktor.server.routing.Routing
import io.ktor.server.routing.Route

fun Routing.ignoredFieldsResponse() = basicGetGenerator<TransientObject>()
fun Routing.unbackedFieldsResponse() = basicGetGenerator<UnbackedObject>()
fun Routing.customFieldNameResponse() = basicGetGenerator<SerialNameObject>()
fun Route.ignoredFieldsResponse() = basicGetGenerator<TransientObject>()
fun Route.unbackedFieldsResponse() = basicGetGenerator<UnbackedObject>()
fun Route.customFieldNameResponse() = basicGetGenerator<SerialNameObject>()
4 changes: 2 additions & 2 deletions core/src/test/kotlin/io/bkbn/kompendium/core/util/Defaults.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package io.bkbn.kompendium.core.util
import io.bkbn.kompendium.core.fixtures.TestResponse
import io.bkbn.kompendium.json.schema.definition.TypeDefinition
import io.bkbn.kompendium.oas.payload.Parameter
import io.ktor.server.routing.Routing
import io.ktor.server.routing.Route

fun Routing.defaultParameter() = basicGetGenerator<TestResponse>(
fun Route.defaultParameter() = basicGetGenerator<TestResponse>(
params = listOf(
Parameter(
name = "id",
Expand Down
17 changes: 8 additions & 9 deletions core/src/test/kotlin/io/bkbn/kompendium/core/util/Enrichment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import io.bkbn.kompendium.enrichment.NumberEnrichment
import io.bkbn.kompendium.enrichment.ObjectEnrichment
import io.bkbn.kompendium.enrichment.StringEnrichment
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.install
import io.ktor.server.routing.Routing
import io.ktor.server.routing.Route
import io.ktor.server.routing.route

fun Routing.enrichedSimpleResponse() {
fun Route.enrichedSimpleResponse() {
route("/enriched") {
install(NotarizedRoute()) {
get = GetInfo.builder {
Expand All @@ -44,7 +43,7 @@ fun Routing.enrichedSimpleResponse() {
}
}

fun Routing.enrichedSimpleRequest() {
fun Route.enrichedSimpleRequest() {
route("/example") {
install(NotarizedRoute()) {
parameters = TestModules.defaultParams
Expand Down Expand Up @@ -78,7 +77,7 @@ fun Routing.enrichedSimpleRequest() {
}
}

fun Routing.enrichedNestedCollection() {
fun Route.enrichedNestedCollection() {
route("/example") {
install(NotarizedRoute()) {
parameters = TestModules.defaultParams
Expand Down Expand Up @@ -114,7 +113,7 @@ fun Routing.enrichedNestedCollection() {
}
}

fun Routing.enrichedTopLevelCollection() {
fun Route.enrichedTopLevelCollection() {
route("/example") {
install(NotarizedRoute()) {
parameters = TestModules.defaultParams
Expand Down Expand Up @@ -150,7 +149,7 @@ fun Routing.enrichedTopLevelCollection() {
}
}

fun Routing.enrichedComplexGenericType() {
fun Route.enrichedComplexGenericType() {
route("/example") {
install(NotarizedRoute()) {
parameters = TestModules.defaultParams
Expand Down Expand Up @@ -193,7 +192,7 @@ fun Routing.enrichedComplexGenericType() {
}
}

fun Routing.enrichedGenericResponse() {
fun Route.enrichedGenericResponse() {
route("/example") {
install(NotarizedRoute()) {
get = GetInfo.builder {
Expand Down Expand Up @@ -228,7 +227,7 @@ fun Routing.enrichedGenericResponse() {
}
}

fun Routing.enrichedMap() {
fun Route.enrichedMap() {
route("/example") {
install(NotarizedRoute()) {
get = GetInfo.builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package io.bkbn.kompendium.core.util
import io.bkbn.kompendium.core.fixtures.TestResponse
import io.bkbn.kompendium.core.util.TestModules.defaultPath
import io.ktor.server.auth.authenticate
import io.ktor.server.routing.Routing
import io.ktor.server.routing.Route
import io.ktor.server.routing.route

fun Routing.samePathSameMethod() {
fun Route.samePathSameMethod() {
route(defaultPath) {
basicGetGenerator<TestResponse>()
authenticate("basic") {
Expand Down
11 changes: 5 additions & 6 deletions core/src/test/kotlin/io/bkbn/kompendium/core/util/Examples.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import io.bkbn.kompendium.json.schema.definition.TypeDefinition
import io.bkbn.kompendium.oas.payload.MediaType
import io.bkbn.kompendium.oas.payload.Parameter
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.install
import io.ktor.server.routing.Routing
import io.ktor.server.routing.Route
import io.ktor.server.routing.route

fun Routing.reqRespExamples() {
fun Route.reqRespExamples() {
route(rootPath) {
install(NotarizedRoute()) {
post = PostInfo.builder {
Expand All @@ -44,7 +43,7 @@ fun Routing.reqRespExamples() {
}
}

fun Routing.exampleParams() = basicGetGenerator<TestResponse>(
fun Route.exampleParams() = basicGetGenerator<TestResponse>(
params = listOf(
Parameter(
name = "id",
Expand All @@ -57,7 +56,7 @@ fun Routing.exampleParams() = basicGetGenerator<TestResponse>(
)
)

fun Routing.optionalReqExample() {
fun Route.optionalReqExample() {
route(rootPath) {
install(NotarizedRoute()) {
post = PostInfo.builder {
Expand All @@ -84,7 +83,7 @@ fun Routing.optionalReqExample() {
}
}

fun Routing.exampleSummaryAndDescription() {
fun Route.exampleSummaryAndDescription() {
route(rootPath) {
install(NotarizedRoute()) {
post = PostInfo.builder {
Expand Down
Loading

0 comments on commit 21f922a

Please sign in to comment.