Skip to content
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

Support wildcards in typed paths #1003

Merged
merged 2 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions axum-extra/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ and this project adheres to [Semantic Versioning].
# Unreleased

- **fixed:** `Option` and `Result` are now supported in typed path route handler parameters ([#1001])
- **fixed:** Support wildcards in typed paths ([#1003])

[#1001]: https://github.com/tokio-rs/axum/pull/1001
[#1003]: https://github.com/tokio-rs/axum/pull/1003

# 0.3.0 (27. April, 2022)

Expand Down
2 changes: 2 additions & 0 deletions axum-macros/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# Unreleased

- **fixed:** `Option` and `Result` are now supported in typed path route handler parameters ([#1001])
- **fixed:** Support wildcards in typed paths ([#1003])

[#1001]: https://github.com/tokio-rs/axum/pull/1001
[#1003]: https://github.com/tokio-rs/axum/pull/1003

# 0.2.0 (31. March, 2022)

Expand Down
12 changes: 4 additions & 8 deletions axum-macros/src/typed_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,10 @@ fn parse_path(path: &LitStr) -> syn::Result<Vec<Segment>> {
path.value()
.split('/')
.map(|segment| {
if segment.contains('*') {
return Err(syn::Error::new_spanned(
path,
"`typed_path` cannot contain wildcards",
));
}

if let Some(capture) = segment.strip_prefix(':') {
if let Some(capture) = segment
.strip_prefix(':')
.or_else(|| segment.strip_prefix('*'))
{
Ok(Segment::Capture(capture.to_owned(), path.span()))
} else {
Ok(Segment::Static(segment.to_owned()))
Expand Down
7 changes: 0 additions & 7 deletions axum-macros/tests/typed_path/fail/wildcard.rs

This file was deleted.

5 changes: 0 additions & 5 deletions axum-macros/tests/typed_path/fail/wildcard.stderr

This file was deleted.

12 changes: 12 additions & 0 deletions axum-macros/tests/typed_path/pass/wildcards.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use axum_extra::routing::{RouterExt, TypedPath};
use serde::Deserialize;

#[derive(TypedPath, Deserialize)]
#[typed_path("/*rest")]
struct MyPath {
rest: String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vec<String> as another test case would be good I think. Also making sure the right thing ends up in here. But maybe that's covered for non-typed paths so there's not much of a point?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually thats why I thought supporting wildcards was goanna be hard but turns out Path doesn't split wildcards into multiple segments at all. So if you do

let app = Router::new().route("/*rest", get(handler));

async fn handler(Path(rest): Path<Vec<String>>) {
    dbg!(rest);
}

and then call GET /foo/a/b rest will be ["/foo/a/b"]. It doesn't automatically know to split things by /.

If you do

#[derive(Deserialize, Debug)]
struct Params {
    rest: Vec<String>
}

async fn handler(Path(rest): Path<Params>) {
    dbg!(&rest);
}

Path will fail with Unsupported type alloc::vec::Vecalloc::string::String`.

So users would have to write their own deserializer to do that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, okay.

}

fn main() {
axum::Router::<axum::body::Body>::new().typed_get(|_: MyPath| async {});
}