Skip to content

Commit 1ce4d93

Browse files
committed
Process OpenAPI documents
This allows handling of complete OpenAPI specifications, iterating through the components and recursing through the paths. Related to pglass#8
1 parent ae4fc97 commit 1ce4d93

File tree

6 files changed

+11299
-2
lines changed

6 files changed

+11299
-2
lines changed

openapi_schema_to_json_schema/to_jsonschema.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def __init__(self, msg):
77
super(InvalidTypeError, self).__init__(msg)
88

99

10-
def convert(schema, options=None):
10+
def _prepare(schema, options=None):
1111
notSupported = [
1212
'nullable', 'discriminator', 'readOnly',
1313
'writeOnly', 'xml', 'externalDocs',
@@ -43,12 +43,53 @@ def convert(schema, options=None):
4343
if options['cloneSchema']:
4444
schema = json.loads(json.dumps(schema))
4545

46+
return schema, options
47+
48+
49+
def convert(schema, options=None):
50+
schema, options = _prepare(schema, options)
51+
4652
schema = convertSchema(schema, options)
4753
schema['$schema'] = 'http://json-schema.org/draft-04/schema#'
4854

4955
return schema
5056

5157

58+
def _recurse(tree, options):
59+
for key, subtree in list(tree.items()):
60+
if isinstance(subtree, dict):
61+
if key == 'schema':
62+
tree[key] = convertSchema(subtree, options)
63+
else:
64+
tree[key] = _recurse(subtree, options)
65+
elif isinstance(subtree, list):
66+
tree[key] = [
67+
_recurse(item, options) if isinstance(item, dict) else item
68+
for item in subtree
69+
]
70+
71+
return tree
72+
73+
74+
def convertDoc(doc, options):
75+
doc, options = _prepare(doc, options)
76+
77+
components_schemas = doc.get('components', {}).get('schemas')
78+
if components_schemas:
79+
for name, struct in list(components_schemas.items()):
80+
components_schemas[name] = convertSchema(struct, options)
81+
82+
paths = doc.get('paths')
83+
84+
if paths:
85+
doc['paths'] = dict((path, _recurse(tree, options))
86+
for path, tree in paths.items())
87+
88+
doc['$schema'] = 'http://json-schema.org/draft-04/schema#'
89+
90+
return doc
91+
92+
5293
def convertSchema(schema, options):
5394
structs = options['_structs']
5495
notSupported = options['_notSupported']

0 commit comments

Comments
 (0)