-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinline_examples.py
More file actions
executable file
·54 lines (37 loc) · 1.65 KB
/
inline_examples.py
File metadata and controls
executable file
·54 lines (37 loc) · 1.65 KB
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
#!/usr/bin/env python
import json
import os
import sys
from jsonpath_ng import parse
SPEC_DIR = f"{os.path.dirname(os.path.realpath(__file__))}/../specification"
def main(file: str):
with open(file, 'r') as f:
spec = json.load(f)
req_path = parse("$.paths.['/'].get.responses.*.content.['application/json'].examples.*.['$ref']")
req_examples = req_path.find(spec)
inline_examples(spec, req_examples)
req_path = parse("$.paths.['/'].post.responses.*.content.['application/json'].examples.*.['$ref']")
req_examples = req_path.find(spec)
inline_examples(spec, req_examples)
req_path = parse(
"$.paths.['/search-postcode-or-place'].post.responses.*.content.['application/json'].examples.*.['$ref']")
req_examples = req_path.find(spec)
inline_examples(spec, req_examples)
req_path = parse(
"$.paths.['/organisationtypes'].get.responses.*.content.['application/json'].examples.*.['$ref']")
req_examples = req_path.find(spec)
inline_examples(spec, req_examples)
print(json.dumps(spec))
def inline_examples(spec, examples_path):
for example in examples_path:
ref = example.full_path
example_file_content = read_example_from_component(spec, ref)
example_path = ref.left
example_path.update(spec, example_file_content)
def read_example_from_component(spec: dict, path):
component = path.find(spec)[0].value
com_path = component.replace("#/", "").replace("/", ".")
example_path = parse(f"$.{com_path}")
return example_path.find(spec)[0].value
if __name__ == '__main__':
main(sys.argv[1])