Skip to content

โ†” ๐Ÿ—„

Warning

๐Ÿ‘‰ ๐Ÿ‘ ๐Ÿง โš’. ๐Ÿ‘† ๐ŸŽฒ ๐Ÿ’ช ๐Ÿšถ โšซ๏ธ.

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ“„ ๐Ÿ”ฐ - ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿฆฎ, ๐Ÿ‘† ๐Ÿ’ช ๐ŸŽฒ ๐Ÿšถ ๐Ÿ‘‰ ๐Ÿ“„.

๐Ÿšฅ ๐Ÿ‘† โช ๐Ÿ’ญ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”€ ๐Ÿ— ๐Ÿ—„ ๐Ÿ”—, ๐Ÿ˜ฃ ๐Ÿ‘‚.

๐Ÿ“ค ๐Ÿ’ผ ๐ŸŒโ” ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’ช ๐Ÿ”€ ๐Ÿ— ๐Ÿ—„ ๐Ÿ”—.

๐Ÿ‘‰ ๐Ÿ“„ ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ โ”.

๐Ÿ˜ ๐Ÿ› ๏ธ

๐Ÿ˜ (๐Ÿ”ข) ๐Ÿ› ๏ธ, โฉ.

FastAPI ๐Ÿˆธ (๐Ÿ‘) โœ”๏ธ .openapi() ๐Ÿ‘ฉโ€๐Ÿ”ฌ ๐Ÿ‘ˆ ๐Ÿ“ˆ ๐Ÿ“จ ๐Ÿ—„ ๐Ÿ”—.

๐Ÿ• ๐Ÿˆธ ๐ŸŽš ๐Ÿ—, โžก ๐Ÿ› ๏ธ /openapi.json (โš–๏ธ โšซ๏ธโ” ๐Ÿ‘† โš’ ๐Ÿ‘† openapi_url) ยฎ.

โšซ๏ธ ๐Ÿ“จ ๐ŸŽป ๐Ÿ“จ โฎ๏ธ ๐Ÿ ๐Ÿˆธ .openapi() ๐Ÿ‘ฉโ€๐Ÿ”ฌ.

๐Ÿ”ข, โšซ๏ธโ” ๐Ÿ‘ฉโ€๐Ÿ”ฌ .openapi() ๐Ÿ”จ โœ… ๐Ÿ  .openapi_schema ๐Ÿ‘€ ๐Ÿšฅ โšซ๏ธ โœ”๏ธ ๐ŸŽš & ๐Ÿ“จ ๐Ÿ‘ซ.

๐Ÿšฅ โšซ๏ธ ๐Ÿšซ, โšซ๏ธ ๐Ÿ— ๐Ÿ‘ซ โš™๏ธ ๐Ÿš™ ๐Ÿ”ข fastapi.openapi.utils.get_openapi.

& ๐Ÿ‘ˆ ๐Ÿ”ข get_openapi() ๐Ÿ“จ ๐Ÿ”ข:

  • title: ๐Ÿ—„ ๐Ÿ“›, ๐ŸŽฆ ๐Ÿฉบ.
  • version: โฌ ๐Ÿ‘† ๐Ÿ› ๏ธ, โœ… 2.5.0.
  • openapi_version: โฌ ๐Ÿ—„ ๐Ÿ”ง โš™๏ธ. ๐Ÿ”ข, โช: 3.0.2.
  • description: ๐Ÿ“› ๐Ÿ‘† ๐Ÿ› ๏ธ.
  • routes: ๐Ÿ“‡ ๐Ÿ›ฃ, ๐Ÿ‘ซ ๐Ÿ”  ยฎ โžก ๐Ÿ› ๏ธ. ๐Ÿ‘ซ โœŠ โšช๏ธโžก๏ธ app.routes.

๐Ÿ”‘ ๐Ÿ”ข

โš™๏ธ โ„น ๐Ÿ”›, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐ŸŽ ๐Ÿš™ ๐Ÿ”ข ๐Ÿ— ๐Ÿ—„ ๐Ÿ”— & ๐Ÿ” ๐Ÿ”  ๐Ÿ• ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’ช.

๐Ÿ–ผ, โžก๏ธ ๐Ÿšฎ ๐Ÿ“„ ๐Ÿ—„ โ†” ๐Ÿ”Œ ๐Ÿ›ƒ ๐Ÿ”ฑ.

๐Ÿ˜ FastAPI

๐Ÿฅ‡, โœ ๐ŸŒ ๐Ÿ‘† FastAPI ๐Ÿˆธ ๐Ÿ›Ž:

from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi

app = FastAPI()


@app.get("/items/")
async def read_items():
    return [{"name": "Foo"}]


