From 22d61759a77f2ab3c4e610bb251684c78f71d2e6 Mon Sep 17 00:00:00 2001 From: Peter Selby Date: Thu, 16 Sep 2021 15:58:05 -0400 Subject: [PATCH] fix SwaggerHub allOf display issue --- Scripts/buildOpenAPI.py | 4 +++- Scripts/dereferenceAll.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Scripts/buildOpenAPI.py b/Scripts/buildOpenAPI.py index e9ff3c8c..3c9b5ed2 100644 --- a/Scripts/buildOpenAPI.py +++ b/Scripts/buildOpenAPI.py @@ -9,7 +9,7 @@ import glob import sys import os -from dereferenceAll import dereferenceBrAPI +import dereferenceAll def str_presenter(dumper, data): @@ -63,6 +63,8 @@ def go(rootPaths, metaFilePath = './swaggerMetaData.yaml'): out['paths'].update(paths) out['components'].update(defin) + out = dereferenceAll.dereferenceAllOfClause(out, out) + with open(outFilePath, 'w') as outfile: print(outFilePath) yaml.dump(out, outfile, default_flow_style=False, width=float("inf"), Dumper=noalias_dumper) diff --git a/Scripts/dereferenceAll.py b/Scripts/dereferenceAll.py index 93900331..47076174 100644 --- a/Scripts/dereferenceAll.py +++ b/Scripts/dereferenceAll.py @@ -51,6 +51,38 @@ def dereferenceAll(obj, parent): return obj +def dereferenceAllOfClause(obj, parent): + #print(breadCrumb) + try: + if type(obj) is dict: + for fieldStr in obj: + #print(fieldStr) + if(fieldStr == 'allOf'): + comboObj = {'properties': {}, 'type': 'object'} + for item in obj[fieldStr]: + itemObj = dereferenceAll(item, parent) + comboObj['properties'] = {**(comboObj['properties']), **(itemObj['properties'])} + if 'title' in itemObj: + comboObj['title'] = itemObj['title'] + if 'description' in itemObj: + comboObj['description'] = itemObj['description'] + if 'example' in itemObj: + comboObj['example'] = itemObj['example'] + + obj = comboObj + else: + obj[fieldStr] = dereferenceAllOfClause(obj[fieldStr], parent) + elif type(obj) is list: + newList = [] + for item in obj: + newList.append(dereferenceAllOfClause(item, parent)) + obj = newList + except Exception as ex: + ##print(obj) + raise ex + return obj + + def dereferenceBrAPI(filePath = './brapi_openapi.yaml', verbose = False): fileObj = {} if verbose :