Skip to content

Commit

Permalink
Merge pull request #71 from Giuliopime/fix/query-parameters-ignored-f…
Browse files Browse the repository at this point in the history
…or-path-composition

Fix/query parameters ignored for path composition
  • Loading branch information
SMILEY4 authored Nov 13, 2023
2 parents ef627ee + 91ab052 commit 6ae057d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import io.github.smiley4.ktorswaggerui.dsl.DocumentedRouteSelector
import io.github.smiley4.ktorswaggerui.dsl.OpenApiRoute
import io.ktor.http.HttpMethod
import io.ktor.server.auth.AuthenticationRouteSelector
import io.ktor.server.routing.HttpMethodRouteSelector
import io.ktor.server.routing.RootRouteSelector
import io.ktor.server.routing.Route
import io.ktor.server.routing.RouteSelector
import io.ktor.server.routing.TrailingSlashRouteSelector
import io.ktor.server.routing.*
import kotlin.reflect.full.isSubclassOf

class RouteCollector(
Expand Down Expand Up @@ -52,6 +48,7 @@ class RouteCollector(
return (route.selector as HttpMethodRouteSelector).method
}

@Suppress("CyclomaticComplexMethod")
private fun getPath(route: Route, config: PluginConfigData): String {
val selector = route.selector
return if (isIgnoredSelector(selector, config)) {
Expand All @@ -63,6 +60,9 @@ class RouteCollector(
is DocumentedRouteSelector -> route.parent?.let { getPath(it, config) } ?: ""
is HttpMethodRouteSelector -> route.parent?.let { getPath(it, config) } ?: ""
is AuthenticationRouteSelector -> route.parent?.let { getPath(it, config) } ?: ""
is ParameterRouteSelector -> route.parent?.let { getPath(it, config) } ?: ""
is ConstantParameterRouteSelector -> route.parent?.let { getPath(it, config) } ?: ""
is OptionalParameterRouteSelector -> route.parent?.let { getPath(it, config) } ?: ""
else -> (route.parent?.let { getPath(it, config) } ?: "") + "/" + route.selector.toString()
}
}
Expand All @@ -75,6 +75,9 @@ class RouteCollector(
is DocumentedRouteSelector -> true
is HttpMethodRouteSelector -> true
is AuthenticationRouteSelector -> true
is ParameterRouteSelector -> true
is ConstantParameterRouteSelector -> true
is OptionalParameterRouteSelector -> true
else -> config.ignoredRouteSelectors.any { selector::class.isSubclassOf(it) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.fasterxml.jackson.databind.SerializationFeature
import io.github.smiley4.ktorswaggerui.SwaggerUI
import io.github.smiley4.ktorswaggerui.dsl.resources.get
import io.github.smiley4.ktorswaggerui.dsl.resources.post
import io.github.smiley4.ktorswaggerui.dsl.resources.delete
import io.ktor.http.HttpStatusCode
import io.ktor.resources.Resource
import io.ktor.serialization.jackson.jackson
Expand All @@ -28,7 +27,7 @@ fun main() {
}

@Resource("articles")
class ArticlesRoute
class ArticlesRoute(val sorting: String?)

data class Article(
val title: String,
Expand Down Expand Up @@ -76,6 +75,11 @@ private fun Application.myModule() {
tags = listOf("articles")
description = "Articles endpoint"
operationId = "get-articles"
request {
queryParameter<String?>("sorting") {
description = "Optional sorting applied to articles"
}
}
response {
default {
description = "Default Response"
Expand All @@ -89,7 +93,10 @@ private fun Application.myModule() {
}
}
}) {
call.respond(HttpStatusCode.OK, "No articles yet")
if (it.sorting != null)
call.respond(HttpStatusCode.OK, "No articles yet (with ${it.sorting} sorting)")
else
call.respond(HttpStatusCode.OK, "No articles yet")
}

post<ArticlesRoute>({
Expand Down

0 comments on commit 6ae057d

Please sign in to comment.