From 7571227682a687cf094ccebe0cdc23b5de30e2dd Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Fri, 6 May 2022 12:09:14 +0200 Subject: [PATCH 1/2] Support wildcards in typed paths --- axum-macros/src/typed_path.rs | 12 ++++-------- axum-macros/tests/typed_path/fail/wildcard.rs | 7 ------- axum-macros/tests/typed_path/fail/wildcard.stderr | 5 ----- axum-macros/tests/typed_path/pass/wildcards.rs | 12 ++++++++++++ 4 files changed, 16 insertions(+), 20 deletions(-) delete mode 100644 axum-macros/tests/typed_path/fail/wildcard.rs delete mode 100644 axum-macros/tests/typed_path/fail/wildcard.stderr create mode 100644 axum-macros/tests/typed_path/pass/wildcards.rs diff --git a/axum-macros/src/typed_path.rs b/axum-macros/src/typed_path.rs index b1416c8b51..7d91ac6880 100644 --- a/axum-macros/src/typed_path.rs +++ b/axum-macros/src/typed_path.rs @@ -297,14 +297,10 @@ fn parse_path(path: &LitStr) -> syn::Result> { 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())) diff --git a/axum-macros/tests/typed_path/fail/wildcard.rs b/axum-macros/tests/typed_path/fail/wildcard.rs deleted file mode 100644 index f9d6455598..0000000000 --- a/axum-macros/tests/typed_path/fail/wildcard.rs +++ /dev/null @@ -1,7 +0,0 @@ -use axum_extra::routing::TypedPath; - -#[derive(TypedPath)] -#[typed_path("/users/*rest")] -struct MyPath; - -fn main() {} diff --git a/axum-macros/tests/typed_path/fail/wildcard.stderr b/axum-macros/tests/typed_path/fail/wildcard.stderr deleted file mode 100644 index bf897f40cb..0000000000 --- a/axum-macros/tests/typed_path/fail/wildcard.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: `typed_path` cannot contain wildcards - --> tests/typed_path/fail/wildcard.rs:4:14 - | -4 | #[typed_path("/users/*rest")] - | ^^^^^^^^^^^^^^ diff --git a/axum-macros/tests/typed_path/pass/wildcards.rs b/axum-macros/tests/typed_path/pass/wildcards.rs new file mode 100644 index 0000000000..0c9155f71d --- /dev/null +++ b/axum-macros/tests/typed_path/pass/wildcards.rs @@ -0,0 +1,12 @@ +use axum_extra::routing::{RouterExt, TypedPath}; +use serde::Deserialize; + +#[derive(TypedPath, Deserialize)] +#[typed_path("/*rest")] +struct MyPath { + rest: String, +} + +fn main() { + axum::Router::::new().typed_get(|_: MyPath| async {}); +} From 1f6629b63284ab3684f8564ee23b6cd807c75dff Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Fri, 6 May 2022 12:11:08 +0200 Subject: [PATCH 2/2] changelog --- axum-extra/CHANGELOG.md | 2 ++ axum-macros/CHANGELOG.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/axum-extra/CHANGELOG.md b/axum-extra/CHANGELOG.md index 2bf1d8ac0d..6614a7f767 100644 --- a/axum-extra/CHANGELOG.md +++ b/axum-extra/CHANGELOG.md @@ -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) diff --git a/axum-macros/CHANGELOG.md b/axum-macros/CHANGELOG.md index 7ab2df0668..6a1377d040 100644 --- a/axum-macros/CHANGELOG.md +++ b/axum-macros/CHANGELOG.md @@ -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)