Skip to content

Feature request: add config option to control trailing slash behaviour in From<SwaggerUi> for axum::Router #1467

@aleksanderkrauze

Description

@aleksanderkrauze

I am using utoipa_swagger_ui::SwaggerUi in an axum project. I added tower_http::normalize_path::NormalizePathLayer to my application in order to remove trailing slashes from requests. However this is incompatible with how axum::Router is constructed from SwaggerUi. Right now it uses following logic:

let path = if path.ends_with('/') {
    &path[..path.len() - 1]
} else {
    path
};
debug_assert!(!path.is_empty());

let slash_path = format!("{}/", path);
router
    .route(
        path,
        routing::get(|| async move { axum::response::Redirect::to(&slash_path) }),
    )
    .route(&format!("{}/", path), handler.clone())
    .route(&format!("{}/*rest", path), handler)

So when I do this:

SwaggerUi::new("/swagger")
    .url("/api-doc/openapi.json", openapi)
    .into() // into axum::Router

following endpoints are generated:

  • /swagger which redirects to /swagger/
  • /swagger/ which handles the main page
  • /swagger/{*rest} which handles assets

This is however incompatible with removing trailing slashes from all requests, because it creates an infinite redirect loop. I want to ask for a feature, where I could configure SwaggerUi to change this behaviour. For example you could add trim_trailing_slash(enabled: bool) method to SwaggerUi which by default would be set to false (to keep current behaviour), but once enabled it would generate two endpoints:

  • /swagger which handles the main page
  • /swagger/{*rest} which handles assets

If you accept this request I would be glad to provide an implementation PR.

PS. I have managed to figure out a temporary workaround for my current project, so you don't have to try and help me come up with one. However, like workarounds usually are, it is not very nice and I would prefer to be able to configure behaviour of utoipa-swagger-ui crate instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions