Skip to main content

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.10+

  • 0.4.3 - FastAPI >= 0.140.3, < 0.140.7
  • 0.4.2 - FastAPI >= 0.140.0, < 0.140.3
  • 0.4.1 - FastAPI >= 0.137.2, < 0.140.0
  • 0.4.0 - FastAPI >= 0.137.0, <= 0.137.1
  • 0.3.5 - FastAPI >= 0.129.0, < 0.137.0
  • 0.3.4 - FastAPI >= 0.129.0 (last tested against 0.133.1)
  • 0.3.3 - FastAPI >= 0.128.4, <= 0.128.8 (FastAPI has dropped Python 3.9 support since 0.129.1)
  • 0.3.2 - FastAPI >= 0.128.0, <= 0.128.3
  • 0.3.1 - FastAPI >= 0.123.9, <= 0.127.1 + Pydantic v1/v2 (Since FastAPI 0.126.1, Pydantic v1 is deprecated)
  • 0.3.0 - FastAPI >= 0.119.0, <= 0.123.8 + Pydantic v1/v2
  • 0.2.7 - FastAPI >= 0.100.0, < 0.119.0 + Pydantic v1/v2
  • 0.2.4 - FastAPI >= 0.100.0
  • 0.1.1 - FastAPI >= 0.99.0, <= 0.99.1
  • 0.0.3 - FastAPI >= 0.79.0, <= 0.98.0

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:

  • http://localhost:8000/swagger2.json
  • http://localhost:8000/swagger2/docs
  • http://localhost:8000/swagger2/redoc

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

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.4.3.tar.gz (11.6 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.4.3-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_swagger2-0.4.3.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastapi_swagger2-0.4.3.tar.gz
Algorithm Hash digest
SHA256 28cd0212ea78c6bcbc3f991675a5f6300794ca39d7ad0d7f58cb485c846fce34
MD5 a7aebff937c7a0e16ce317a29d1f5d22
BLAKE2b-256 f9bc99dd06529815f17de4a1ba6837aaf3a063aec3aa3606a518a90df96f26ba

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_swagger2-0.4.3-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastapi_swagger2-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6af5e2a8f175e7f3daa678f103304f40307e9d568808bf4aca50046dfa650672
MD5 4cb2aafb7c39edb75018720d161a3a1d
BLAKE2b-256 da34d6d438a3d3a477dc5af532ff9de17f257999e13b102946a3a9f181677217

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