mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-29 00:00:18 +02:00
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:
+8
-12
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user