feat: implement FieldGroupReader for Option<Vec<T>> in multipart form… (#3577)

* feat: implement FieldGroupReader for Option<Vec<T>> in multipart form handling

* add tests

---------

Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
This commit is contained in:
fasilmveloor 2026-01-29 18:30:23 +05:30 committed by GitHub
parent 2737e88973
commit aa8df45fce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 76 additions and 0 deletions

View File

@ -2,8 +2,11 @@
## Unreleased ## Unreleased
- Add `MultipartForm` support for `Option<Vec<T>>` fields. [#3577]
- Minimum supported Rust version (MSRV) is now 1.88. - Minimum supported Rust version (MSRV) is now 1.88.
[#3577]: https://github.com/actix/actix-web/pull/3577
## 0.7.2 ## 0.7.2
- Fix re-exported version of `actix-multipart-derive`. - Fix re-exported version of `actix-multipart-derive`.

View File

@ -187,6 +187,45 @@ where
} }
} }
impl<'t, T> FieldGroupReader<'t> for Option<Vec<T>>
where
T: FieldReader<'t>,
{
type Future = LocalBoxFuture<'t, Result<(), MultipartError>>;
fn handle_field(
req: &'t HttpRequest,
field: Field,
limits: &'t mut Limits,
state: &'t mut State,
_duplicate_field: DuplicateField,
) -> Self::Future {
let field_name = field.name().unwrap().to_string();
Box::pin(async move {
let vec = state
.entry(field_name)
.or_insert_with(|| Box::<Vec<T>>::default())
.downcast_mut::<Vec<T>>()
.unwrap();
let item = T::read_field(req, field, limits).await?;
vec.push(item);
Ok(())
})
}
fn from_state(name: &str, state: &'t mut State) -> Result<Self, MultipartError> {
if let Some(boxed_vec) = state.remove(name) {
let vec = *boxed_vec.downcast::<Vec<T>>().unwrap();
Ok(Some(vec))
} else {
Ok(None)
}
}
}
/// Trait that allows a type to be used in the [`struct@MultipartForm`] extractor. /// Trait that allows a type to be used in the [`struct@MultipartForm`] extractor.
/// ///
/// You should use the [`macro@MultipartForm`] macro to derive this for your struct. /// You should use the [`macro@MultipartForm`] macro to derive this for your struct.
@ -506,6 +545,40 @@ mod tests {
assert_eq!(response.status(), StatusCode::OK); assert_eq!(response.status(), StatusCode::OK);
} }
/// Test `Option<Vec>` fields.
#[derive(MultipartForm)]
struct TestOptionVec {
list1: Option<Vec<Text<String>>>,
list2: Option<Vec<Text<String>>>,
}
async fn test_option_vec_route(form: MultipartForm<TestOptionVec>) -> impl Responder {
let form = form.into_inner();
let strings = form
.list1
.unwrap()
.into_iter()
.map(|s| s.into_inner())
.collect::<Vec<_>>();
assert_eq!(strings, vec!["value1", "value2", "value3"]);
assert!(form.list2.is_none());
HttpResponse::Ok().finish()
}
#[actix_rt::test]
async fn test_option_vec() {
let srv =
actix_test::start(|| App::new().route("/", web::post().to(test_option_vec_route)));
let mut form = multipart::Form::default();
form.add_text("list1", "value1");
form.add_text("list1", "value2");
form.add_text("list1", "value3");
let response = send_form(&srv, form, "/").await;
assert_eq!(response.status(), StatusCode::OK);
}
/// Test the `rename` field attribute. /// Test the `rename` field attribute.
#[derive(MultipartForm)] #[derive(MultipartForm)]
struct TestFieldRenaming { struct TestFieldRenaming {