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

Support wildcards in typed paths #1003

merged 2 commits into from
May 6, 2022

Conversation

davidpdrsn
Copy link
Member

Fixes #996

Well that was easy 😅

#[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.

@davidpdrsn davidpdrsn merged commit b5183af into main May 6, 2022
@davidpdrsn davidpdrsn deleted the typed-path-wildcard branch May 6, 2022 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make TypedPath support wild cards
2 participants