diff --git a/build.gradle.kts b/build.gradle.kts index a2aabc8..640c4e8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -121,8 +121,10 @@ subprojects { compileOnly("io.javalin:javalin:$javalin") testImplementation("io.javalin:javalin:$javalin") testImplementation("io.javalin:javalin-testtools:$javalin") - kaptTest("io.javalin.community.openapi:openapi-annotation-processor:$javalin") - testImplementation("io.javalin.community.openapi:javalin-openapi-plugin:$javalin") + + val openapi = "5.6.3" + kaptTest("io.javalin.community.openapi:openapi-annotation-processor:$openapi") + testImplementation("io.javalin.community.openapi:javalin-openapi-plugin:$openapi") val jackson = "2.15.3" testImplementation("com.fasterxml.jackson.core:jackson-databind:$jackson") diff --git a/routing-core/src/main/kotlin/io/javalin/community/routing/RouteComparator.kt b/routing-core/src/main/kotlin/io/javalin/community/routing/RouteComparator.kt index 4f58631..1920f96 100644 --- a/routing-core/src/main/kotlin/io/javalin/community/routing/RouteComparator.kt +++ b/routing-core/src/main/kotlin/io/javalin/community/routing/RouteComparator.kt @@ -4,7 +4,7 @@ import java.text.Collator import java.text.RuleBasedCollator import java.util.Locale -fun List.sortRoutes(): List = +fun Collection.sortRoutes(): List = sortedWith(RouteComparator()) open class RouteComparator : Comparator { diff --git a/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/DslRouting.kt b/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/DslRouting.kt new file mode 100644 index 0000000..f1cc789 --- /dev/null +++ b/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/DslRouting.kt @@ -0,0 +1,40 @@ +package io.javalin.community.routing.dsl + +import io.javalin.community.routing.dsl.defaults.DefaultDsl +import io.javalin.community.routing.dsl.defaults.DefaultDsl.DefaultConfiguration +import io.javalin.community.routing.dsl.defaults.DefaultDsl.DefaultScope +import io.javalin.community.routing.dsl.defaults.DefaultRoute +import io.javalin.community.routing.sortRoutes +import io.javalin.config.JavalinConfig +import io.javalin.http.HandlerType +import io.javalin.router.InternalRouter +import io.javalin.router.RoutingApiInitializer +import java.util.function.Consumer + +open class DslRouting< + CONFIG : RoutingDslConfiguration, + ROUTE : DslRoute, + CONTEXT, + RESPONSE : Any +>( + private val factory: RoutingDslFactory +) : RoutingApiInitializer { + + companion object { + object Dsl : DslRouting(DefaultDsl) + } + + override fun initialize(cfg: JavalinConfig, internalRouter: InternalRouter, setup: Consumer) { + val dslConfig = factory.createConfiguration() + setup.accept(dslConfig) + + dslConfig.routes + .sortRoutes() + .map { route -> route to factory.createHandler(route) } + .forEach { (route, handler) -> internalRouter.addHttpHandler(HandlerType.valueOf(route.method.toString()), route.path, handler) } + + dslConfig.exceptionHandlers.forEach { (exceptionClass, handler) -> + internalRouter.addHttpExceptionHandler(exceptionClass.java, factory.createExceptionHandler(handler)) + } + } +} \ No newline at end of file diff --git a/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/DslRoutingPlugin.kt b/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/DslRoutingPlugin.kt deleted file mode 100644 index f9bf69c..0000000 --- a/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/DslRoutingPlugin.kt +++ /dev/null @@ -1,89 +0,0 @@ -package io.javalin.community.routing.dsl - -import io.javalin.Javalin -import io.javalin.community.routing.dsl.defaults.DefaultDsl -import io.javalin.community.routing.dsl.defaults.DefaultDsl.DefaultConfiguration -import io.javalin.community.routing.dsl.defaults.DefaultRoutes -import io.javalin.community.routing.registerRoute -import io.javalin.community.routing.sortRoutes -import io.javalin.config.JavalinConfig -import io.javalin.plugin.Plugin -import kotlin.reflect.KClass - -class DslRoutingPlugin< - CONFIG : RoutingDslConfiguration, - ROUTE : DslRoute, - CONTEXT, - RESPONSE : Any ->( - private val routingDsl: RoutingDslFactory -) : Plugin { - - private val registeredRoutes = mutableListOf() - private val registeredExceptionHandlers = mutableMapOf, DslExceptionHandler>() - - override fun apply(app: Javalin) { - registeredRoutes - .sortRoutes() - .map { route -> route to routingDsl.createHandler(route) } - .forEach { (route, handler) -> app.registerRoute(route.method, route.path, handler) } - - registeredExceptionHandlers.forEach { (exceptionClass, handler) -> - app.exception(exceptionClass.java, routingDsl.createExceptionHandler(handler)) - } - } - - fun routing(init: CONFIG.() -> Unit): DslRoutingPlugin = also { - val routingConfiguration = routingDsl.createConfiguration() - routingConfiguration.init() - registeredRoutes.addAll(routingConfiguration.routes) - registeredExceptionHandlers.putAll(routingConfiguration.exceptionHandlers) - } - - fun routing(vararg containers: DslContainer): DslRoutingPlugin = - routing { - containers.forEach { container -> - addRoutes(container.routes()) - container.exceptionHandlers().forEach { exception(it.type, it.handler) } - } - } - - fun routing(routes: Collection): DslRoutingPlugin = - routing { - addRoutes(routes) - } - - fun routing(vararg routes: ROUTE): DslRoutingPlugin = - routing(routes.toList()) - -} - -fun < - CONFIG : RoutingDslConfiguration, - ROUTE : DslRoute, - CONTEXT, - RESPONSE : Any -> JavalinConfig.routing( - routingDsl: RoutingDslFactory, - vararg routes: DslContainer -) = plugins.register( - DslRoutingPlugin(routingDsl).routing(*routes) -) - -fun JavalinConfig.routing(vararg routes: DefaultRoutes) = - routing(DefaultDsl, *routes) - -fun < - CONFIG : RoutingDslConfiguration, - ROUTE : DslRoute, - CONTEXT, - RESPONSE : Any -> JavalinConfig.routing( - routingDsl: RoutingDslFactory, - init: CONFIG.() -> Unit -) = plugins.register( - DslRoutingPlugin(routingDsl).routing(init) -) - -fun JavalinConfig.routing(init: DefaultConfiguration.() -> Unit) = - routing(DefaultDsl, init) \ No newline at end of file diff --git a/routing-dsl/src/test/kotlin/io/javalin/community/routing/dsl/InPlaceRoutingDslTest.kt b/routing-dsl/src/test/kotlin/io/javalin/community/routing/dsl/InPlaceRoutingDslTest.kt index 4aedeb3..759181e 100644 --- a/routing-dsl/src/test/kotlin/io/javalin/community/routing/dsl/InPlaceRoutingDslTest.kt +++ b/routing-dsl/src/test/kotlin/io/javalin/community/routing/dsl/InPlaceRoutingDslTest.kt @@ -2,6 +2,7 @@ package io.javalin.community.routing.dsl import io.javalin.Javalin import io.javalin.community.routing.Route +import io.javalin.community.routing.dsl.DslRouting.Companion.Dsl import io.javalin.community.routing.dsl.defaults.Path import io.javalin.community.routing.dsl.specification.TestSpecification import io.javalin.testtools.JavalinTest @@ -18,8 +19,8 @@ class InPlaceRoutingDslTest : TestSpecification() { fun `each http dsl method is properly mapped to javalin handler`() = JavalinTest.test( // given: a javalin app with routes defined using the dsl Javalin.create { config -> - config.routing { - before("/before") { header("test", "before") } + config.router.mount(Dsl) { + it.before("/before") { header("test", "before") } after("/after") { header("test", "after") } get("/throwing") { throw RuntimeException() } @@ -37,7 +38,8 @@ class InPlaceRoutingDslTest : TestSpecification() { defaultConfig ) { _, client -> // when: a request is made to the http route - Route.values() + Route.entries + .asSequence() .filter { it.isHttpMethod } .map { it.name.lowercase() } .map { it to request(it, "${client.origin}/$it").asEmpty() }