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,7 +16,11 @@ 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
@@ -27,6 +31,8 @@ def resolve_schema_references(schema_part: Dict[str, Any], reference_schema: Dic
27
31
Returns:
28
32
The schema with references resolved
29
33
"""
34
+ seen = seen or set ()
35
+
30
36
# Make a copy to avoid modifying the input schema
31
37
schema_part = schema_part .copy ()
32
38
@@ -35,6 +41,9 @@ def resolve_schema_references(schema_part: Dict[str, Any], reference_schema: Dic
35
41
ref_path = schema_part ["$ref" ]
36
42
# Standard OpenAPI references are in the format "#/components/schemas/ModelName"
37
43
if ref_path .startswith ("#/components/schemas/" ):
44
+ if ref_path in seen :
45
+ return {"$ref" : ref_path }
46
+ seen .add (ref_path )
38
47
model_name = ref_path .split ("/" )[- 1 ]
39
48
if "components" in reference_schema and "schemas" in reference_schema ["components" ]:
40
49
if model_name in reference_schema ["components" ]["schemas" ]:
@@ -47,11 +56,12 @@ def resolve_schema_references(schema_part: Dict[str, Any], reference_schema: Dic
47
56
# Recursively resolve references in all dictionary values
48
57
for key , value in schema_part .items ():
49
58
if isinstance (value , dict ):
50
- schema_part [key ] = resolve_schema_references (value , reference_schema )
59
+ schema_part [key ] = resolve_schema_references (value , reference_schema , seen )
51
60
elif isinstance (value , list ):
52
61
# Only process list items that are dictionaries since only they can contain refs
53
62
schema_part [key ] = [
54
- resolve_schema_references (item , reference_schema ) if isinstance (item , dict ) else item for item in value
63
+ resolve_schema_references (item , reference_schema , seen ) if isinstance (item , dict ) else item
64
+ for item in value
55
65
]
56
66
57
67
return schema_part
0 commit comments