@@ -36,14 +36,17 @@ def get_pet(pet_id):
36
36
37
37
:param Flask app: App associated with API documentation
38
38
:param APISpec spec: apispec specification associated with API documentation
39
+ :param bool support_multiple_version: support multiple version swaggers
40
+ by register doc to 'swagger-{api_version}' url
39
41
"""
40
42
41
- def __init__ (self , app = None ):
43
+ def __init__ (self , app = None , support_multiple_version = False ):
42
44
self ._deferred = []
43
45
self .app = app
44
46
self .view_converter = None
45
47
self .resource_converter = None
46
48
self .spec = None
49
+ self .support_multiple_version = support_multiple_version
47
50
48
51
if app :
49
52
self .init_app (app )
@@ -67,9 +70,13 @@ def _defer(self, callable, *args, **kwargs):
67
70
if self .app :
68
71
bound ()
69
72
73
+ @property
74
+ def blueprint_name (self ):
75
+ return 'flask-apispec-' + self .spec .version if self .support_multiple_version else 'flask-apispec'
76
+
70
77
def add_swagger_routes (self ):
71
78
blueprint = flask .Blueprint (
72
- 'flask-apispec' ,
79
+ self . blueprint_name ,
73
80
__name__ ,
74
81
static_folder = './static' ,
75
82
template_folder = './templates' ,
@@ -78,19 +85,37 @@ def add_swagger_routes(self):
78
85
79
86
json_url = self .app .config .get ('APISPEC_SWAGGER_URL' , '/swagger/' )
80
87
if json_url :
88
+ if self .support_multiple_version :
89
+ json_url = self .make_url_with_suffix_version (json_url )
90
+
81
91
blueprint .add_url_rule (json_url , 'swagger-json' , self .swagger_json )
82
92
83
93
ui_url = self .app .config .get ('APISPEC_SWAGGER_UI_URL' , '/swagger-ui/' )
84
94
if ui_url :
95
+ if self .support_multiple_version :
96
+ ui_url = self .make_url_with_suffix_version (ui_url )
85
97
blueprint .add_url_rule (ui_url , 'swagger-ui' , self .swagger_ui )
86
98
87
99
self .app .register_blueprint (blueprint )
88
100
101
+ def make_url_with_suffix_version (self , url ):
102
+ # adding version suffix
103
+ if url .endswith ('/' ):
104
+ url = url [:- 1 ] + '-' + self .spec .version + '/'
105
+ elif url .endswith ('.json' ) or url .endswith ('.html' ):
106
+ # support extension url
107
+ url = url .replace ('.json' , '-' + self .spec .version + '.json' ) \
108
+ .replace ('.html' , '-' + self .spec .version + '.html' )
109
+ else :
110
+ url += '-' + self .spec .version
111
+ return url
112
+
89
113
def swagger_json (self ):
90
114
return flask .jsonify (self .spec .to_dict ())
91
115
92
116
def swagger_ui (self ):
93
- return flask .render_template ('swagger-ui.html' )
117
+ return flask .render_template ('swagger-ui.html' ,
118
+ blueprint_name = self .blueprint_name )
94
119
95
120
def register_existing_resources (self ):
96
121
for name , rule in self .app .view_functions .items ():
0 commit comments