Skip to content

[Multipart] Validation sequence #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2023

Conversation

czechboy0
Copy link
Contributor

Motivation

The OpenAPI document provides information about which parts are required, optional, arrays, and single values, so we need to enforce those semantics for the adopter, just like we enforce (using JSONDecoder) that a received JSON payload follows the documented structure.

Modifications

Since the mutlipart body is not a struct, but an async sequence of parts, it's a little more complicated.

We introduce a MultipartValidationSequence with a state machine that keeps track of the requirements and which of them have already been fulfilled over time. And it throws an error if any of the requirements are violated.

For missing required parts, the error is thrown when nil is received from the upstream sequence, indicating that there will be no more parts coming.

To implement this, an internal type ContentDisposition was also introduced for working with that header's values, and helper accessors on MultipartRawPart as well.

Result

Adopters don't have to validate these semantics manually, if they successfully iterate over the parts without an error being thrown, they can be confident that the received (or sent) parts match the requirements from the OpenAPI document.

Test Plan

Unit tests for the sequence, the validator, and the state machine were added. Also added unit tests for the ContentDisposition type.

@czechboy0 czechboy0 merged commit d50b489 into apple:main Nov 20, 2023
@czechboy0 czechboy0 deleted the hd-multipart-validation branch November 20, 2023 11:42
@czechboy0 czechboy0 added the 🔨 semver/patch No public API change. label Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants