Skip to content

Commit

Permalink
Fix axum path nesting (#1231)
Browse files Browse the repository at this point in the history
Fix axum path nesting in `OpenApiRouter` to allow empty paths and
correctly template the path for `OpenApiRouter` from colons.

Relates to #1199
  • Loading branch information
juhaku authored Dec 9, 2024
1 parent 19fbf67 commit c423be5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions utoipa-axum/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed

* Fix axum path nesting (https://github.com/juhaku/utoipa/pull/1231)
* Fix diverging axum route and openapi spec (https://github.com/juhaku/utoipa/pull/1199)

## 0.1.2 - Oct 29 2024
Expand Down
12 changes: 6 additions & 6 deletions utoipa-axum/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,23 +322,23 @@ where
pub fn nest(self, path: &str, router: OpenApiRouter<S>) -> Self {
// from axum::routing::path_router::path_for_nested_route
// method is private, so we need to replicate it here
fn path_for_nested_route<'a>(prefix: &'a str, path: &'a str) -> String {
fn path_for_nested_route(prefix: &str, path: &str) -> String {
let path = if path.is_empty() { "/" } else { path };
debug_assert!(prefix.starts_with('/'));
debug_assert!(path.starts_with('/'));

if prefix.ends_with('/') {
format!("{prefix}{}", path.trim_start_matches('/')).into()
format!("{prefix}{}", path.trim_start_matches('/'))
} else if path == "/" {
prefix.into()
} else {
format!("{prefix}{path}").into()
format!("{prefix}{path}")
}
}

let api = self.1.nest_with_path_composer(
path_for_nested_route(path, "/"),
path_for_nested_route(&path_template(path), "/"),
router.1,
|a: &str, b: &str| path_for_nested_route(a, b),
path_for_nested_route,
);
let router = self.0.nest(&colonized_params(path), router.0);

Expand Down

0 comments on commit c423be5

Please sign in to comment.