Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/controllers/openapi/_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _place(body: dict[str, Any], name: str, parts: list[FileStorage], *, file_fi
value: FileStorage | list[FileStorage] = parts if many else parts[0]
target = body if key is None else body.setdefault(field, {})
leaf = field if key is None else key
if leaf in target:
if not isinstance(target, dict) or leaf in target:
raise InvalidFilePart(f"{name}: given more than once")
target[leaf] = value

Expand Down
11 changes: 11 additions & 0 deletions api/tests/unit_tests/controllers/openapi/test__multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,14 @@ def test_multipart_places_every_part_name_shape(app: Flask) -> None:
def test_multipart_rejects_malformed_bodies(app: Flask, data: Mapping[str, object], reason: str) -> None:
with _multipart(app, data), pytest.raises(InvalidFilePart, match=reason):
body_from_request(file_fields=_FILE_FIELDS)


@pytest.mark.parametrize("direct", ["files", "files[]"])
@pytest.mark.parametrize("mapped", ["files[doc]", "files[doc][]"])
@pytest.mark.parametrize("mapped_first", [False, True], ids=["direct_first", "mapped_first"])
def test_multipart_rejects_conflicting_file_shapes(app: Flask, direct: str, mapped: str, mapped_first: bool) -> None:
names = [mapped, direct] if mapped_first else [direct, mapped]
data = {name: _part() for name in names}
with _multipart(app, data), pytest.raises(InvalidFilePart, match="given more than once") as exc:
body_from_request(file_fields=_FILE_FIELDS)
assert exc.value.code == 422