When using .nest() with dynamic segments in the outer path (e.g., /{version}/api), the nested router will capture those outer segments. If you use a Path extractor in the nested handler, it will contain both the segments defined in the nested router and the segments captured by the outer nesting path.
use axum::{extract::Path, routing::get, Router};
use std::collections::HashMap;
async fn users_get(Path(params): Path<HashMap<String, String>>) {
// Both `version` and `id` are captured even though `users_api` only
// explicitly captures `id`.
let version = params.get("version");
let id = params.get("id");
}
let users_api = Router::new().route("/users/{id}", get(users_get));
// The 'version' segment from the nest is passed into users_api
let app = Router::new().nest("/{version}/api", users_api);
# let _: Router = app;
use axum::{
extract::Path,
routing::get,
Router,
};
use std::collections::HashMap;
async fn users_get(Path(params): Path<HashMap<String, String>>) {
// Both `version` and `id` were captured even though `users_api` only
// explicitly captures `id`.
let version = params.get("version");
let id = params.get("id");
}
let users_api = Router::new().route("/users/{id}", get(users_get));
let app = Router::new().nest("/{version}/api", users_api);
# let _: Router = app;