@@ -78,8 +78,7 @@ def construct_open_api_with_schema_class(
78
78
if not schema_classes :
79
79
return open_api
80
80
81
- schema_classes .sort (key = lambda x : x .__name__ )
82
- logger .debug ("schema_classes: %s" , schema_classes )
81
+ schema_classes .sort (key = lambda x : x .__qualname__ )
83
82
84
83
# update new_open_api with new #/components/schemas
85
84
if PYDANTIC_V2 :
@@ -174,11 +173,19 @@ def _construct_ref_obj(pydantic_schema: PydanticSchema[PydanticType]) -> Referen
174
173
for JSONschema $ref names will get replaced with underscores.
175
174
Especially needed for Pydantic generic Models with brackets "[]"
176
175
176
+ For nested classes where the qualified name is not the same as the name of the
177
+ class, we need to prepend the module name in front of the qualified name. Otherwise,
178
+ just use the regular name of the class.
179
+
177
180
see: https://github.com/pydantic/pydantic/blob/aee6057378ccfec02126bf9c984a9b6d6b411777/pydantic/json_schema.py#L2031
178
181
"""
179
- ref_name = re .sub (
180
- r"[^a-zA-Z0-9.\-_]" , "_" , pydantic_schema .schema_class .__name__
181
- ).replace ("." , "__" )
182
+ module = pydantic_schema .schema_class .__module__
183
+ name = pydantic_schema .schema_class .__name__
184
+ qual_name = pydantic_schema .schema_class .__qualname__
185
+ specify_qualified_name = qual_name != name
186
+ schema_name = f"{ module } .{ qual_name } " if specify_qualified_name else name
187
+
188
+ ref_name = re .sub (r"[^a-zA-Z0-9.\-_]" , "_" , schema_name ).replace ("." , "__" )
182
189
ref_obj = Reference (** {"$ref" : ref_prefix + ref_name })
183
190
logger .debug (f"ref_obj={ ref_obj } " )
184
191
return ref_obj
0 commit comments