Skip to content

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

Open
JerrySLau wants to merge 1 commit into
OpenAPITools:masterfrom
JerrySLau:fix-python-binary-response
Open

fix(python-fastapi): map binary response type file to bytes#24285
JerrySLau wants to merge 1 commit into
OpenAPITools:masterfrom
JerrySLau:fix-python-binary-response

Conversation

@JerrySLau

@JerrySLau JerrySLau commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

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.

Written for commit 24dd78a. Summary will update on new commits.

Review in cubic

Fixes invalid `-> file` / `"model": file` for octet-stream responses (OpenAPITools#20775).

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

Re-trigger cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [python] [server] [fastapi] Generates invalid typings for binary responses

1 participant