Last active
September 14, 2018 16:03
-
-
Save lobster1234/a2f0e233f967caba347f53217cfee346 to your computer and use it in GitHub Desktop.
Quick and Dirty utility to merge swagger.json files for microservices.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from collections import OrderedDict | |
from operator import itemgetter | |
import requests | |
import sys | |
if len(sys.argv) < 2: | |
print("Usage : python merge.py [http(s)://path_to_swagger_json(s)]") | |
sys.exit(-1) | |
# Loop through the URLs | |
files = list() | |
for arg in sys.argv[1:]: | |
files.append(json.loads(requests.get(arg).text)) | |
# We set the header of the final file. | |
# This gets pulled from the first file in the list | |
final = OrderedDict() | |
final['swagger'] = files[0]['swagger'] | |
final['info'] = files[0]['info'] | |
final['basePath'] = files[0]['basePath'] | |
# Next we pull out the tags, paths and definitions | |
tags = list() | |
definitions = dict() | |
paths = dict() | |
for file in files: | |
tags = tags + file['tags'] | |
definitions.update(file['definitions']) | |
paths.update(file['paths']) | |
# Sort the paths | |
sortedPaths = OrderedDict() | |
for key in sorted(paths.keys()): | |
sortedPaths[key] = paths[key] | |
final['paths'] = sortedPaths | |
# Sort the tags | |
tags.sort(key=itemgetter('name')) | |
final['tags'] = tags | |
# Add definitions | |
final['definitions'] = definitions | |
print(json.dumps(final)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment