9
9
from flask import current_app
10
10
11
11
import yaml
12
+ import re
12
13
from apispec import APISpec
13
14
from apispec .ext .marshmallow import MarshmallowPlugin
14
15
from apispec_webframeworks .flask import FlaskPlugin
15
16
from faraday import __version__ as f_version
16
17
import json
17
- from urllib .parse import urljoin
18
18
from faraday .server .config import LOCAL_OPENAPI_FILE
19
19
20
20
from faraday .utils .faraday_openapi_plugin import FaradayAPIPlugin
21
21
22
22
23
+ def make_apispec_args (endpoint_route ):
24
+
25
+ parameters = []
26
+
27
+ endpoint_params = [x [1 :- 1 ] for x in endpoint_route .split ("/" ) if re .search ("<(.*)>" , x )]
28
+
29
+ for param in endpoint_params :
30
+
31
+ param_name = param_type = ""
32
+
33
+ if ":" not in param or re .search ("(path|string|uuid):(.+)" , param ):
34
+ param_name = param .split (":" )[- 1 ]
35
+ param_type = "string"
36
+ elif re .search ("int:(.+)" , param ):
37
+ param_name = param .split (":" )[- 1 ]
38
+ param_type = "integer"
39
+ elif re .search ("float:(.+)" , param ):
40
+ param_name = param .split (":" )[- 1 ]
41
+ param_type = "number"
42
+
43
+ parameters .append ({
44
+ "name" : param_name ,
45
+ "in" : "path" ,
46
+ "schema" : {"type" : param_type },
47
+ "required" : True
48
+ })
49
+
50
+ return parameters
51
+
52
+
23
53
def openapi_format (server , modify_default = False , return_tags = False ):
24
54
extra_specs = {'info' : {
25
55
'description' : 'The Faraday REST API enables you to interact with '
@@ -33,8 +63,6 @@ def openapi_format(server, modify_default=False, return_tags=False):
33
63
if not server .startswith ('http' ):
34
64
raise ValueError ('Server must be an http url' )
35
65
36
- server = urljoin (server , "/_api" )
37
-
38
66
extra_specs ['servers' ] = [{'url' : server }]
39
67
40
68
spec = APISpec (
@@ -58,12 +86,17 @@ def openapi_format(server, modify_default=False, return_tags=False):
58
86
59
87
tags = set ()
60
88
89
+ endpoint_parameters = {}
90
+
91
+ for rule in current_app .url_map .iter_rules ():
92
+ endpoint_parameters [rule .endpoint ] = make_apispec_args (rule .rule )
93
+
61
94
with current_app .test_request_context ():
62
95
for name , endpoint in current_app .view_functions .items ():
63
96
# TODO: check why this endpoint is breaking spec.path
64
97
if name in ('static' , 'index' ):
65
98
continue
66
- spec .path (view = endpoint , app = current_app )
99
+ spec .path (view = endpoint , app = current_app , parameters = endpoint_parameters [ name ] )
67
100
68
101
# Set up global tags
69
102
spec_yaml = yaml .load (spec .to_yaml (), Loader = yaml .SafeLoader )
0 commit comments