@@ -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,15 @@ def _defer(self, callable, *args, **kwargs):
6770 if self .app :
6871 bound ()
6972
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+
7079 def add_swagger_routes (self ):
7180 blueprint = flask .Blueprint (
72- 'flask-apispec' ,
81+ self . blueprint_name ,
7382 __name__ ,
7483 static_folder = './static' ,
7584 template_folder = './templates' ,
@@ -78,19 +87,37 @@ def add_swagger_routes(self):
7887
7988 json_url = self .app .config .get ('APISPEC_SWAGGER_URL' , '/swagger/' )
8089 if json_url :
90+ if self .support_multiple_version :
91+ json_url = self .make_url_with_suffix_version (json_url )
92+
8193 blueprint .add_url_rule (json_url , 'swagger-json' , self .swagger_json )
8294
8395 ui_url = self .app .config .get ('APISPEC_SWAGGER_UI_URL' , '/swagger-ui/' )
8496 if ui_url :
97+ if self .support_multiple_version :
98+ ui_url = self .make_url_with_suffix_version (ui_url )
8599 blueprint .add_url_rule (ui_url , 'swagger-ui' , self .swagger_ui )
86100
87101 self .app .register_blueprint (blueprint )
88102
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+
89115 def swagger_json (self ):
90116 return flask .jsonify (self .spec .to_dict ())
91117
92118 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 )
94121
95122 def register_existing_resources (self ):
96123 for name , rule in self .app .view_functions .items ():
0 commit comments