Skip to content

Commit

Permalink
Fix redirect routes for wildcard patterns.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cody Casterline committed Jan 8, 2024
1 parent 9aef6f1 commit 777c425
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
2 changes: 0 additions & 2 deletions integrations/actix/tests/extract_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ fn test_redirect_app() {
"/bar",
"/bar/",
"/baz/*any",
"/baz/*any/", // !!! TODO
"/baz/:id",
"/baz/:id/",
"/baz/:name",
Expand All @@ -129,7 +128,6 @@ fn test_redirect_app() {
"/baz/{name}",
"/baz/{name}/",
"/baz/{tail:.*}",
"/baz/{tail:.*}", // !!! TODO
"/foo",
"/foo/",
],
Expand Down
19 changes: 10 additions & 9 deletions router/src/components/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,36 +691,37 @@ fn create_routes(
for original_path in expand_optionals(&route_def.path) {
let mut path = join_paths(base, &original_path).to_string();
trailing_slash.normalize_route_path(&mut path);
let mut is_wildcard = false;
let pattern = if is_leaf {
path
} else if let Some((path, _splat)) = path.split_once("/*") {
is_wildcard = true;
path.to_string()
} else {
path
};
acc.push(RouteData {

let route_data = RouteData {
key: route_def.clone(),
id: route_def.id,
matcher: Matcher::new_with_partial(&pattern, !is_leaf),
pattern,
original_path: original_path.into_owned(),
});
};

if is_wildcard {
if route_data.matcher.is_wildcard() {
// already handles trailing_slash
// TODO: confirm/test
} else if let Some(redirect_route) = redirect_route_for(route_def) {
let pattern = &redirect_route.path;
acc.push(RouteData {
let redirect_route_data = RouteData {
id: redirect_route.id,
matcher: Matcher::new_with_partial(&pattern, !is_leaf), // TODO: 👀
matcher: Matcher::new_with_partial(&pattern, !is_leaf),
pattern: pattern.to_owned(),
original_path: pattern.to_owned(),
key: redirect_route,
});
};
acc.push(redirect_route_data);
}

acc.push(route_data);
}
acc
}
Expand Down
5 changes: 5 additions & 0 deletions router/src/matching/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ impl Matcher {
Some(PathMatch { path, params })
}
}

#[doc(hidden)]
pub(crate) fn is_wildcard(&self) -> bool {
self.splat.is_some()
}
}

fn get_segments(pattern: &str) -> Vec<&str> {
Expand Down
1 change: 0 additions & 1 deletion router/tests/extract_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ fn test_generated_routes_redirect() {
"/bar",
"/bar/",
"/baz/*any",
"/baz/*any/", // !!! TODO
"/baz/:id",
"/baz/:id/",
"/baz/:name",
Expand Down

0 comments on commit 777c425

Please sign in to comment.