-
Notifications
You must be signed in to change notification settings - Fork 405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: 2 overlapping routes on the same level are executed #231
Comments
The same observation is done for koa-router@next var r = require('koa-router')();
r.get('/my/:path/1', function(ctx, next){ console.log('route #1'); next() })
r.get('/my/:path/:id', function(ctx, next){ console.log('route #2'); next()})
var ctx = { path: '/my/path/1', method: 'GET' }
var mw = r.middleware()
mw(ctx,function(){}).then(function(res) { console.log('done')}, function(err) {console.log(err)}) leads to
|
I modified an existing test to show a reproduction of the multiple matching route bug |
Yes, this is a regression. The intended behavior is: the more specific route matches, then the route that was registered first. Ideally it would throw an error when registering overlapping routes, but that would incur more backwards-incompatibility. |
The code has changed a lot since I looked at it in 3.1.0, with the layers and inheritance. I could take a look at it to see how we could modify things, but does koa-router already have "order matching tests" where all these ordering rules are tested ? I just looked rapidly at the tests but did not find something clearly identified as such + I am not sure I understand how orderding should work among a "native" route like "/a/b/c" and a route composed with multiple routers like "/a" ("/b/c") |
Any ETA on a fix for this issue? Just like @jeromew I too am relying on only one route being executed, and I'd rather not stick with my hacky workaround for any length of time. |
Has anybody found a workaround for this? Any thoughts on this @alexmingoia? I've looked at other non-Koa routers, and they all seem to act they way you described above. |
@jbielick I saw your comment on #292 regarding this. I am trying to see how it could be fixed. As I understand it, the current code is trying to mix 2 features : (1) was working and when (2) was introduced it created backward incompatibility because the algorithm forgot to take (1) into account. Now this was more than a year ago and people seem to be trying to make usage of this bug in unexpected ways (leading to more bugs) - cf issue #311. Looking at the code, here is a fix that may help fix (1) while keeping (2) : among all matching routes keep "the more specific route matches, then the route that was registered first" + keep all the matching routes which have the After that, what will be missing I think is a cristal clear priority rule mechanism in koa-router that could be tested and to which people could be directed to avoid issues. Here are some docs I extracted from hapi router - https://hapijs.com/api - for comparison
Tell me what you think |
Is this fixed in the upcoming 8.0 branch? It looks like it is, but I'm not sure. There's so many issues related to this one thing: https://github.com/alexmingoia/koa-router/blob/c0e9e088cd679f3c4030b73d8c0fc7b69e1ffecb/test/matched.test.js#L64-L73 |
@mikew it is addressed in the 8.x branch |
Any ETA on this? This feels like a bug that should patched separately from 8.x. Why delay this bug fix? It has been dangling for 2 1/2 years. Thank you. |
@landau Would you like to pull request a fix? There is no one working on this library full-time. |
I've just been bitten by this :( I have "koa-router": "^7.4.0" |
Note: You need to make sure that more specific routes are matched first! For convenience, all my endpoints are resources with a prefix:
By compiling middleware in order, The debouncing middleware: /**
* Debounce router calls for multiple matches
*/
export const debounceRouter: Koa.Middleware = async (ctx, next) => {
ctx.state.firstMatch = _.isNil(ctx.state.firstMatch);
if (!ctx.state.firstMatch) {
return; // terminate middleware chain
}
await next();
}; Make sure the router middleware runs functions as |
I've seen weird behaviors too. Maybe I misunderstood the usage.
and
at the same time, I can't even complete a |
Hello,
I tried to upgrade from 3.1.0 to 5.3.0 and ran into a problem which I think is a backward incompatibility issue. I am not sure if it is now a feature or a bug.
When you have 2 conflicting routes, koa-router used to select one (the first defined) and go with that route. Now, koa-router follow both routes.
After some tests, the problem seem to have appeared between 5.1.3 and 5.2.0. Here is a quick explanation of the problem :
Launched with 5.1.2, it gives
Launched with 5.2.0 it gives
Launched with 5.3.0 it gives
I currently rely on the fact that only one route get executed in such a scenario.
Is this a bug or should I rewrite my routes differently ?
multiple route matching seems incompatible with #41 (comment) so I wonder if this is a major evolution of the design related the nesting feature or just a bug.
The release note on 5.2 - 8f30c9c does not seem to mention this.
The text was updated successfully, but these errors were encountered: