Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use swagger 2 in FastAPI #1665

Closed
somta opened this issue Jul 2, 2020 · 5 comments
Closed

How to use swagger 2 in FastAPI #1665

somta opened this issue Jul 2, 2020 · 5 comments

Comments

@somta
Copy link

somta commented Jul 2, 2020

We need to use swagger2 in the FastAPI project, can we use Swagger 2 to replace the original OpenAPI

@somta somta added the question Question or problem label Jul 2, 2020
@ArcLightSlavik
Copy link
Contributor

ArcLightSlavik commented Jul 2, 2020

I've had the same issue and i fixed it by using https://github.com/LucyBot-Inc/api-spec-converter
You can make an endpoint which gets swagger3 and writes to file after which you run the spec converter cli

I did have to do some manual changes after that (some security stuff and empty body that gcp doesn't like) but that will be different for everyone

I can give a more concrete example later if you wish

@somta
Copy link
Author

somta commented Jul 6, 2020

Can you provide an example

@ArcLightSlavik
Copy link
Contributor

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

    spec = requests.get('http://WHATEVER_URL_U_HAVE/openapi').json()
    dest = ROOT / 'openapi_3.yaml'
    dest.write_yaml(spec)

Then i call the spec-converter as a CLI (requires a node env with npm install api-spec-converter)

def convert():
    subprocess.check_call('api-spec-converter --from=openapi_3 --to=swagger_2 --syntax=yaml /path/to/openapi_3.yaml > /path/to/openapi.yaml', shell=True)
# yes i know that shell=True is not a good practice

After which you should have a version 2 swagger in the openapi.yaml :)
The conversion while good wasn't completely perfect for me
For that i do a manual update by just loading the yaml file and changing whatever fields i want

def manual_update():
    openapi_yaml = yaml.load(open(str(ROOT.joinpath(openapi_location))))
    openapi_yaml['host'] = 'some_very_nice_fake_host'

@tiangolo
Copy link
Owner

tiangolo commented Dec 6, 2020

Thanks for the help here @ArcLightSlavik ! 👏 🙇

If that solves the original problem, then you can close this issue @somta ✔️

@github-actions
Copy link
Contributor

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7321 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

3 participants