Skip to content

Commit b99b5d4

Browse files
authored
Implemented the SwaggerUI urls config parameter which displays a dropdown of API specs instead of an editable textbox (flasgger#473)
1 parent 1695c0f commit b99b5d4

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""
2+
In this example a spec name is provided in order to trigger SwaggeUI's dropdown list of specs.
3+
"""
4+
from flask import Flask, jsonify
5+
try:
6+
from http import HTTPStatus
7+
except ImportError:
8+
import httplib as HTTPStatus
9+
from flasgger import Swagger
10+
from flasgger.utils import swag_from
11+
12+
13+
swagger_config = {
14+
"headers": [
15+
],
16+
"specs": [
17+
{
18+
"version": "0.0.1",
19+
"title": "Api v1",
20+
"name": "v1",
21+
"endpoint": 'v1_spec',
22+
"description": 'This is the version 1 of our API',
23+
"route": '/v1/spec',
24+
"rule_filter": lambda rule: rule.rule.startswith('/v1/'),
25+
},
26+
{
27+
"version": "0.0.2",
28+
"title": "Api v2",
29+
"name": "v2",
30+
"description": 'This is the version 2 of our API',
31+
"endpoint": 'v2_spec',
32+
"route": '/v2/spec',
33+
"rule_filter": lambda rule: rule.rule.startswith('/v2/'),
34+
}
35+
],
36+
"static_url_path": "/flasgger_static",
37+
}
38+
39+
app = Flask(__name__)
40+
swag = Swagger(app, config=swagger_config)
41+
42+
43+
44+
@app.route('/v1/hello')
45+
def v1_hello():
46+
"""
47+
A test view
48+
49+
---
50+
responses:
51+
200:
52+
description: OK
53+
"""
54+
return jsonify(hello="world")
55+
56+
57+
@app.route('/v2/hello')
58+
def v2_hello():
59+
"""
60+
A test view v2
61+
62+
---
63+
responses:
64+
200:
65+
description: OK
66+
"""
67+
return jsonify(hello="world")
68+
69+
70+
def test_swag(client, specs_data):
71+
"""
72+
This test is runs automatically in Travis CI
73+
74+
:param client: Flask app test client
75+
:param specs_data: {'url': {swag_specs}} for every spec in app
76+
"""
77+
assert client.get('/apidocs/').status_code == HTTPStatus.OK
78+
79+
80+
if __name__ == '__main__':
81+
app.run(debug=True)

flasgger/base.py

+9
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,22 @@ def get(self):
7878
{
7979
"url": url_for(".".join((base_endpoint, spec['endpoint']))),
8080
"title": spec.get('title', 'API Spec 1'),
81+
"name": spec.get('name', None),
8182
"version": spec.get("version", '0.0.1'),
8283
"endpoint": spec.get('endpoint')
8384
}
8485
for spec in self.config.get('specs', [])
8586
]
87+
urls = [
88+
{
89+
"name": spec["name"],
90+
"url": spec["url"]
91+
}
92+
for spec in specs if spec["name"]
93+
]
8694
data = {
8795
"specs": specs,
96+
"urls": urls,
8897
"title": self.config.get('title', 'Flasgger')
8998
}
9099
if request.args.get('json'):

flasgger/ui3/templates/flasgger/swagger.html

+4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
Object.assign(
1212
{
1313

14+
{% if urls %}
15+
urls: {{ urls | tojson }},
16+
{% else %}
1417
url: "{{ specs[0]['url'] }}",
18+
{% endif %}
1519
dom_id: '#swagger-ui',
1620
validatorUrl: null,
1721
displayOperationId: true,

0 commit comments

Comments
 (0)