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)

Installation

$ pip install fastapi_swagger2

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

$ pip install "/path/to/fastapi_swagger2/repo[test,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.3.1.tar.gz (33.7 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.1-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_swagger2-0.3.1.tar.gz
  • Upload date:
  • Size: 33.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for fastapi_swagger2-0.3.1.tar.gz
Algorithm Hash digest
SHA256 d82dd8668166a53ecc282140b3a4e0f85218c797ed602fc02ac51a82f57f376e
MD5 54198adcc4ddae291c1c802836a12080
BLAKE2b-256 75a72d3362eae5c2e3f5b1aaeb7ac1ae64667f12f928edd33f434f5a03446a5f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_swagger2-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fcf832d93de8f047d3a2b4153b70b123f83b243abace616f8bf0ab82b0203bd0
MD5 23484b1b60103db158d0edb6e88a17f7
BLAKE2b-256 570dcd17a74333edd18d260f455add8798878e3c9e9d67657c037ad92ec366b0

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