Skip to content

Commit 4aa3d17

Browse files
author
Dante Acosta
committed
modified parameters for interactive swagger.
also changed flake8 precommit repo.
1 parent 5285c91 commit 4aa3d17

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ repos:
1414
args: [ --unsafe ]
1515
- id: debug-statements
1616
exclude: '^faraday/server/www/'
17-
- repo: https://gitlab.com/pycqa/flake8
17+
- repo: https://github.com/pycqa/flake8
1818
rev: 3.9.2
1919
hooks:
2020
- id: flake8

faraday/server/commands/app_urls.py

+37-4
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,47 @@
99
from flask import current_app
1010

1111
import yaml
12+
import re
1213
from apispec import APISpec
1314
from apispec.ext.marshmallow import MarshmallowPlugin
1415
from apispec_webframeworks.flask import FlaskPlugin
1516
from faraday import __version__ as f_version
1617
import json
17-
from urllib.parse import urljoin
1818
from faraday.server.config import LOCAL_OPENAPI_FILE
1919

2020
from faraday.utils.faraday_openapi_plugin import FaradayAPIPlugin
2121

2222

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+
2353
def openapi_format(server, modify_default=False, return_tags=False):
2454
extra_specs = {'info': {
2555
'description': 'The Faraday REST API enables you to interact with '
@@ -33,8 +63,6 @@ def openapi_format(server, modify_default=False, return_tags=False):
3363
if not server.startswith('http'):
3464
raise ValueError('Server must be an http url')
3565

36-
server = urljoin(server, "/_api")
37-
3866
extra_specs['servers'] = [{'url': server}]
3967

4068
spec = APISpec(
@@ -58,12 +86,17 @@ def openapi_format(server, modify_default=False, return_tags=False):
5886

5987
tags = set()
6088

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+
6194
with current_app.test_request_context():
6295
for name, endpoint in current_app.view_functions.items():
6396
# TODO: check why this endpoint is breaking spec.path
6497
if name in ('static', 'index'):
6598
continue
66-
spec.path(view=endpoint, app=current_app)
99+
spec.path(view=endpoint, app=current_app, parameters=endpoint_parameters[name])
67100

68101
# Set up global tags
69102
spec_yaml = yaml.load(spec.to_yaml(), Loader=yaml.SafeLoader)

0 commit comments

Comments
 (0)