fix(python-fastapi): multipart array files and Form/File wire-name aliases

Merged
JE
4mo ago
+68-11

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

  1. Array of binary in multipart/form-data was typed as a single UploadFile after #23793 (single-file fix only). Clients and specs that send multiple parts with the same name (see form-multipart-binary-array.yaml) did not match the generated stub.
  2. camelCase form/file fields were renamed to snake_case in Python without a FastAPI alias, while query parameters already used Query(..., alias=...). Petstore uploadImage metadata (additionalMetadata) and mixed multipart fields (statusArray) could not be bound from wire names generated by typical clients (e.g. typescript-fetch).

Changes

  • PythonFastAPIServerCodegen.uploadFileFormParamTyping: use List[UploadFile] / Optional[List[UploadFile]] when isArray && isFile.
  • endpoint_argument_definition.mustache: add alias="{{baseName}}" for form params (covers both Form and File).
  • PythonFastAPIServerCodegenTest: regressions on form-multipart-binary-array.yaml and petstore.yaml.
  • Regenerated samples/server/petstore/python-fastapi via bin/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 — Query alias for camelCase (this PR extends the same pattern to Form/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:
    ./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
    
    (Full ./bin/generate-samples.sh bin/configs/*.yaml not run; only the affected python-fastapi sample 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] (or Optional[List[UploadFile]]) so repeated parts bind correctly.
    • Form(...) and File(...) now include alias="{{baseName}}" (parity with Query(...)) to match wire names like additionalMetadata and statusArray.
  • Tests

    • Added regression tests for multipart array files and form/file aliasing; updated the FastAPI sample.