2828"""A reference counter"""
2929
3030
31- def count_references (schema : Any , this : Reference , counter : ReferenceCounter ):
31+ def populate_jsonschema_defs (schema : Any ) -> Any :
32+ """Given a $defs element of the JsonSchema
33+ 1. Constructs back references map for all links
34+ 2. Populates types by copying its body into parent $def (if there is only one reference)
35+ 3. Adds a new $defs object (if there is more than one reference), and rewrites $refs
36+ 4. Returns a huge jsonschema $defs object containing all structs that have been referenced by the structs
37+ from the original schema
38+ """
39+ counter : ReferenceCounter = defaultdict (lambda : set ())
40+ shared_schemas : dict [str , Any ] = {}
41+ _count_references (schema , None , counter )
42+ res = _populate_jsonschema_recur (schema , counter , shared_schemas )
43+ return {** res , ** shared_schemas }
44+
45+
46+ def _count_references (schema : Any , this : Reference , counter : ReferenceCounter ):
47+ """Recursively constructs back references within the JsonSchema"""
3248 if not isinstance (schema , dict ):
3349 return
3450
@@ -43,26 +59,19 @@ def count_references(schema: Any, this: Reference, counter: ReferenceCounter):
4359 child = (ref .filepath , ref .doc_path )
4460 counter [child ].add (this )
4561 with set_current_doc_path (ref .filepath ):
46- return count_references (doc , child , counter )
62+ return _count_references (doc , child , counter )
4763
4864 for v in schema .values ():
49- count_references (v , this , counter )
50-
51-
52- def populate_jsonschema (schema : Any ) -> Any :
53- counter : ReferenceCounter = defaultdict (lambda : set ())
54- shared_schemas : dict [str , Any ] = {}
55- count_references (schema , None , counter )
56- res = populate_jsonschema_recur (schema , counter , shared_schemas )
57- return {** res , ** shared_schemas }
65+ _count_references (v , this , counter )
5866
5967
60- def populate_jsonschema_recur (
68+ def _populate_jsonschema_recur (
6169 schema : Any ,
6270 counter : ReferenceCounter ,
6371 shared_schemas : dict [str , Any ],
6472 ignore_shared : bool = False ,
6573) -> Any :
74+ """Recursively populates JsonSchema $defs object"""
6675 if not isinstance (schema , dict ):
6776 return schema
6877
@@ -74,7 +83,7 @@ def populate_jsonschema_recur(
7483 back_refs = counter [(ref .filepath , ref .doc_path )]
7584 if len (back_refs ) > 1 and not ignore_shared :
7685 ref_struct_name = ref .doc_path [- 1 ]
77- shared_schemas [ref_struct_name ] = populate_jsonschema_recur (
86+ shared_schemas [ref_struct_name ] = _populate_jsonschema_recur (
7887 schema , counter , shared_schemas , True
7988 )
8089 return {"$ref" : f"#/$defs/{ ref_struct_name } " }
@@ -84,9 +93,9 @@ def populate_jsonschema_recur(
8493 for p in ref .doc_path :
8594 doc = doc [p ]
8695 with set_current_doc_path (ref .filepath ):
87- return populate_jsonschema_recur (doc , counter , shared_schemas )
96+ return _populate_jsonschema_recur (doc , counter , shared_schemas )
8897
8998 return {
90- k : populate_jsonschema_recur (v , counter , shared_schemas )
99+ k : _populate_jsonschema_recur (v , counter , shared_schemas )
91100 for k , v in schema .items ()
92101 }
0 commit comments