Skip to content

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

Description

@hegerdes

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)? - NOTE: The link is points to an unregistered domain
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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:
Image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions