SMILEY4 / ktor-swagger-ui

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

Can I hide a route? #32

Closed dashfwd closed 1 year ago

dashfwd commented 1 year ago

I have some routes that aren't API endpoints; some are pages that generate HTML etc; I don't want them to be a part of the swagger docs. Is there a way to mark a particular route as hidden so it doesn't show up in the UI?

I could also see this as something that would be useful for endpoints that aren't yet public.

dashfwd commented 1 year ago

Looks like one approach would be to mark it deprecated:

            get("/", {
                description = "Web path, don't use this"
                this.deprecated = true
            })

Not the best, but better than nothing

SMILEY4 commented 1 year ago

Hi, this is possible in the configuration via the pathFilter.

Filter to apply to all routes. Return 'false' for routes to not include them in the OpenApi-Spec and Swagger-UI. The url of the paths are already split at '/'.

example:

install(SwaggerUI) {
    pathFilter = {_, url -> !url.contains("hidden")} // should filter out all routes which contain the part "hidden"
}
dashfwd commented 1 year ago

That's perfect thanks @SMILEY4 , here's what I ended up using just in case it's useful to anyone else:

install(SwaggerUI) {
    pathFilter = {_, urlList -> urlList.getOrNull(0) == "api"  }
}

since all my API URLs start with /api.