|
5 | 5 | import json
|
6 | 6 | from glob import glob
|
7 | 7 | from pathlib import Path
|
| 8 | +import re |
8 | 9 |
|
9 | 10 | SWAGGER_DOCS_DIR = 'docs/swagger_v3/'
|
10 | 11 | MDW_VERSION_FILE = 'AEMDW_VERSION'
|
|
14 | 15 | # Read YAML file
|
15 | 16 | schemas = {}
|
16 | 17 | paths = {}
|
| 18 | +node_schemas = None |
17 | 19 | swagger = None
|
18 | 20 | mdw_version = None
|
19 | 21 |
|
|
23 | 25 | with open(MDW_VERSION_FILE) as mdw_version_file:
|
24 | 26 | mdw_version = mdw_version_file.read().strip()
|
25 | 27 |
|
| 28 | +with open(os.path.join(SWAGGER_DOCS_DIR, 'node_oas3.yaml')) as node_stream: |
| 29 | + node_oas3 = yaml.safe_load(node_stream) |
| 30 | + node_schemas = node_oas3['components']['schemas'] |
| 31 | + |
26 | 32 | for filepath in Path(SWAGGER_DOCS_DIR).glob("*.spec.yaml"):
|
27 | 33 | with open(filepath, 'r') as stream:
|
28 | 34 | data_loaded = yaml.safe_load(stream)
|
|
36 | 42 | swagger['info']['version'] = mdw_version
|
37 | 43 | swagger['servers'][0]['url'] = PATH_PREFIX
|
38 | 44 |
|
| 45 | +old_swagger_schema_len = len(swagger['components']['schemas']) |
| 46 | + |
| 47 | +while True: |
| 48 | + swagger_str = json.dumps(swagger, indent=2) |
| 49 | + cleaned_refs = re.findall(r'(?m)"\$ref": "#/components/schemas/(\w+)"', swagger_str) |
| 50 | + |
| 51 | + missing_refs = [] |
| 52 | + for ref in cleaned_refs: |
| 53 | + if ref not in swagger['components']['schemas']: |
| 54 | + missing_refs.append(ref) |
| 55 | + |
| 56 | + if missing_refs == []: |
| 57 | + break |
| 58 | + |
| 59 | + for missing_ref in missing_refs: |
| 60 | + node_schema = node_schemas[missing_ref] |
| 61 | + swagger['components']['schemas'][missing_ref] = node_schema |
| 62 | + |
| 63 | +swagger_schema_len = len(swagger['components']['schemas']) |
| 64 | + |
| 65 | +print(f"Added {swagger_schema_len - old_swagger_schema_len} missing schemas") |
| 66 | + |
39 | 67 | with open(os.path.join(SWAGGER_OUTPUT_DIR, "swagger_v3.json"), 'w') as jsonfile:
|
40 | 68 | json.dump(swagger, jsonfile, indent=2)
|
41 | 69 |
|
|
0 commit comments