def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema
    openapi_schema = get_openapi(
        title="Custom title",
        version="2.5.0",
        summary="This is a very custom OpenAPI schema",
        description="Here's a longer description of the custom **OpenAPI** schema",
        routes=app.routes,
    )
    openapi_schema["info"]["x-logo"] = {
        "url": "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"
    }
    app.openapi_schema = openapi_schema
    return app.openapi_schema


app.openapi = custom_openapi

๐Ÿ— ๐Ÿ—„ ๐Ÿ”—

โคด๏ธ, โš™๏ธ ๐ŸŽ ๐Ÿš™ ๐Ÿ”ข ๐Ÿ— ๐Ÿ—„ ๐Ÿ”—, ๐Ÿ”˜ custom_openapi() ๐Ÿ”ข:

from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi

app = FastAPI()


@app.get("/items/")
async def read_items():
    return [{"name": "Foo"}]


def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema
    openapi_schema = get_openapi(
        title="Custom title",
        version="2.5.0",
        summary="This is a very custom OpenAPI schema",
        description="Here's a longer description of the custom **OpenAPI** schema",
        routes=app.routes,
    )
    openapi_schema["info"]["x-logo"] = {
        "url": "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"
    }
    app.openapi_schema = openapi_schema
    return app.openapi_schema


app.openapi = custom_openapi

๐Ÿ”€ ๐Ÿ—„ ๐Ÿ”—

๐Ÿ”œ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ ๐Ÿ“„ โ†”, โŽ ๐Ÿ›ƒ x-logo info "๐ŸŽš" ๐Ÿ—„ ๐Ÿ”—:

from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi

app = FastAPI()


@app.get("/items/")
async def read_items():
    return [{"name": "Foo"}]


def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema
    openapi_schema = get_openapi(
        title="Custom title",
        version="2.5.0",
        summary="This is a very custom OpenAPI schema",
        description="Here's a longer description of the custom **OpenAPI** schema",
        routes=app.routes,
    )
    openapi_schema["info"]["x-logo"] = {
        "url": "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"
    }
    app.openapi_schema = openapi_schema
    return app.openapi_schema


app.openapi = custom_openapi

๐Ÿ’พ ๐Ÿ—„ ๐Ÿ”—

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐Ÿ  .openapi_schema "๐Ÿ’พ", ๐Ÿช ๐Ÿ‘† ๐Ÿ— ๐Ÿ”—.

๐Ÿ‘ˆ ๐ŸŒŒ, ๐Ÿ‘† ๐Ÿˆธ ๐Ÿ† ๐Ÿšซ โœ”๏ธ ๐Ÿ— ๐Ÿ”— ๐Ÿ”  ๐Ÿ•ฐ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ“‚ ๐Ÿ‘† ๐Ÿ› ๏ธ ๐Ÿฉบ.

โšซ๏ธ ๐Ÿ”œ ๐Ÿ— ๐Ÿ•ด ๐Ÿ•, & โคด๏ธ ๐ŸŽ ๐Ÿ’พ ๐Ÿ”— ๐Ÿ”œ โš™๏ธ โญ ๐Ÿ“จ.

from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi

app = FastAPI()


@app.get("/items/")
async def read_items():
    return [{"name": "Foo"}]


def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema
    openapi_schema = get_openapi(
        title="Custom title",
        version="2.5.0",
        summary="This is a very custom OpenAPI schema",
        description="Here's a longer description of the custom **OpenAPI** schema",
        routes=app.routes,
    )
    openapi_schema["info"]["x-logo"] = {
        "url": "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"
    }
    app.openapi_schema = openapi_schema
    return app.openapi_schema


app.openapi = custom_openapi

๐Ÿ” ๐Ÿ‘ฉโ€๐Ÿ”ฌ

๐Ÿ”œ ๐Ÿ‘† ๐Ÿ’ช โŽ .openapi() ๐Ÿ‘ฉโ€๐Ÿ”ฌ โฎ๏ธ ๐Ÿ‘† ๐Ÿ†• ๐Ÿ”ข.

from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi

app = FastAPI()


@app.get("/items/")
async def read_items():
    return [{"name": "Foo"}]


def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema
    openapi_schema = get_openapi(
        title="Custom title",
        version="2.5.0",
        summary="This is a very custom OpenAPI schema",
        description="Here's a longer description of the custom **OpenAPI** schema",
        routes=app.routes,
    )
    openapi_schema["info"]["x-logo"] = {
        "url": "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"
    }
    app.openapi_schema = openapi_schema
    return app.openapi_schema


app.openapi = custom_openapi

โœ… โšซ๏ธ

๐Ÿ• ๐Ÿ‘† ๐Ÿšถ http://127.0.0.1:8000/redoc ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿ‘ˆ ๐Ÿ‘† โš™๏ธ ๐Ÿ‘† ๐Ÿ›ƒ ๐Ÿ”ฑ (๐Ÿ‘‰ ๐Ÿ–ผ, FastAPI'โ“‚ ๐Ÿ”ฑ):