Simplify dump_swagger

We don't need most of the templating stuff. All we have to do is merge together
the swagger files, and resolve references.
This commit is contained in:
Richard van der Hoff 2017-09-27 07:48:50 +01:00
parent 726a8c2f61
commit 0dfff6b190

View file

@ -25,17 +25,17 @@ import json
import logging import logging
import os.path import os.path
import re import re
import shutil
import sys import sys
import yaml import yaml
scripts_dir = os.path.dirname(os.path.abspath(__file__)) scripts_dir = os.path.dirname(os.path.abspath(__file__))
templating_dir = os.path.join(os.path.dirname(scripts_dir), "templating") templating_dir = os.path.join(os.path.dirname(scripts_dir), "templating")
api_dir = os.path.join(os.path.dirname(scripts_dir), "api") api_dir = os.path.join(os.path.dirname(scripts_dir), "api")
sys.path.insert(0, templating_dir) sys.path.insert(0, templating_dir)
from matrix_templates.units import resolve_references, MatrixUnits from matrix_templates import units
if len(sys.argv) > 3: if len(sys.argv) > 3:
sys.stderr.write("usage: %s [output_file] [client_release_label]\n" % (sys.argv[0],)) sys.stderr.write("usage: %s [output_file] [client_release_label]\n" % (sys.argv[0],))
@ -53,12 +53,8 @@ match = re.match("^(r\d+)(\.\d+)*$", major_version)
if match: if match:
major_version = match.group(1) major_version = match.group(1)
logging.basicConfig() logging.basicConfig()
os.chdir(templating_dir)
apis = MatrixUnits().load_swagger_apis()
output = { output = {
"basePath": "/", "basePath": "/",
"consumes": ["application/json"], "consumes": ["application/json"],
@ -74,13 +70,23 @@ output = {
"swagger": "2.0", "swagger": "2.0",
} }
with open(os.path.join(api_dir, 'client-server', 'definitions', cs_api_dir = os.path.join(api_dir, 'client-server')
with open(os.path.join(cs_api_dir, 'definitions',
'security.yaml')) as f: 'security.yaml')) as f:
output['securityDefinitions'] = yaml.load(f) output['securityDefinitions'] = yaml.load(f)
for file, contents in apis.items(): for filename in os.listdir(cs_api_dir):
basePath = contents['basePath'] if not filename.endswith(".yaml"):
for path, methods in contents["paths"].items(): continue
filepath = os.path.join(cs_api_dir, filename)
print("Reading swagger API: %s" % filepath)
with open(filepath, "r") as f:
api = yaml.load(f.read())
api = units.resolve_references(filepath, api)
basePath = api['basePath']
for path, methods in api["paths"].items():
path = (basePath + path).replace('%CLIENT_MAJOR_VERSION%', path = (basePath + path).replace('%CLIENT_MAJOR_VERSION%',
major_version) major_version)
for method, spec in methods.items(): for method, spec in methods.items():
@ -89,7 +95,6 @@ for file, contents in apis.items():
output["paths"][path] = {} output["paths"][path] = {}
output["paths"][path][method] = spec output["paths"][path][method] = spec
print "Generating %s" % output_file print "Generating %s" % output_file
try: try: