Skip to content

Commit

Permalink
fix nested fallback actionable also on nested root
Browse files Browse the repository at this point in the history
  • Loading branch information
dalton-oliveira committed Jan 17, 2024
1 parent 9e31949 commit 6310b3a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion axum/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- None.
- **fixed:** Fallback handlers on nested routers returning 404

# 0.7.4 (13. January, 2024)

Expand Down
10 changes: 10 additions & 0 deletions axum/src/routing/path_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,26 @@ where
.expect("no path for route id. This is a bug in axum. Please file an issue");

let path = path_for_nested_route(prefix, inner_path);
let route_nested_root = !path.ends_with('/') && "/".eq(inner_path.as_ref());

let layer = (
StripPrefix::layer(prefix),
SetNestedPath::layer(path_to_nest_at),
);
match endpoint.layer(layer) {
Endpoint::MethodRouter(method_router) => {
if IS_FALLBACK && route_nested_root {
let path = format!("{path}/");
self.route(&path, method_router.clone())?;
}
self.route(&path, method_router)?;
}
Endpoint::Route(route) => {
if IS_FALLBACK && route_nested_root {
let path = format!("{path}/");
self.route_endpoint(&path, Endpoint::Route(route.clone()))?;
}

self.route_endpoint(&path, Endpoint::Route(route))?;
}
}
Expand Down
13 changes: 13 additions & 0 deletions axum/src/routing/tests/nest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,19 @@ async fn nesting_with_root_inner_router() {
assert_eq!(res.status(), StatusCode::OK);
}

#[tokio::test]
async fn nesting_with_root_inner_fallback() {
let app = Router::new().nest("/router", Router::new().fallback(get(|| async {})));

let client = TestClient::new(app);

let res = client.get("/router").await;
assert_eq!(res.status(), StatusCode::OK);

let res = client.get("/router/").await;
assert_eq!(res.status(), StatusCode::OK);
}

macro_rules! nested_route_test {
(
$name:ident,
Expand Down

0 comments on commit 6310b3a

Please sign in to comment.