3
3
# @Time : 2021/4/30 14:25
4
4
import json
5
5
import os
6
+ import re
6
7
from copy import deepcopy
7
8
from functools import wraps
8
9
from typing import Optional , List , Dict , Union , Any , Type , Callable , Tuple
20
21
from .models .oauth import OAuthConfig
21
22
from .models .security import SecurityScheme
22
23
from .types import OpenAPIResponsesType
23
- from .utils import get_openapi_path , get_operation , get_responses , parse_and_store_tags , parse_parameters , \
24
- validate_responses_type , parse_method , get_operation_id_for_path
24
+ from .utils import get_operation , get_responses , parse_and_store_tags , parse_parameters , validate_responses_type , \
25
+ parse_method , get_operation_id_for_path
25
26
26
27
27
28
class OpenAPI (Flask ):
@@ -57,11 +58,11 @@ def __init__(
57
58
doc_ui: add openapi document UI(swagger and redoc). Defaults to True.
58
59
doc_expansion: String=["list"*, "full", "none"].
59
60
Controls the default expansion setting for the operations and tags.
60
- It can be ' list' (expands only the tags),
61
- ' full' (expands the tags and operations) or ' none' (expands nothing).
61
+ It can be " list" (expands only the tags),
62
+ " full" (expands the tags and operations) or " none" (expands nothing).
62
63
see https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md
63
- doc_prefix: URL prefix used for OpenAPI document and UI. Defaults to ' /openapi' .
64
- api_doc_url: The OpenAPI Spec documentation. Defaults to ' /openapi.json' .
64
+ doc_prefix: URL prefix used for OpenAPI document and UI. Defaults to " /openapi" .
65
+ api_doc_url: The OpenAPI Spec documentation. Defaults to " /openapi.json" .
65
66
swagger_url: The Swagger UI documentation. Defaults to `/swagger`.
66
67
redoc_url: The Redoc UI documentation. Defaults to `/redoc`.
67
68
rapidoc_url: The RapiDoc UI documentation. Defaults to `/rapidoc`.
@@ -74,7 +75,7 @@ def __init__(
74
75
75
76
self .openapi_version = "3.0.3"
76
77
if info is None :
77
- info = Info (title = ' OpenAPI' , version = ' 1.0.0' )
78
+ info = Info (title = " OpenAPI" , version = " 1.0.0" )
78
79
assert isinstance (info , Info ), f"Info is required (got type { type (info )} )"
79
80
self .info = info
80
81
self .security_schemes = security_schemes
@@ -106,55 +107,55 @@ def init_doc(self) -> None:
106
107
Provide Swagger UI, Redoc and Rapidoc
107
108
"""
108
109
_here = os .path .dirname (__file__ )
109
- template_folder = os .path .join (_here , ' templates' )
110
- static_folder = os .path .join (template_folder , ' static' )
110
+ template_folder = os .path .join (_here , " templates" )
111
+ static_folder = os .path .join (template_folder , " static" )
111
112
112
113
blueprint = Blueprint (
113
- ' openapi' ,
114
+ " openapi" ,
114
115
__name__ ,
115
116
url_prefix = self .doc_prefix ,
116
117
template_folder = template_folder ,
117
118
static_folder = static_folder
118
119
)
119
120
blueprint .add_url_rule (
120
121
rule = self .api_doc_url ,
121
- endpoint = ' api_doc' ,
122
+ endpoint = " api_doc" ,
122
123
view_func = lambda : self .api_doc
123
124
)
124
125
blueprint .add_url_rule (
125
126
rule = self .swagger_url ,
126
- endpoint = ' swagger' ,
127
+ endpoint = " swagger" ,
127
128
view_func = lambda : render_template (
128
129
"swagger.html" ,
129
- api_doc_url = self .api_doc_url .lstrip ('/' ),
130
+ api_doc_url = self .api_doc_url .lstrip ("/" ),
130
131
doc_expansion = self .doc_expansion ,
131
132
oauth_config = self .oauth_config .dict () if self .oauth_config else None
132
133
)
133
134
)
134
135
blueprint .add_url_rule (
135
136
rule = self .redoc_url ,
136
- endpoint = ' redoc' ,
137
+ endpoint = " redoc" ,
137
138
view_func = lambda : render_template (
138
139
"redoc.html" ,
139
- api_doc_url = self .api_doc_url .lstrip ('/' )
140
+ api_doc_url = self .api_doc_url .lstrip ("/" )
140
141
)
141
142
)
142
143
blueprint .add_url_rule (
143
144
rule = self .rapidoc_url ,
144
- endpoint = ' rapidoc' ,
145
+ endpoint = " rapidoc" ,
145
146
view_func = lambda : render_template (
146
147
"rapidoc.html" ,
147
- api_doc_url = self .api_doc_url .lstrip ('/' )
148
+ api_doc_url = self .api_doc_url .lstrip ("/" )
148
149
)
149
150
)
150
151
blueprint .add_url_rule (
151
- rule = '/' ,
152
- endpoint = ' index' ,
152
+ rule = "/" ,
153
+ endpoint = " index" ,
153
154
view_func = lambda : render_template (
154
155
"index.html" ,
155
- swagger_url = self .swagger_url .lstrip ('/' ),
156
- redoc_url = self .redoc_url .lstrip ('/' ),
157
- rapidoc_url = self .rapidoc_url .lstrip ('/' )
156
+ swagger_url = self .swagger_url .lstrip ("/" ),
157
+ redoc_url = self .redoc_url .lstrip ("/" ),
158
+ rapidoc_url = self .rapidoc_url .lstrip ("/" )
158
159
)
159
160
)
160
161
self .register_blueprint (blueprint )
@@ -252,7 +253,8 @@ def _do_decorator(
252
253
parse_parameters (func , components_schemas = self .components_schemas , operation = operation )
253
254
# parse response
254
255
get_responses (combine_responses , extra_responses , self .components_schemas , operation )
255
- uri = get_openapi_path (rule )
256
+ # /pet/<petId> --> /pet/{petId}
257
+ uri = re .sub (r"<([^<:]+:)?" , "{" , rule ).replace (">" , "}" )
256
258
# parse method
257
259
parse_method (uri , method , self .paths , operation )
258
260
return header , cookie , path , query , form , body , combine_responses
@@ -276,7 +278,7 @@ def get(
276
278
doc_ui : bool = True ,
277
279
** options : Any
278
280
) -> Callable :
279
- """Decorator for rest api, like: app.route(methods=[' GET' ])"""
281
+ """Decorator for rest api, like: app.route(methods=[" GET" ])"""
280
282
281
283
def decorator (func ) -> Callable :
282
284
header , cookie , path , query , form , body , combine_responses = \
@@ -332,7 +334,7 @@ def post(
332
334
doc_ui : bool = True ,
333
335
** options : Any
334
336
) -> Callable :
335
- """Decorator for rest api, like: app.route(methods=[' POST' ])"""
337
+ """Decorator for rest api, like: app.route(methods=[" POST" ])"""
336
338
337
339
def decorator (func ) -> Callable :
338
340
header , cookie , path , query , form , body , combine_responses = \
@@ -388,7 +390,7 @@ def put(
388
390
doc_ui : bool = True ,
389
391
** options : Any
390
392
) -> Callable :
391
- """Decorator for rest api, like: app.route(methods=[' PUT' ])"""
393
+ """Decorator for rest api, like: app.route(methods=[" PUT" ])"""
392
394
393
395
def decorator (func ) -> Callable :
394
396
header , cookie , path , query , form , body , combine_responses = \
@@ -444,7 +446,7 @@ def delete(
444
446
doc_ui : bool = True ,
445
447
** options : Any
446
448
) -> Callable :
447
- """Decorator for rest api, like: app.route(methods=[' DELETE' ])"""
449
+ """Decorator for rest api, like: app.route(methods=[" DELETE" ])"""
448
450
449
451
def decorator (func ) -> Callable :
450
452
header , cookie , path , query , form , body , combine_responses = \
@@ -500,7 +502,7 @@ def patch(
500
502
doc_ui : bool = True ,
501
503
** options : Any
502
504
) -> Callable :
503
- """Decorator for rest api, like: app.route(methods=[' PATCH' ])"""
505
+ """Decorator for rest api, like: app.route(methods=[" PATCH" ])"""
504
506
505
507
def decorator (func ) -> Callable :
506
508
header , cookie , path , query , form , body , combine_responses = \
0 commit comments