Skip to content

fix: remove trailing / from nested paths#150

Merged
jplatte merged 2 commits intotamasfe:masterfrom
0xf09f95b4:fix-trailing-paths
Jan 12, 2025
Merged

fix: remove trailing / from nested paths#150
jplatte merged 2 commits intotamasfe:masterfrom
0xf09f95b4:fix-trailing-paths

Conversation

@0xf09f95b4
Copy link
Contributor

Hi!

This PR attempts to fix #134.

Currently, API documentation for nested routes is built by combining the path of the base route (with trailing slashes removed) with the path of the nested route.

If a path of a nested route is only "/", as seen in the todo list example, this leads to that path now containing a trailing slash:

ApiRouter::new().nest_api_service("/todo", ApiRouter::new().api_route("/",post_with(handler, docs)))

Generates this path:

"paths": {
    "/todo/": {
        ...
    },
    ...
}

This PR fixes this issue by removing any trailing slahes after the nested path is built in the nest and nest_api_service functions.

One edge case needs to be handled. In a construct like the following:

ApiRouter::new().nest_api_service("/", ApiRouter::new().api_route("/",post_with(handler, docs)))

the path of this route would be empty. A route of just / is therefore returned unmodified.

I've also added a basic unit test.

This seems to be a simple fix, I hope I didn't miss any other cases here.

@2vg 2vg mentioned this pull request Oct 29, 2024
@JakkuSakura
Copy link
Collaborator

This is called URL normalization. It's better to have a fn normalize(&mut String) for all the trims scattered around the crate. There are a few crates for this purpose, but I prefer the simple or builtin approach.

@0xf09f95b4
Copy link
Contributor Author

Cool to see that you took over maintainer duties for this crate. Thank you.

I pushed a new variant of this fix, this time basing it directly on the function that Axum uses for the same route calculation. See this function in path_router.rs.

I don't think this is necessarily standard URL normalization: We don't want to remove trailing slashes everywhere, just in this one specific instance. Thanks to your comment, I did find a flaw with my previous implementation and I reflected that realization in an extended test:

  • if we nest /test with / we want to get /test
  • if we nest /test with /bar we want to get /test/bar
  • BUT if we nest /test with /bar/ we want to get /test/bar/, with a trailing slash.

I also removed the previous test that uses / as a base for nesting, this was officially disallowed in Axum with tokio-rs/axum#3051.

@jplatte jplatte self-requested a review January 10, 2025 16:30
Copy link
Collaborator

@JakkuSakura JakkuSakura left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@jplatte jplatte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A test showing that nesting under /app/ works as expected would be nice, but the implementation looks solid already. 👍🏼

@jplatte jplatte merged commit 70d41c6 into tamasfe:master Jan 12, 2025
1 check passed
dnlsndr pushed a commit to GiroBet/aide that referenced this pull request Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Broken URLs with trailing / due to .nest()

3 participants