@@ -49,38 +49,69 @@ def get_openapi_schema(self, **kwargs):
49
49
schema_dict = schema .model_dump (by_alias = True )
50
50
51
51
# Find all multipart/form-data references that might be missing
52
- missing_refs = []
53
- paths = schema_dict .get ("paths" , {})
54
- for path_item in paths .values ():
55
- for _method , operation in path_item .items ():
56
- if not isinstance (operation , dict ):
57
- continue
58
-
59
- if "requestBody" not in operation :
60
- continue
61
-
62
- req_body = operation .get ("requestBody" , {})
63
- content = req_body .get ("content" , {})
64
- multipart = content .get ("multipart/form-data" , {})
65
- schema_ref = multipart .get ("schema" , {})
66
-
67
- if "$ref" in schema_ref :
68
- ref = schema_ref ["$ref" ]
69
- if ref .startswith ("#/components/schemas/" ):
70
- component_name = ref [len ("#/components/schemas/" ) :]
71
-
72
- # Check if the component exists
73
- components = schema_dict .get ("components" , {})
74
- schemas = components .get ("schemas" , {})
75
-
76
- if component_name not in schemas :
77
- missing_refs .append ((component_name , ref ))
52
+ missing_refs = self ._find_missing_component_references (schema_dict )
78
53
79
54
# If no missing references, return the original schema
80
55
if not missing_refs :
81
56
return schema
82
57
83
58
# Add missing components to the schema
59
+ self ._add_missing_components (schema_dict , missing_refs )
60
+
61
+ # Rebuild the schema with the added components
62
+ return schema .__class__ (** schema_dict )
63
+
64
+ def _find_missing_component_references (self , schema_dict : dict ) -> list [tuple [str , str ]]:
65
+ """Find all missing component references in multipart/form-data schemas."""
66
+ missing_refs = []
67
+ paths = schema_dict .get ("paths" , {})
68
+
69
+ for path_item in paths .values ():
70
+ self ._check_path_item_for_missing_refs (path_item , schema_dict , missing_refs )
71
+
72
+ return missing_refs
73
+
74
+ def _check_path_item_for_missing_refs (
75
+ self ,
76
+ path_item : dict ,
77
+ schema_dict : dict ,
78
+ missing_refs : list [tuple [str , str ]]
79
+ ) -> None :
80
+ """Check a single path item for missing component references."""
81
+ for _method , operation in path_item .items ():
82
+ if not isinstance (operation , dict ) or "requestBody" not in operation :
83
+ continue
84
+
85
+ self ._check_operation_for_missing_refs (operation , schema_dict , missing_refs )
86
+
87
+ def _check_operation_for_missing_refs (
88
+ self ,
89
+ operation : dict ,
90
+ schema_dict : dict ,
91
+ missing_refs : list [tuple [str , str ]]
92
+ ) -> None :
93
+ """Check a single operation for missing component references."""
94
+ req_body = operation .get ("requestBody" , {})
95
+ content = req_body .get ("content" , {})
96
+ multipart = content .get ("multipart/form-data" , {})
97
+ schema_ref = multipart .get ("schema" , {})
98
+
99
+ if "$ref" in schema_ref :
100
+ ref = schema_ref ["$ref" ]
101
+ if ref .startswith ("#/components/schemas/" ):
102
+ component_name = ref [len ("#/components/schemas/" ) :]
103
+
104
+ if self ._is_component_missing (schema_dict , component_name ):
105
+ missing_refs .append ((component_name , ref ))
106
+
107
+ def _is_component_missing (self , schema_dict : dict , component_name : str ) -> bool :
108
+ """Check if a component is missing from the schema."""
109
+ components = schema_dict .get ("components" , {})
110
+ schemas = components .get ("schemas" , {})
111
+ return component_name not in schemas
112
+
113
+ def _add_missing_components (self , schema_dict : dict , missing_refs : list [tuple [str , str ]]) -> None :
114
+ """Add missing components to the schema."""
84
115
components = schema_dict .setdefault ("components" , {})
85
116
schemas = components .setdefault ("schemas" , {})
86
117
@@ -98,9 +129,6 @@ def get_openapi_schema(self, **kwargs):
98
129
"required" : ["file" ],
99
130
}
100
131
101
- # Rebuild the schema with the added components
102
- return schema .__class__ (** schema_dict )
103
-
104
132
105
133
def create_test_app ():
106
134
"""Create a test app with the fixed resolver."""
0 commit comments