Bug Report Checklist
Description
When you use the python sever generation for fastapi and declare a binary output in your OpenAPI-Spec, the generator produces invalid type hints. There is no file type in python 3 AFAIK, and if there is the import is missing.
openapi-generator version
openapi-generator-cli 7.13.0-SNAPSHOT
commit : 5cef080
built : -999999999-01-01T00:00:00+18:00
source : https://github.com/openapitools/openapi-generator
docs : https://openapi-generator.tech/
OpenAPI declaration file content or url
openapi: 3.1.3
info:
title: File Demo - OpenAPI 3.0
version: 0.0.1
servers:
- url: http://localhost:8080
paths:
/coverletter/{asset_id}:
get:
summary: Download a pdf
operationId: download_pdf
parameters:
- name: asset_id
in: path
description: ID of asset to get
required: true
schema:
type: string
responses:
"200":
description: PDF binary
content:
application/pdf:
schema:
type: string
format: binary
"401":
description: Authorization information is missing or invalid.
"404":
description: A document with the specified ID was not found.
Generation Details
The code was generated with:
docker run --rm -it --user $(id -u $USER):$(id -g $USER) \
-v ${PWD}:/local openapitools/openapi-generator-cli generate \
-i /local/file-demo.yaml \
-g python-fastapi \
-o /local/demo/
Steps to reproduce
docker run --rm -it --user $(id -u $USER):$(id -g $USER) \
-v ${PWD}:/local openapitools/openapi-generator-cli generate \
-i /local/file-demo.yaml \
-g python-fastapi \
-o /local/demo/
cd demo
pip install -r requirements.txt
PYTHONPATH=src uvicorn openapi_server.main:app --host 0.0.0.0 --port 8080
Related issues/PRs
None
Suggest a fix
The python types are declared here:
|
languageSpecificPrimitives.add("file"); |
|
languageSpecificPrimitives.add("bytes"); |
But this might not be the right type for file. There is no file type in python (AFAIK), only a File type that is part of FastAPI, see or a FileResponse see
If the File type from FastApi is meant here, the first letter must be upper case and it is missing the import in
|
from fastapi import ( # noqa: F401 |
|
APIRouter, |
|
Body, |
|
Cookie, |
|
Depends, |
|
Form, |
Screenshot of the error:

Bug Report Checklist
Description
When you use the python sever generation for fastapi and declare a binary output in your OpenAPI-Spec, the generator produces invalid type hints. There is no
filetype in python 3 AFAIK, and if there is the import is missing.openapi-generator version
OpenAPI declaration file content or url
Generation Details
The code was generated with:
Steps to reproduce
Related issues/PRs
None
Suggest a fix
The python types are declared here:
openapi-generator/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonPydanticV1Codegen.java
Lines 100 to 101 in 70df117
But this might not be the right type for
file. There is nofiletype in python (AFAIK), only aFiletype that is part of FastAPI, see or aFileResponseseeIf the
Filetype from FastApi is meant here, the first letter must be upper case and it is missing the import inopenapi-generator/modules/openapi-generator/src/main/resources/python-fastapi/api.mustache
Lines 10 to 15 in 70df117
Screenshot of the error:
