…rators#1511)
Fixes openapi-generators#1509
Since openapi-generators#1005 every generated `multipart/form-data` request sets
`Content-Type: multipart/form-data; boundary=+++`. httpx2 builds the
body with that boundary, so a file that contains `\r\n--+++` is split
there and the server gets a truncated file plus a stray part.
This stops setting `Content-Type` for `multipart/form-data` bodies.
httpx2 then adds the header with its own random boundary, the same way
single-body endpoints worked before 0.29.0. For endpoints with several
body types, the multipart branch no longer sets the header either, so
openapi-generators#947 stays fixed: httpx2 still writes a boundary because the header
isn't set. The other content types still set their header explicitly.
Changes:
- `endpoint_module.py.jinja`: skip the `Content-Type` line for multipart
bodies.
- Golden records and the integration-test client regenerated (only the
`boundary=+++` line goes away).
- New functional test `test_multipart_bodies.py`: generates a
single-body and a multi-body endpoint, sends a file containing
`\r\n--+++` through a `MockTransport` and parses the multipart body
back.
- Patch changeset.
Testing:
- `pytest
end_to_end_tests/functional_tests/generated_code_execution/test_multipart_bodies.py`
- before the template change: 2 failed (`{'file': b'first line'} !=
{'file': b'first line\r\n--+++\r\nstill the same file\r\n'}`), 1 passed
(JSON control)
- after: 3 passed
- Full `pdm test` equivalent with coverage: 471 passed, 100% coverage, 5
snapshots passed
- `ruff format --check .`, `ruff check .`, `mypy openapi_python_client
fuzz` pass
I didn't run the `integration` job locally because it needs the
openapi-test-server container.
Actual Behavior
When an endpoint only has one body type, which is multipart/form-data, the Content-Type header is not set, so that HTTPX can set it with the correct boundary.
However, when an endpoint has multiple body types, the Content-Type header is always set, and HTTPX do not override it, which results in a missing boundary for multipart/form-data body types (resulting in a 400 - Bad Request response from the servers).
Proposed Changes
Let's always set the boundary to +++ to avoid any potential problem.
NB: This is kind of related to #1004, since in my specific case, the multipart/form-data body type is the last, it is always the one that is used.