Skip to main content

Swagger2 support for FastAPI framework

Project description

fastapi_swagger2

Swagger2 support for FastAPI

Package version Supported Python versions


Reason behind this library:

Few API GW services like Google Cloud API GW still support only Swagger 2.0 spec. Since FastAPI only supports OAS3, it is a challenge. Converting from OAS3 to Swagger 2.0 requires some manual steps which would hinder CI/CD.


Requirements

Python 3.9+

  • 0.0.3 - FastAPI >= 0.79.0, <= 0.98.0
  • 0.1.1 - FastAPI >= 0.99.0, <= 0.99.1
  • 0.2.4 - FastAPI >= 0.100.0
  • 0.2.7 - FastAPI >= 0.100.0, < 0.119.0 + Pydantic v1/v2
  • 0.3.0 - FastAPI >= 0.119.0, <= 0.123.8 + Pydantic v1/v2
  • 0.3.1 - FastAPI >= 0.123.9, <= 0.127.1 + Pydantic v1/v2 (Since FastAPI 0.126.1, Pydantic v1 is deprecated)
  • 0.3.2 - FastAPI >= 0.128.0, <= 0.128.3
  • 0.3.3 - FastAPI >= 0.128.4, <= 0.128.8 (FastAPI has dropped Python 3.9 support since 0.129.1)

Installation

$ pip install fastapi_swagger2

Development

$ uv sync --extra dev --extra test --extra all

Example

from typing import Union

from fastapi import FastAPI
from fastapi_swagger2 import FastAPISwagger2

app = FastAPI()
FastAPISwagger2(app)


@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
    return {"item_id": item_id, "q": q}

This adds following endpoints:

Generate spec for CI/CD

import os

import yaml

from app.main import app

URL = os.environ["CLOUD_RUN_URL"]

app.servers.append(URL)

spec = app.swagger2()
spec['x-google-backend'] = {'address': URL}

print(yaml.dump(spec))

Development

$ uv sync --extra dev --extra test --extra all

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_swagger2-0.3.3.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_swagger2-0.3.3-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_swagger2-0.3.3.tar.gz.

File metadata

  • Download URL: fastapi_swagger2-0.3.3.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.28

File hashes

Hashes for fastapi_swagger2-0.3.3.tar.gz
Algorithm Hash digest
SHA256 c9f41004f8373afb21727efa305a38e2e122cc9c29ed6f3407f3cf3cf27576f1
MD5 a4ada0984ec08508789b97e43bc6f0c5
BLAKE2b-256 0df6fd1031988d1bf4283486af19dfefea824860415a0860726d533b280f5d79

See more details on using hashes here.

File details

Details for the file fastapi_swagger2-0.3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_swagger2-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 55b43dc263f131d03b42ba3e5493d3d65840fc52582c314e01cb9915ae3ec94f
MD5 d6296f557bfcfa63440b5c25532a15e9
BLAKE2b-256 6e4e7ac754984dae127fbe91b6754205fe3d3ac03e1ed5ae5885189b8d21d6d3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page