Skip to content

Commit 2deae74

Browse files
committed
Fix nested classes
1 parent 8f2a7ca commit 2deae74

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

openapi_pydantic/util.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ def construct_open_api_with_schema_class(
7878
if not schema_classes:
7979
return open_api
8080

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__)
8382

8483
# update new_open_api with new #/components/schemas
8584
if PYDANTIC_V2:
@@ -174,11 +173,19 @@ def _construct_ref_obj(pydantic_schema: PydanticSchema[PydanticType]) -> Referen
174173
for JSONschema $ref names will get replaced with underscores.
175174
Especially needed for Pydantic generic Models with brackets "[]"
176175
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+
177180
see: https://github.com/pydantic/pydantic/blob/aee6057378ccfec02126bf9c984a9b6d6b411777/pydantic/json_schema.py#L2031
178181
"""
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(".", "__")
182189
ref_obj = Reference(**{"$ref": ref_prefix + ref_name})
183190
logger.debug(f"ref_obj={ref_obj}")
184191
return ref_obj

openapi_pydantic/v3/v3_0/util.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def construct_open_api_with_schema_class(
149149
if not schema_classes:
150150
return open_api
151151

152-
schema_classes.sort(key=lambda x: x.__name__)
152+
schema_classes.sort(key=lambda x: x.__qualname__)
153153
logger.debug("schema_classes: %s", schema_classes)
154154

155155
# update new_open_api with new #/components/schemas
@@ -248,11 +248,19 @@ def _construct_ref_obj(pydantic_schema: PydanticSchema[PydanticType]) -> Referen
248248
for JSONschema $ref names will get replaced with underscores.
249249
Especially needed for Pydantic generic Models with brackets "[]"
250250
251+
For nested classes where the qualified name is not the same as the name of the
252+
class, we need to prepend the module name in front of the qualified name. Otherwise,
253+
just use the regular name of the class.
254+
251255
see: https://github.com/pydantic/pydantic/blob/aee6057378ccfec02126bf9c984a9b6d6b411777/pydantic/json_schema.py#L2031
252256
"""
253-
ref_name = re.sub(
254-
r"[^a-zA-Z0-9.\-_]", "_", pydantic_schema.schema_class.__name__
255-
).replace(".", "__")
257+
module = pydantic_schema.schema_class.__module__
258+
name = pydantic_schema.schema_class.__name__
259+
qual_name = pydantic_schema.schema_class.__qualname__
260+
specify_qualified_name = qual_name != name
261+
schema_name = f"{module}.{qual_name}" if specify_qualified_name else name
262+
263+
ref_name = re.sub(r"[^a-zA-Z0-9.\-_]", "_", schema_name).replace(".", "__")
256264
ref_obj = Reference(**{"$ref": ref_prefix + ref_name})
257265
logger.debug(f"ref_obj={ref_obj}")
258266
return ref_obj

0 commit comments

Comments
 (0)