Skip to content

Commit 0a2585e

Browse files
fix nested fallback actionable also on nested root
1 parent 9e31949 commit 0a2585e

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

Diff for: axum/CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
# Unreleased
99

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

1212
# 0.7.4 (13. January, 2024)
1313

Diff for: axum/src/routing/path_router.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,26 @@ where
180180
.expect("no path for route id. This is a bug in axum. Please file an issue");
181181

182182
let path = path_for_nested_route(prefix, inner_path);
183-
183+
let route_nested_root = !path.ends_with('/') && "/".eq(inner_path.as_ref());
184+
let nested_root = match IS_FALLBACK && route_nested_root {
185+
true => Some(format!("{path}/")),
186+
false => None,
187+
};
184188
let layer = (
185189
StripPrefix::layer(prefix),
186190
SetNestedPath::layer(path_to_nest_at),
187191
);
188192
match endpoint.layer(layer) {
189193
Endpoint::MethodRouter(method_router) => {
194+
if let Some(nested_root) = nested_root {
195+
self.route(&nested_root, method_router.clone())?;
196+
}
190197
self.route(&path, method_router)?;
191198
}
192199
Endpoint::Route(route) => {
200+
if let Some(nested_root) = nested_root {
201+
self.route_endpoint(&nested_root, Endpoint::Route(route.clone()))?;
202+
}
193203
self.route_endpoint(&path, Endpoint::Route(route))?;
194204
}
195205
}

Diff for: axum/src/routing/tests/nest.rs

+13
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,19 @@ async fn nesting_with_root_inner_router() {
383383
assert_eq!(res.status(), StatusCode::OK);
384384
}
385385

386+
#[tokio::test]
387+
async fn nesting_with_root_inner_fallback() {
388+
let app = Router::new().nest("/router", Router::new().fallback(get(|| async {})));
389+
390+
let client = TestClient::new(app);
391+
392+
let res = client.get("/router").await;
393+
assert_eq!(res.status(), StatusCode::OK);
394+
395+
let res = client.get("/router/").await;
396+
assert_eq!(res.status(), StatusCode::OK);
397+
}
398+
386399
macro_rules! nested_route_test {
387400
(
388401
$name:ident,

0 commit comments

Comments
 (0)