Fix Router::merge for overlapping routes same different methods (#543)

Discovered while working on something else that we didn't properly
handle this:

```rust
let one = Router::new().route("/", get(|| async {}));
let two = Router::new().route("/", post(|| async {}));

let app = one.merge(two);
```
This commit is contained in:
David Pedersen
2021-11-19 21:15:41 +01:00
committed by GitHub
parent 1d94d75c57
commit 22931688f7
2 changed files with 24 additions and 12 deletions
+8 -12
View File
@@ -244,12 +244,15 @@ where
nested_at_root,
} = other;
if let Err(err) = self.node.merge(node) {
self.panic_on_matchit_error(err);
}
for (id, route) in routes {
assert!(self.routes.insert(id, route).is_none());
let path = node
.route_id_to_path
.get(&id)
.expect("no path for route id. This is a bug in axum. Please file an issue");
self = match route {
Endpoint::MethodRouter(route) => self.route(path, route),
Endpoint::Route(route) => self.route(path, route),
};
}
self.fallback = match (self.fallback, fallback) {
@@ -603,13 +606,6 @@ impl Node {
Ok(())
}
fn merge(&mut self, other: Node) -> Result<(), matchit::InsertError> {
for (id, path) in other.route_id_to_path {
self.insert(&*path, id)?;
}
Ok(())
}
fn at<'n, 'p>(
&'n self,
path: &'p str,