@@ -36,14 +36,17 @@ def get_pet(pet_id):
3636
3737 :param Flask app: App associated with API documentation
3838 :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
3941 """
4042
41- def __init__ (self , app = None ):
43+ def __init__ (self , app = None , support_multiple_version = False ):
4244 self ._deferred = []
4345 self .app = app
4446 self .view_converter = None
4547 self .resource_converter = None
4648 self .spec = None
49+ self .support_multiple_version = support_multiple_version
4750
4851 if app :
4952 self .init_app (app )
@@ -67,9 +70,13 @@ def _defer(self, callable, *args, **kwargs):
6770 if self .app :
6871 bound ()
6972
73+ @property
74+ def blueprint_name (self ):
75+ return 'flask-apispec-' + self .spec .version if self .support_multiple_version else 'flask-apispec'
76+
7077 def add_swagger_routes (self ):
7178 blueprint = flask .Blueprint (
72- 'flask-apispec' ,
79+ self . blueprint_name ,
7380 __name__ ,
7481 static_folder = './static' ,
7582 template_folder = './templates' ,
@@ -78,19 +85,37 @@ def add_swagger_routes(self):
7885
7986 json_url = self .app .config .get ('APISPEC_SWAGGER_URL' , '/swagger/' )
8087 if json_url :
88+ if self .support_multiple_version :
89+ json_url = self .make_url_with_suffix_version (json_url )
90+
8191 blueprint .add_url_rule (json_url , 'swagger-json' , self .swagger_json )
8292
8393 ui_url = self .app .config .get ('APISPEC_SWAGGER_UI_URL' , '/swagger-ui/' )
8494 if ui_url :
95+ if self .support_multiple_version :
96+ ui_url = self .make_url_with_suffix_version (ui_url )
8597 blueprint .add_url_rule (ui_url , 'swagger-ui' , self .swagger_ui )
8698
8799 self .app .register_blueprint (blueprint )
88100
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+
89113 def swagger_json (self ):
90114 return flask .jsonify (self .spec .to_dict ())
91115
92116 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 )
94119
95120 def register_existing_resources (self ):
96121 for name , rule in self .app .view_functions .items ():
0 commit comments