SMILEY4 / ktor-swagger-ui

Kotlin Ktor plugin to generate OpenAPI and provide Swagger UI
Apache License 2.0
156 stars 25 forks source link

KTOR Rate Limit Plugin Bug #43

Closed ScottPierce closed 1 year ago

ScottPierce commented 1 year ago

When Using the Ktor Rate Limit Plugin, the routes are documented incorrectly, and show up with a prefix: / (RateLimit <rate limit name)/<your endpoint here>.

    install(RateLimit) {
        register(apiTokenRateLimit) {
            rateLimiter(limit = 100, refillPeriod = 60.seconds)
            requestKey { call ->
                // We've already checked and confirmed that the API Token exists.
                call.request.headers[ApiTokenValidator.API_TOKEN_HEADER]!!.trim()
            }
        }
    }

    routing {
        rateLimit(apiTokenRateLimit) {
            myRoutes()
        }
    }
image
ScottPierce commented 1 year ago

This is solved by using ignoredRouteSelectors.

    install(SwaggerUI) {
        ignoredRouteSelectors = ignoredRouteSelectors + RateLimitRouteSelector::class

This should be documented somewhere, and ideally it'd be ignored by default, along with other ktor plugins.

SMILEY4 commented 1 year ago

Hi, your solution is the intended one. I'm not sure if i can easily ignore all "unnecessary" route-selectors by default, so i'd rather have the default-behaviour be an obviously weird-looking route than a route where a part is silently missing. I'll check again and make the documentation regarding these types of configs more obvious.

ScottPierce commented 1 year ago

That makes sense. Instead of adding classes, which requires a reference to the library, we could always change it to a Set of String qualified class names that should be ignored.

That would allow us to add known ktor plugins like this, that should be automatically ignored.