-
Notifications
You must be signed in to change notification settings - Fork 1.4k
axum: Update matchit to 0.8.6 and support capture prefixes and suffixes #3679
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| [default] | ||
| extend-ignore-re = [ | ||
| "`([^`]+)`", | ||
| ] | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| Add another route to the router. | ||
|
|
||
| `path` is a string of path segments separated by `/`. Each segment | ||
| can be either static, a capture, or a wildcard. | ||
| can either be static, contain a capture, or be a wildcard. | ||
|
|
||
| `method_router` is the [`MethodRouter`] that should receive the request if the | ||
| path matches `path`. Usually, `method_router` will be a handler wrapped in a method | ||
|
|
@@ -24,11 +24,15 @@ Paths can contain segments like `/{key}` which matches any single segment and | |
| will store the value captured at `key`. The value captured can be zero-length | ||
| except for in the invalid path `//`. | ||
|
|
||
| Each segment may have only one capture, but it may have static prefixes and suffixes. | ||
|
|
||
| Examples: | ||
|
|
||
| - `/{key}` | ||
| - `/users/{id}` | ||
| - `/users/{id}/tweets` | ||
| - `/avatars/large_{id}.png` | ||
| - `/avatars/small_{id}.jpg` | ||
|
|
||
| Captures can be extracted using [`Path`](crate::extract::Path). See its | ||
| documentation for more details. | ||
|
|
@@ -38,6 +42,31 @@ regular expression. You must handle that manually in your handlers. | |
|
|
||
| [`MatchedPath`] can be used to extract the matched path rather than the actual path. | ||
|
|
||
| Captures must not be empty. For example `/a/` will not match `/a/{capture}` and | ||
| `/.png` will not match `/{image}.png`. | ||
|
|
||
| You may mix captures that have different static prefixes or suffixes, though it is discouraged as it | ||
| might lead to surprising behavior. If multiple routes would match, the one with the longest static | ||
| prefix is used, if there are multiple with the same match, the longest matched static suffix is | ||
| chosen. For example, if a request is done to `/abcdef` here are examples of routes that would all | ||
| match. If multiple of these were defined in a single router, the topmost one would be used. | ||
|
|
||
| - `/abcdef` | ||
| - `/abc{x}ef` | ||
| - `/abc{x}f` | ||
| - `/abc{x}` | ||
| - `/a{x}def` | ||
| - `/a{x}` | ||
| - `/{x}def` | ||
| - `/{x}` | ||
|
Comment on lines
+48
to
+61
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the reason we didn't merge the 0.8.6 update. The logic is simply too surprising. If you want to push this forward, can you please update matchit to 0.9 and change the documentation (and possibly the new tests) to align with that version? That version disallows these semi-conflicting routes in a single router. |
||
|
|
||
| This is done on each level of the path and if the path matches even if due to a wildcard, that path | ||
| will be chosen. For example if one makes a request to `/foo/bar/baz` the first route will be used by | ||
| axum because it has better match on the leftmost differing path segment and the whole path matches. | ||
|
|
||
| - `/foo/{*wildcard}` | ||
| - `/fo{x}/bar/baz` | ||
|
|
||
| # Wildcards | ||
|
|
||
| Paths can end in `/{*key}` which matches all segments and will store the segments | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to suppress the false positive of type checker
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is much too broad of an exclusion. Just update the docs sample to something else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Content wrapped in backticks usually consists of proper nouns and inline code, so I think it’s reasonable to exclude this pattern.