Generate openapi schema without running server #7445
Replies: 5 comments
-
Duplicate of #1173. |
Beta Was this translation helpful? Give feedback.
-
@phy25, thanks for the link, it was exactly what I needed. I'll close out. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the help here @phy25 ! 👏 🙇 Thanks for reporting back and closing the issue @BenDavidAaron 👍 |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
Here's a Typer alternative that does that import json
import sys
from typing import Union
import typer
import yaml
from loguru import logger
from uvicorn.importer import import_from_string
def do(
app_main: str = "main:app",
out: str = "openapi.json",
app_dir: Union[str, None] = None,
):
if app_dir is not None:
logger.debug(f"adding {app_dir} to sys.path")
sys.path.insert(0, app_dir)
logger.info(f"importing app from {app_main}")
app = import_from_string(app_main)
openapi = app.openapi()
version = openapi.get("openapi", "unknown version")
logger.debug(f"writing openapi spec v{version}")
with open(out, "w") as f:
if out.endswith(".json"):
json.dump(openapi, f, indent=2)
else:
yaml.dump(openapi, f, sort_keys=False)
logger.info(f"spec written to {out}")
def main():
typer.run(do) |
Beta Was this translation helpful? Give feedback.
-
Is your feature request related to a problem
Not a bug, just a problem with my CI system.
I want to be able to programmatically access the openapi schema and save it to a file without running the web server
The solution you would like
Ideally, I'd like to write a script that would output the openapi schema without running the webserver.
Describe alternatives you've considered
I've considered running the app in my CICD workflow and requesting the schema via http, but that seems like a lot of overhead, and I'd bet the functions to do what I want are already in the code and just need to be invoked in the right order.
Additional context
Nothing to add, other than I'm gonna open a PR and see if I can figure it out.
Beta Was this translation helpful? Give feedback.
All reactions