@@ -7,7 +7,7 @@ def __init__(self, msg):
7
7
super (InvalidTypeError , self ).__init__ (msg )
8
8
9
9
10
- def convert (schema , options = None ):
10
+ def _prepare (schema , options = None ):
11
11
notSupported = [
12
12
'nullable' , 'discriminator' , 'readOnly' ,
13
13
'writeOnly' , 'xml' , 'externalDocs' ,
@@ -43,12 +43,53 @@ def convert(schema, options=None):
43
43
if options ['cloneSchema' ]:
44
44
schema = json .loads (json .dumps (schema ))
45
45
46
+ return schema , options
47
+
48
+
49
+ def convert (schema , options = None ):
50
+ schema , options = _prepare (schema , options )
51
+
46
52
schema = convertSchema (schema , options )
47
53
schema ['$schema' ] = 'http://json-schema.org/draft-04/schema#'
48
54
49
55
return schema
50
56
51
57
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
+
52
93
def convertSchema (schema , options ):
53
94
structs = options ['_structs' ]
54
95
notSupported = options ['_notSupported' ]
0 commit comments