| 1 |
import json |
| 2 |
|
| 3 |
import click |
| 4 |
from flask import current_app |
| 5 |
from flask.cli import with_appcontext |
| 6 |
|
| 7 |
from .utils import is_openapi3 |
| 8 |
|
| 9 |
|
| 10 |
@click.command() |
| 11 |
@click.option("-f", "--file", type=click.File("w"), default="-") |
| 12 |
@click.option("-e", "--endpoint", default=None) |
| 13 |
@with_appcontext |
| 14 |
def generate_api_schema(file, endpoint): |
| 15 |
"""Generate the swagger schema for your api.""" |
| 16 |
try: |
| 17 |
if endpoint is None: |
| 18 |
endpoint = current_app.swag.config["specs"][0]["endpoint"] |
| 19 |
|
| 20 |
spec = current_app.swag.get_apispecs(endpoint) |
| 21 |
except RuntimeError as e: |
| 22 |
click.echo(e, err=True) |
| 23 |
click.echo( |
| 24 |
"Possible values for endpoint are: {}".format( |
| 25 |
", ".join( |
| 26 |
[ |
| 27 |
spec["endpoint"] |
| 28 |
for spec in current_app.swag.config["specs"] |
| 29 |
if "endpoint" in spec |
| 30 |
] |
| 31 |
) |
| 32 |
), |
| 33 |
err=True, |
| 34 |
) |
| 35 |
raise click.Abort |
| 36 |
|
| 37 |
|
| 38 |
if is_openapi3(spec.get("openapi")): |
| 39 |
if "definitions" in spec: |
| 40 |
del spec["definitions"] |
| 41 |
|
| 42 |
json.dump(spec, file, indent=4) |
| 43 |
|
| 44 |
return spec |