File tree 3 files changed +94
-0
lines changed
3 files changed +94
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change @@ -78,13 +78,22 @@ def get(self):
78
78
{
79
79
"url" : url_for ("." .join ((base_endpoint , spec ['endpoint' ]))),
80
80
"title" : spec .get ('title' , 'API Spec 1' ),
81
+ "name" : spec .get ('name' , None ),
81
82
"version" : spec .get ("version" , '0.0.1' ),
82
83
"endpoint" : spec .get ('endpoint' )
83
84
}
84
85
for spec in self .config .get ('specs' , [])
85
86
]
87
+ urls = [
88
+ {
89
+ "name" : spec ["name" ],
90
+ "url" : spec ["url" ]
91
+ }
92
+ for spec in specs if spec ["name" ]
93
+ ]
86
94
data = {
87
95
"specs" : specs ,
96
+ "urls" : urls ,
88
97
"title" : self .config .get ('title' , 'Flasgger' )
89
98
}
90
99
if request .args .get ('json' ):
Original file line number Diff line number Diff line change 11
11
Object . assign (
12
12
{
13
13
14
+ { % if urls % }
15
+ urls : { { urls | tojson } } ,
16
+ { % else % }
14
17
url : "{{ specs[0]['url'] }}" ,
18
+ { % endif % }
15
19
dom_id : '#swagger-ui' ,
16
20
validatorUrl : null ,
17
21
displayOperationId : true ,
You can’t perform that action at this time.
0 commit comments