Skip to content

Conversation

@fasilmveloor
Copy link

@fasilmveloor fasilmveloor commented Feb 12, 2025

PR Type

Feature

PR Checklist

  • Tests added or updated
  • Documentation updated
  • Changelog entry added
  • Ensure CI builds and tests pass
  • Rebased on top of the latest master branch
  • Code formatted with rustfmt
  • No breaking changes introduced

Overview

This PR adds support for using Option<Vec<T>> with the MultipartForm extractor in actix-multipart. It enables multipart fields that may be completely absent or may appear multiple times, while preserving the distinction between missing fields and empty collections.

Behavior

  • Field absent: None
  • Field present one or more times: Some(Vec<T>), where each occurrence is parsed via T: FieldReader<'t>

This allows optional repeated multipart fields to be represented ergonomically in MultipartForm-derived structs.

Example

#[derive(MultipartForm)]
struct MyForm {
    tags: Option<Vec<String>>,
}
  • No tags parts → tags == None
  • Multiple tags parts → tags == Some(vec![ ... ])

Motivation

Issue #3576 identified a gap in multipart extraction:

  • Vec<T> could collect multiple values but did not distinguish absence from an empty list.
  • Option<T> expressed absence but allowed only a single value.

Supporting Option<Vec<T>> resolves this by allowing both optionality and repetition.

Implementation Details

  • Adds an implementation of FieldGroupReader<'t> for Option<Vec<T>> in
    actix-multipart/src/form/mod.rs.

  • handle_field collects all occurrences of the field into a vector stored in extractor state.

  • from_state returns:

    • Some(Vec<T>) if values were collected,
    • None if the field never appeared.
  • The change follows existing internal patterns and maintains full backward compatibility.

Changelog

To be added under the Unreleased section of actix-multipart/CHANGES.md:

* Added support for `Option<Vec<T>>` in MultipartForm, enabling optional repeated multipart fields.

Semver

This is a backward-compatible enhancement and should be released as a semver-minor update.

Related Issues

@robjtede robjtede added B-semver-minor A-multipart project: actix-multipart labels May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-multipart project: actix-multipart B-semver-minor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

actix_multipart::form::FieldGroupReader is not Implemented For Option<Vec<T>>>

2 participants