Skip to content
Myr

axum-macros: report invalid TypedPath capture names without panicking (#3918)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Shuvro Roy · committed by GitHub · parent f8b02f2 · Browse files · 4 files+56 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+56 added−4 removed

  • tests:+45 added−0 removed
  • other:+11 added−4 removed

4 files+56 added−4 removed

modified:axum-macros/src/typed_path.rs+9 added-4 removed
402 unmodified lines
403 if capture.is_empty() || capture.contains('{') {404 return Err(syn::Error::new_spanned(path, "invalid capture in path"));405 }406407 segments.push(Segment::Capture(408 capture.strip_prefix('*').unwrap_or(capture).to_owned(),409 path.span(),410 ));407 let capture = capture.strip_prefix('*').unwrap_or(capture);408 if syn::parse_str::<syn::Ident>(capture).map_or(true, |ident| ident != capture) {409 return Err(syn::Error::new_spanned(410 path,411 format!("Invalid capture name `{capture}`: expected a Rust identifier"),412 ));413 }414415 segments.push(Segment::Capture(capture.to_owned(), path.span()));416417 rest = &rest[end + 1..];418 } else {419 segments.push(Segment::Static(format!("{{{rest}")));420 rest = "";
105 unmodified lines
added:axum-macros/tests/typed_path/fail/invalid_capture_name.rs+22 added
1use axum_macros::TypedPath;2use serde::Deserialize;34#[derive(TypedPath, Deserialize)]5#[typed_path("/users/{user-id}")]6struct Named {7 user_id: String,8}910#[derive(TypedPath, Deserialize)]11#[typed_path("/users/{123}")]12struct Tuple(String);1314#[derive(TypedPath, Deserialize)]15#[typed_path("/files/{*file-name}")]16struct Wildcard(String);1718#[derive(TypedPath, Deserialize)]19#[typed_path("/users/{user_id }")]20struct Whitespace(String);2122fn main() {}
added:axum-macros/tests/typed_path/fail/invalid_capture_name.stderr+23 added
1error: Invalid capture name `user-id`: expected a Rust identifier2 --> tests/typed_path/fail/invalid_capture_name.rs:5:143 |45 | #[typed_path("/users/{user-id}")]5 | ^^^^^^^^^^^^^^^^^^67error: Invalid capture name `123`: expected a Rust identifier8 --> tests/typed_path/fail/invalid_capture_name.rs:11:149 |1011 | #[typed_path("/users/{123}")]11 | ^^^^^^^^^^^^^^1213error: Invalid capture name `file-name`: expected a Rust identifier14 --> tests/typed_path/fail/invalid_capture_name.rs:15:1415 |1615 | #[typed_path("/files/{*file-name}")]17 | ^^^^^^^^^^^^^^^^^^^^^1819error: Invalid capture name `user_id `: expected a Rust identifier20 --> tests/typed_path/fail/invalid_capture_name.rs:19:1421 |2219 | #[typed_path("/users/{user_id }")]23 | ^^^^^^^^^^^^^^^^^^^
modified:axum-macros/CHANGELOG.md+2 added
5 unmodified lines
6and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).78# Unreleased910- **fixed:** Report invalid `TypedPath` capture names without panicking ([#3918])11- **breaking:** `#[from_request(via(Extractor))]` now uses the extractor's12 rejection type instead of `axum::response::Response` ([#3261])13- **breaking:** `Option<T>` fields in `#[derive(FromRequest)]` and14 `#[derive(FromRequestParts)]` now use `OptionalFromRequest` /
2 unmodified lines
17 silently converting all rejections to `None`. Extractors used with18 `Option<T>` must implement `OptionalFromRequest` or19 `OptionalFromRequestParts`. ([#3623])2021[#3918]: https://github.com/tokio-rs/axum/pull/391822[#3261]: https://github.com/tokio-rs/axum/pull/326123[#3623]: https://github.com/tokio-rs/axum/issues/36232425# 0.5.1
203 unmodified lines