@@ -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,15 @@ def _defer(self, callable, *args, **kwargs):
67
70
if self .app :
68
71
bound ()
69
72
73
+ @property
74
+ def blueprint_name (self ):
75
+ if self .support_multiple_version :
76
+ return 'flask-apispec-' + self .spec .version
77
+ return 'flask-apispec'
78
+
70
79
def add_swagger_routes (self ):
71
80
blueprint = flask .Blueprint (
72
- 'flask-apispec' ,
81
+ self . blueprint_name ,
73
82
__name__ ,
74
83
static_folder = './static' ,
75
84
template_folder = './templates' ,
@@ -78,19 +87,37 @@ def add_swagger_routes(self):
78
87
79
88
json_url = self .app .config .get ('APISPEC_SWAGGER_URL' , '/swagger/' )
80
89
if json_url :
90
+ if self .support_multiple_version :
91
+ json_url = self .make_url_with_suffix_version (json_url )
92
+
81
93
blueprint .add_url_rule (json_url , 'swagger-json' , self .swagger_json )
82
94
83
95
ui_url = self .app .config .get ('APISPEC_SWAGGER_UI_URL' , '/swagger-ui/' )
84
96
if ui_url :
97
+ if self .support_multiple_version :
98
+ ui_url = self .make_url_with_suffix_version (ui_url )
85
99
blueprint .add_url_rule (ui_url , 'swagger-ui' , self .swagger_ui )
86
100
87
101
self .app .register_blueprint (blueprint )
88
102
103
+ def make_url_with_suffix_version (self , url ):
104
+ # adding version suffix
105
+ if url .endswith ('/' ):
106
+ url = url [:- 1 ] + '-' + self .spec .version + '/'
107
+ elif url .endswith ('.json' ) or url .endswith ('.html' ):
108
+ # support extension url
109
+ url = url .replace ('.json' , '-' + self .spec .version + '.json' ) \
110
+ .replace ('.html' , '-' + self .spec .version + '.html' )
111
+ else :
112
+ url += '-' + self .spec .version
113
+ return url
114
+
89
115
def swagger_json (self ):
90
116
return flask .jsonify (self .spec .to_dict ())
91
117
92
118
def swagger_ui (self ):
93
- return flask .render_template ('swagger-ui.html' )
119
+ return flask .render_template ('swagger-ui.html' ,
120
+ blueprint_name = self .blueprint_name )
94
121
95
122
def register_existing_resources (self ):
96
123
for name , rule in self .app .view_functions .items ():
0 commit comments