Skip to content

Commit c2a00f3

Browse files
WiZeYARPetrov Yaroslav
and
Petrov Yaroslav
authored
Make recursive JsonSchema population also affect lists (#13)
Co-authored-by: Petrov Yaroslav <[email protected]>
1 parent 725ac45 commit c2a00f3

File tree

1 file changed

+17
-4
lines changed
  • src/asyncapi_python_codegen/document

1 file changed

+17
-4
lines changed

src/asyncapi_python_codegen/document/utils.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323

2424
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"""
2626

2727
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"""
2929

3030

3131
def populate_jsonschema_defs(schema: Any) -> Any:
@@ -45,10 +45,17 @@ def populate_jsonschema_defs(schema: Any) -> Any:
4545

4646
def _count_references(schema: Any, this: Reference, counter: ReferenceCounter):
4747
"""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
4855
if not isinstance(schema, dict):
4956
return
5057

51-
if "$ref" in schema:
58+
if "$ref" in schema: # If dict is $ref object
5259
ref: Ref[Any] = Ref.model_validate(schema)
5360
with set_current_doc_path(ref.filepath):
5461
ref = ref.flatten()
@@ -61,7 +68,7 @@ def _count_references(schema: Any, this: Reference, counter: ReferenceCounter):
6168
with set_current_doc_path(ref.filepath):
6269
return _count_references(doc, child, counter)
6370

64-
for v in schema.values():
71+
for v in schema.values(): # Recur
6572
_count_references(v, this, counter)
6673

6774

@@ -72,6 +79,12 @@ def _populate_jsonschema_recur(
7279
ignore_shared: bool = False,
7380
) -> Any:
7481
"""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
7588
if not isinstance(schema, dict):
7689
return schema
7790

0 commit comments

Comments
 (0)