Skip to content

How to use swagger 2 in FastAPI #7321

Answered by ArcLightSlavik
somta asked this question in Questions
Discussion options

You must be logged in to vote

First i make the app fill the api data and create an endpoint which just returns a json of the schema

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

app = FastAPI()

openapi_schema = get_openapi(
    version='1.0',
    title='Title',
    description='Api',
    openapi_prefix='',
    routes=app.routes,
)

app.openapi_schema = openapi_schema

@app.get('/openapi')
async def openapi_export():
    openapi = app.openapi_schema
    return jsonable_encoder(openapi)

Then i have a function that goes to that url takes the schema and writes to a yaml file (json is also possible i believe)

def build():
    import requests

    …

Replies: 6 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Kludex
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
4 participants
Converted from issue

This discussion was converted from issue #1665 on February 28, 2023 01:09.