-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathverify_docs.py
92 lines (77 loc) · 2.6 KB
/
verify_docs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/python3
# Simply check the swagger yaml files to make sure that every path:
# contains a "summary"
# "tags" are not present
#
# returns an exit code accordingly
import sys
import yaml
import copy
from distutils.version import StrictVersion
def verify_swagger(spec):
api_version = StrictVersion(spec["swagger"])
if api_version.version[0] != 2:
# Major version must be 2
print(
"Error: swagger version '%s' not supported; major version must be 2"
% api_version
)
return True
errored = False
for i in spec["paths"]:
for j in spec["paths"][i]:
k = spec["paths"][i][j]
if "summary" not in k:
print("Error: summary missing from Swagger doc in: %s" % (i))
errored = True
return errored
def verify_openapi(spec):
api_version = StrictVersion(spec["openapi"])
if api_version.version[0] != 3:
# Major version must be 3
print(
"Error: OpenAPI version '%s' not supported; major version must be 3"
% api_version
)
return True
errored = False
for i in spec["paths"]:
for j in spec["paths"][i]:
k = spec["paths"][i][j]
if "summary" not in k:
print("Error: summary missing from OpenAPI doc in: %s" % (i))
errored = True
return errored
def verify_asyncapi_1(spec):
msgs = spec["components"].get("messages", {})
errored = False
for name, msg in msgs.items():
if "summary" not in msg:
print("Error: #components/messages/%s is missing a summary" % name)
errored = True
return errored
def verify_docs_files(files):
errored = False
for yfile in files:
with open(yfile, "r") as ymlfile:
spec = yaml.load(ymlfile, Loader=yaml.SafeLoader)
if "openapi" in spec:
return verify_openapi(spec)
elif "swagger" in spec:
return verify_swagger(spec)
elif "asyncapi" in spec:
api_version = StrictVersion(spec["asyncapi"])
if api_version.version[0] == 1:
# We currently only support major version 1.
return verify_asyncapi_1(spec)
else:
print(
"Error: cannot verify AsyncAPI version %s; major version must be 1"
% api_version
)
return True
else:
print("Error: unable to identify API spec kind.")
return True
if __name__ == "__main__":
sys.exit(verify_docs_files(sys.argv[1:]))