Skip to content

Commit feee430

Browse files
committed
Support deserializing paths to sequences for multi-component (eg. tail) matches
1 parent f61fcbe commit feee430

File tree

3 files changed

+119
-3
lines changed

3 files changed

+119
-3
lines changed

actix-router/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## 0.5.3
66

77
- Add `unicode` crate feature (on-by-default) to switch between `regex` and `regex-lite` as a trade-off between full unicode support and binary size.
8+
- Add support for extracting multi-component path params into a sequence (Vec, tuple, ...)
89
- Minimum supported Rust version (MSRV) is now 1.72.
910

1011
## 0.5.2

actix-router/src/de.rs

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,25 @@ impl<'de> Deserializer<'de> for Value<'de> {
396396
visitor.visit_newtype_struct(self)
397397
}
398398

399-
fn deserialize_tuple<V>(self, _: usize, _: V) -> Result<V::Value, Self::Error>
399+
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
400+
where
401+
V: Visitor<'de>,
402+
{
403+
let value_seq = ValueSeq::new(self.value);
404+
if len == value_seq.len() {
405+
visitor.visit_seq(value_seq)
406+
} else {
407+
Err(de::value::Error::custom(
408+
"path and tuple lengths don't match",
409+
))
410+
}
411+
}
412+
413+
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
400414
where
401415
V: Visitor<'de>,
402416
{
403-
Err(de::value::Error::custom("unsupported type: tuple"))
417+
visitor.visit_seq(ValueSeq::new(self.value))
404418
}
405419

406420
fn deserialize_struct<V>(
@@ -428,7 +442,6 @@ impl<'de> Deserializer<'de> for Value<'de> {
428442
}
429443

430444
unsupported_type!(deserialize_any, "any");
431-
unsupported_type!(deserialize_seq, "seq");
432445
unsupported_type!(deserialize_map, "map");
433446
unsupported_type!(deserialize_identifier, "identifier");
434447
}
@@ -498,6 +511,45 @@ impl<'de> de::VariantAccess<'de> for UnitVariant {
498511
}
499512
}
500513

514+
struct ValueSeq<'de> {
515+
value: &'de str,
516+
elems: std::str::Split<'de, char>,
517+
}
518+
519+
impl<'de> ValueSeq<'de> {
520+
fn new(value: &'de str) -> Self {
521+
Self {
522+
value,
523+
elems: value.split('/'),
524+
}
525+
}
526+
527+
fn len(&self) -> usize {
528+
self.value.split('/').filter(|s| !s.is_empty()).count()
529+
}
530+
}
531+
532+
impl<'de> de::SeqAccess<'de> for ValueSeq<'de> {
533+
type Error = de::value::Error;
534+
535+
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
536+
where
537+
T: de::DeserializeSeed<'de>,
538+
{
539+
for elem in &mut self.elems {
540+
if !elem.is_empty() {
541+
return seed.deserialize(Value { value: elem }).map(Some);
542+
}
543+
}
544+
545+
Ok(None)
546+
}
547+
548+
fn size_hint(&self) -> Option<usize> {
549+
Some(self.len())
550+
}
551+
}
552+
501553
#[cfg(test)]
502554
mod tests {
503555
use serde::Deserialize;
@@ -532,6 +584,16 @@ mod tests {
532584
val: TestEnum,
533585
}
534586

587+
#[derive(Debug, Deserialize)]
588+
struct TestSeq1 {
589+
tail: Vec<String>,
590+
}
591+
592+
#[derive(Debug, Deserialize)]
593+
struct TestSeq2 {
594+
tail: (String, String, String),
595+
}
596+
535597
#[test]
536598
fn test_request_extract() {
537599
let mut router = Router::<()>::build();
@@ -627,6 +689,39 @@ mod tests {
627689
assert!(format!("{:?}", i).contains("unknown variant"));
628690
}
629691

692+
#[test]
693+
fn test_extract_seq() {
694+
let mut router = Router::<()>::build();
695+
router.path("/path/to/{tail:.*}", ());
696+
let router = router.finish();
697+
698+
let mut path = Path::new("/path/to/tail/with/slash%2fes");
699+
assert!(router.recognize(&mut path).is_some());
700+
701+
let i: (String,) = de::Deserialize::deserialize(PathDeserializer::new(&path)).unwrap();
702+
assert_eq!(i.0, String::from("tail/with/slash/es"));
703+
704+
let i: TestSeq1 = de::Deserialize::deserialize(PathDeserializer::new(&path)).unwrap();
705+
assert_eq!(
706+
i.tail,
707+
vec![
708+
String::from("tail"),
709+
String::from("with"),
710+
String::from("slash/es")
711+
]
712+
);
713+
714+
let i: TestSeq2 = de::Deserialize::deserialize(PathDeserializer::new(&path)).unwrap();
715+
assert_eq!(
716+
i.tail,
717+
(
718+
String::from("tail"),
719+
String::from("with"),
720+
String::from("slash/es")
721+
)
722+
);
723+
}
724+
630725
#[test]
631726
fn test_extract_errors() {
632727
let mut router = Router::<()>::build();

actix-web/src/types/path.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ use crate::{
5353
/// format!("Welcome {}!", info.name)
5454
/// }
5555
/// ```
56+
///
57+
/// Segments matching multiple path components can be deserialized
58+
/// into a Vec<_> to percent-decode the components individually. Empty
59+
/// path components are ignored.
60+
///
61+
/// ```
62+
/// use actix_web::{get, web};
63+
/// use serde::Deserialize;
64+
///
65+
/// #[derive(Deserialize)]
66+
/// struct Tail {
67+
/// tail: Vec<String>,
68+
/// }
69+
///
70+
/// // extract `Tail` from a path using serde
71+
/// #[get("/path/to/{tail:.*}")]
72+
/// async fn index(info: web::Path<Tail>) -> String {
73+
/// format!("Navigating to {}!", info.tail.join(" :: "))
74+
/// }
75+
/// ```
5676
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Deref, DerefMut, AsRef, Display, From)]
5777
pub struct Path<T>(T);
5878

0 commit comments

Comments
 (0)