fix(python-fastapi): map binary response type file to bytes

Merged
JE
3mo ago
+89-0

Description

Fixes invalid -> file / "model": file for binary (application/octet-stream + format: binary) response bodies in python-fastapi (#20775).

Python 3 has no built-in file type, so generated stubs raised NameError on import and broke IDE typing. This change maps OAS file / binary to built-in bytes in PythonFastAPIServerCodegen only (minimal scope). Multipart upload fields still use FastAPI UploadFile via the existing form-param override (#23793).

What changed

  • typeMapping.put("file", "bytes") and typeMapping.put("binary", "bytes") in PythonFastAPIServerCodegen
  • Regression sample: modules/openapi-generator/src/test/resources/3_0/issue_20775.yaml (200/206 + optional Range)
  • Tests: PythonFastapiCodegenTest / PythonFastAPIServerCodegenTest assert -> bytes / "model": bytes and absence of bare file

Verification

./mvnw clean package -DskipTests
./bin/generate-samples.sh ./bin/configs/python*.yaml
./bin/utils/export_docs_generators.sh
mvn -pl modules/openapi-generator -am \
  -Dtest=PythonFastapiCodegenTest,PythonFastAPIServerCodegenTest \
  -Dsurefire.failIfNoSpecifiedTests=false test

No samples/ diff: current python-fastapi petstore has no binary response endpoints (only multipart upload). Coverage is via issue_20775.yaml unit generation.

Related: #20775, #4372

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/python*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • @krjakbrjak (Python FastAPI)

Summary by cubic

Maps binary response bodies to bytes in python-fastapi server generation to replace the invalid Python file type. This prevents import errors and fixes typing for application/octet-stream responses.

  • Bug Fixes
    • Map OAS file and binary to bytes in PythonFastAPIServerCodegen.
    • Multipart uploads remain UploadFile (unchanged).
    • Add regression spec issue_20775.yaml and tests asserting -> bytes and no file.
    • Fixes #20775; related to #4372.