22
22
23
23
24
24
Reference = Union [None , tuple [Path , tuple [str , ...]]]
25
- """A reference type"""
25
+ """A reference type, maps a document path to a set of references that point to it """
26
26
27
27
ReferenceCounter = defaultdict [Reference , set [Reference ]]
28
- """A reference counter"""
28
+ """A reference counter, maps each reference to a set of references that point to it """
29
29
30
30
31
31
def populate_jsonschema_defs (schema : Any ) -> Any :
@@ -45,10 +45,17 @@ def populate_jsonschema_defs(schema: Any) -> Any:
45
45
46
46
def _count_references (schema : Any , this : Reference , counter : ReferenceCounter ):
47
47
"""Recursively constructs back references within the JsonSchema"""
48
+
49
+ # List case
50
+ if isinstance (schema , list ):
51
+ for v in schema :
52
+ _count_references (v , this , counter )
53
+
54
+ # Dict case
48
55
if not isinstance (schema , dict ):
49
56
return
50
57
51
- if "$ref" in schema :
58
+ if "$ref" in schema : # If dict is $ref object
52
59
ref : Ref [Any ] = Ref .model_validate (schema )
53
60
with set_current_doc_path (ref .filepath ):
54
61
ref = ref .flatten ()
@@ -61,7 +68,7 @@ def _count_references(schema: Any, this: Reference, counter: ReferenceCounter):
61
68
with set_current_doc_path (ref .filepath ):
62
69
return _count_references (doc , child , counter )
63
70
64
- for v in schema .values ():
71
+ for v in schema .values (): # Recur
65
72
_count_references (v , this , counter )
66
73
67
74
@@ -72,6 +79,12 @@ def _populate_jsonschema_recur(
72
79
ignore_shared : bool = False ,
73
80
) -> Any :
74
81
"""Recursively populates JsonSchema $defs object"""
82
+
83
+ # List case
84
+ if isinstance (schema , list ):
85
+ return [_populate_jsonschema_recur (v , counter , shared_schemas , ignore_shared ) for v in schema ]
86
+
87
+ # Dict case
75
88
if not isinstance (schema , dict ):
76
89
return schema
77
90
0 commit comments