Skip to content

Commit bcf0d8c

Browse files
committed
Merge branch 'add_commands' into 2.x
# Conflicts: # flask_openapi3/openapi.py # setup.py
2 parents 8f1869c + d7bb70e commit bcf0d8c

File tree

5 files changed

+82
-1
lines changed

5 files changed

+82
-1
lines changed

docs/OpenAPI/Specification.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,47 @@
1+
## Specification
2+
13
If you need the complete Specification(json) , go to http://127.0.0.1:5000/openapi/openapi.json
24

5+
**command: `flask openapi`**
6+
7+
*New in v2.0.0*
8+
9+
The `flask openapi` command will export the OpenAPI Specification to console when you execute the command:
10+
11+
```
12+
flask openapi
13+
```
14+
15+
Execute `flask openapi --help` for more information:
16+
17+
```
18+
flask openapi --help
19+
20+
Usage: flask openapi [OPTIONS]
21+
22+
Export the OpenAPI Specification to console or a file
23+
24+
Options:
25+
-o, --output PATH The output file path.
26+
-f, --format [json|yaml|markdown]
27+
The output file format.
28+
-i, --indent INTEGER The indentation for JSON dumps.
29+
-a, --ensure_ascii Ensure ASCII characters or not. Defaults to
30+
False.
31+
--help Show this message and exit.
32+
33+
```
34+
35+
!!! info
36+
37+
You need to manually install `yaml` using pip:
38+
```bash
39+
pip install flask-openapi3[yaml]
40+
41+
# or
42+
pip install yaml
43+
```
44+
345
## doc_ui
446

547
You can pass `doc_ui=False` to disable the `OpenAPI spec` when init `OpenAPI `.

flask_openapi3/commands.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
# @Author : llc
3+
# @Time : 2022/4/16 14:21
4+
import json
5+
6+
from flask import current_app
7+
from flask.cli import click
8+
from flask.cli import with_appcontext
9+
10+
from .markdown import openapi_to_markdown
11+
12+
13+
@click.command(name='openapi')
14+
@click.option('--output', '-o', type=click.Path(), help='The output file path.')
15+
@click.option('--format', '-f', type=click.Choice(['json', 'yaml', 'markdown']), help='The output file format.')
16+
@click.option('--indent', '-i', type=int, help='The indentation for JSON dumps.')
17+
@click.option('--ensure_ascii', '-a', is_flag=True, help='Ensure ASCII characters or not. Defaults to False.')
18+
@with_appcontext
19+
def openapi_command(output, format, indent, ensure_ascii):
20+
"""Export the OpenAPI Specification to console or a file"""
21+
if hasattr(current_app, 'api_doc'):
22+
obj = current_app.api_doc
23+
if format == 'yaml':
24+
import yaml
25+
openapi = yaml.safe_dump(obj)
26+
elif format == 'markdown':
27+
openapi = openapi_to_markdown(obj)
28+
else:
29+
openapi = json.dumps(obj, indent=indent, ensure_ascii=ensure_ascii)
30+
if output:
31+
with open(output, 'w', encoding='utf8') as f:
32+
f.write(openapi)
33+
click.echo(f"Saved to {output}.")
34+
else:
35+
click.echo(openapi)

flask_openapi3/markdown.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
# @Author : llc
33
# @Time : 2021/8/11 10:39
4-
from flask_openapi3.models import OPENAPI3_REF_PREFIX
4+
from .models import OPENAPI3_REF_PREFIX
55

66

77
def parse_schemas(schemas):

flask_openapi3/openapi.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pydantic import BaseModel
1414

1515
from .api_blueprint import APIBlueprint
16+
from .commands import openapi_command
1617
from .do_wrapper import _do_wrapper
1718
from .http import HTTPMethod
1819
from .markdown import openapi_to_markdown
@@ -98,6 +99,8 @@ def __init__(
9899
self.doc_expansion = doc_expansion
99100
self.severs = servers
100101
self.external_docs = external_docs
102+
# add openapi command
103+
self.cli.add_command(openapi_command)
101104

102105
def init_doc(self) -> None:
103106
"""

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
zip_safe=False,
3030
platforms='any',
3131
install_requires=["Flask>=2.0", "pydantic>=1.2"],
32+
extras_require={'yaml': ['pyyaml']},
3233
classifiers=[
3334
# 'Development Status :: 1 - Planning',
3435
# 'Development Status :: 2 - Pre-Alpha',

0 commit comments

Comments
 (0)