-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
route
has small footgun when trying to create nested routes
#174
Comments
route
has footgun when trying to create nested routesroute
has small footgun when trying to create nested routes
From discord for context:
|
I would actually like to explore if this could be made to "just work" in the way Note that if we do this we should make sure the implementation is still compatible with openapi generation. |
@davidpdrsn it looks like a great idea, it might be worth exploring this possibility. |
normalizing |
I have thought a bit about this and not sure its possible. In order for it to work That would mean I suppose |
I wonder if it could be possible to detect these routing footguns "while starting/building the service". I don't know how this idea could translate into something doable, though. I might have to take a look at the source myself to make my ideas more concrete 😄 |
That would probably encounter the same issues. Axums routing is quite generic by design which sometimes complicates things. |
But you're off course welcome to give it a shot 😊 |
@davidpdrsn if you don't mind, could you give an example showing how these changes would break routing to arbitrary services? I've been thinking about it lately and the solution of 1) removing |
I think your right and this is probably the way we want to go. I'm gonna check out the express docs. Thanks for sharing those. Something else thats related I've thought about: Currently this is a bit of a foot gun as well However I think it can be solved by requests not getting "trapped" inside a I think we have to implement it and test before we'll know for sure though. I hope to get some time during the weekend to explore this. |
That would be interesting to experiment. I don't know any web framework who does it this way. I think that another valid solution would be to detect this "unreachable route" problem before actually serving requests. It looks feasible and it would be very helpful, in case no other solution is accepted. |
Warp does; |
Right. Axum's |
I did #224 yesterday which makes routing a bit more intuitive I think. I also took a stab at making
It might still be fixable by only calling However with #224 I think routing is in a decent place so I think we should punt on this issue for 0.2 and address it later. I would like to get 0.2 out next week and don't consider this issue critical. |
If you add that check, then I think I'll try to write up everything I absorbed about this routing issue later to ease our understanding of the problem. This way we'll be able to figure out an adequate solution. |
Wish List
My current viewCurrently, axum's routing API is composed of Fortunately, there's the option to encode prefix | full match desire into the Misconception about ExpressAlso, I needed to talk about this sentence I wrote a while ago:
Actually, this isn't what Express does. It distinguishes between defining endpoints (which uses full match + regexes) and mouting a group of routes in a prefix path. SummaryWith the following changes the routing part of axum would be in a even better place:
I can try to help with some of those if you don't mind about the timeline right now (I'm close to finish the semester on my university) Edit: AlternativesWe explain |
This is orthogonal to how "route" or "nest" works. Changing "route" to do prefix matching won't make detecting unreachable routes easier. That will require changes to the overall router design similar to what's described in #240
Isn't this also what axum does? Except that "mount" is called "nest". I would be fine with renaming "nest" to "mount". I named it nest originally because that's what tide calls it.
Can you give some examples of the kinds of routes you imagine being able to define, and which requests they'd accept? Include both handlers and general services. Are there other frameworks where using wildcards strips the matched prefix from URI before calling the handler/endpoint/service in the way "nest" does, while using the common "add endpoint here"-method. Personally this isn't the behavior I would expect. I think the there value in having those two behaviors in two separate functions. It's also not what tide does. They have a specific method you call to enable prefix matching. |
In rocket I believe that self-documented code improve maintainability of the project. So IMHO better solution would be rename |
I disagree. I think "route" is fine. Really there are many words that describe similar things. I don't "endpoint" is objectively better than "route". Rails also has the concept of "mounting" other apps inside a router. So mounting is a word that's used. To be clear I have no intentions of renaming nest, unless people thinks it would make it's behavior more clear. |
I don't think we should go this way anymore.
Here's a small example: Current behavior: .route("/users", handler)
.nest("/api", api_routes)
.route("/service", tower_service) Propposed behavior: .route("/users", handler)
.route("/api/*", api_routes)
.route("/service", tower_service) Let me know if I missed any case that could be interesting to think about.
Yes. Also, for Express "everything is middleware", and for Axum "everything is
I think we shouldn't rename them unless we find evidence that it would be, in fact, better.
Here, we have the option to unify these concepts because axum sees everything as The main point is: both APIs are fine for me, as long as we add unreachable route detection. However, if |
My initial thoughts were that I agree that |
Is this useful is practice? I tend to think that handlers don't care where they are mounted. You just "plug" them where you want to. |
I feel like both are useful but I don't really have any specific use case in mind. At least to me it would be surprising if |
Lot of frameworks separate concept of defining endpoints and group of routes.
grp1 := r.Group("/grp")
{
grp1.GET("/route1", Controller1)
grp1.GET("/route2", Controller2)
}
scope '/admin' do
resources :articles, :comments
end
App::new().service(
web::scope("/api")
.service(...)
.service(...),
) So I find that
|
@z4rx thanks for bringing the examples. I think the difference here in Axum is that it's reasonably easy to try to mix these concepts because of the use of Let me make some observations about the examples to illustrate that:
Anyway, to improve the current situation without touching in the API, maybe we could have a way to generate routing information (like |
I consider that part of #50 |
This panics if you pass a `Router` to `Router::route`. Thats invalid and will result in unreachable routes. Unfortunately I don't think we can make it a type error while supporting general `Service`s. So I think this is a decent workaround. Fixes #174
This panics if you pass a `Router` to `Router::route`. Thats invalid and will result in unreachable routes. Unfortunately I don't think we can make it a type error while supporting general `Service`s. So I think this is a decent workaround. Fixes #174
Originally reported on Axum's discord channel.
As a new user to Axum, this is a piece of code which may be written while trying to implement nested routes:
It compiles fine, although it doesn't work. While investigating the problem, I found out that it's necessary to use the
nest
function to nest routes properly. It would be great if this code could just work, or generate an error instead of compiling.The text was updated successfully, but these errors were encountered: