1
- from typing import Any , Dict
1
+ from typing import Any , Dict , Optional , Set
2
2
3
3
4
4
def get_single_param_type_from_schema (param_schema : Dict [str , Any ]) -> str :
@@ -16,17 +16,24 @@ def get_single_param_type_from_schema(param_schema: Dict[str, Any]) -> str:
16
16
return param_schema .get ("type" , "string" )
17
17
18
18
19
- def resolve_schema_references (schema_part : Dict [str , Any ], reference_schema : Dict [str , Any ]) -> Dict [str , Any ]:
19
+ def resolve_schema_references (
20
+ schema_part : Dict [str , Any ],
21
+ reference_schema : Dict [str , Any ],
22
+ seen : Optional [Set [str ]] = None ,
23
+ ) -> Dict [str , Any ]:
20
24
"""
21
25
Resolve schema references in OpenAPI schemas.
22
26
23
27
Args:
24
28
schema_part: The part of the schema being processed that may contain references
25
29
reference_schema: The complete schema used to resolve references from
30
+ seen: A set of already seen references to avoid infinite recursion
26
31
27
32
Returns:
28
33
The schema with references resolved
29
34
"""
35
+ seen = seen or set ()
36
+
30
37
# Make a copy to avoid modifying the input schema
31
38
schema_part = schema_part .copy ()
32
39
@@ -35,6 +42,9 @@ def resolve_schema_references(schema_part: Dict[str, Any], reference_schema: Dic
35
42
ref_path = schema_part ["$ref" ]
36
43
# Standard OpenAPI references are in the format "#/components/schemas/ModelName"
37
44
if ref_path .startswith ("#/components/schemas/" ):
45
+ if ref_path in seen :
46
+ return {"$ref" : ref_path }
47
+ seen .add (ref_path )
38
48
model_name = ref_path .split ("/" )[- 1 ]
39
49
if "components" in reference_schema and "schemas" in reference_schema ["components" ]:
40
50
if model_name in reference_schema ["components" ]["schemas" ]:
@@ -47,11 +57,12 @@ def resolve_schema_references(schema_part: Dict[str, Any], reference_schema: Dic
47
57
# Recursively resolve references in all dictionary values
48
58
for key , value in schema_part .items ():
49
59
if isinstance (value , dict ):
50
- schema_part [key ] = resolve_schema_references (value , reference_schema )
60
+ schema_part [key ] = resolve_schema_references (value , reference_schema , seen )
51
61
elif isinstance (value , list ):
52
62
# Only process list items that are dictionaries since only they can contain refs
53
63
schema_part [key ] = [
54
- resolve_schema_references (item , reference_schema ) if isinstance (item , dict ) else item for item in value
64
+ resolve_schema_references (item , reference_schema , seen ) if isinstance (item , dict ) else item
65
+ for item in value
55
66
]
56
67
57
68
return schema_part
0 commit comments