fix(python-fastapi): multipart array files and Form/File wire-name aliases
Merged
JE
4mo ago+68+68+01234567890123456789-11-11-01234567890123456789
Type multipart array-of-binary fields as List[UploadFile] instead of a single UploadFile so FastAPI binds multiple parts with the same field name. Emit alias="{{baseName}}" on Form(...) and File(...) like Query(...) (#17111), keeping snake_case Python parameters while matching OpenAPI wire names (e.g. additionalMetadata, statusArray).
Problem
- Array of binary in
multipart/form-datawas typed as a singleUploadFileafter #23793 (single-file fix only). Clients and specs that send multiple parts with the same name (seeform-multipart-binary-array.yaml) did not match the generated stub. - camelCase form/file fields were renamed to snake_case in Python without a FastAPI alias, while query parameters already used
Query(..., alias=...). PetstoreuploadImagemetadata (additionalMetadata) and mixed multipart fields (statusArray) could not be bound from wire names generated by typical clients (e.g.typescript-fetch).
Changes
PythonFastAPIServerCodegen.uploadFileFormParamTyping: useList[UploadFile]/Optional[List[UploadFile]]whenisArray && isFile.endpoint_argument_definition.mustache: addalias="{{baseName}}"for form params (covers bothFormandFile).PythonFastAPIServerCodegenTest: regressions onform-multipart-binary-array.yamlandpetstore.yaml.- Regenerated
samples/server/petstore/python-fastapiviabin/configs/python-fastapi.yaml.
Tests
./mvnw clean package -DskipTests -Dmaven.javadoc.skip=true -Djacoco.skip=true
./mvnw -pl modules/openapi-generator-core,modules/openapi-generator -am \
-Dtest=PythonFastAPIServerCodegenTest -Dsurefire.failIfNoSpecifiedTests=false test
./bin/generate-samples.sh bin/configs/python-fastapi.yaml
./bin/utils/export_docs_generators.sh
Related
- #23793 — single binary multipart →
UploadFile(does not cover arrays) - #17111 —
Queryalias for camelCase (this PR extends the same pattern toForm/File) - No dedicated upstream issue filed yet for array
List[UploadFile]or multipart Form alias
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
(Full./mvnw clean package -DskipTests -Dmaven.javadoc.skip=true -Djacoco.skip=true ./bin/generate-samples.sh bin/configs/python-fastapi.yaml ./bin/utils/export_docs_generators.sh./bin/generate-samples.sh bin/configs/*.yamlnot run; only the affectedpython-fastapisample was regenerated.)
Commit all changed files (including sample regen). - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
FYI @cbornet @tomplus @krjakbrjak @fa0311 @multani
Summary by cubic
Fixes FastAPI multipart handling by generating List[UploadFile] for array file fields and adding aliases to form/file params to match OpenAPI wire names. FastAPI now binds multiple files correctly and accepts camelCase inputs while keeping snake_case parameters.
-
Bug Fixes
- Multipart array-of-binary form fields now use
List[UploadFile](orOptional[List[UploadFile]]) so repeated parts bind correctly. Form(...)andFile(...)now includealias="{{baseName}}"(parity withQuery(...)) to match wire names likeadditionalMetadataandstatusArray.
- Multipart array-of-binary form fields now use
-
Tests
- Added regression tests for multipart array files and form/file aliasing; updated the FastAPI sample.