@@ -1070,6 +1070,37 @@ def _filter_defs_by_strategy(
10701070 return {** schema , "$defs" : new_defs }
10711071
10721072
1073+ def _try_rebuild_model (obj : type ) -> None :
1074+ """Try to rebuild a Pydantic model, handling config models specially."""
1075+ module = getattr (obj , "__module__" , "" )
1076+ class_name = getattr (obj , "__name__" , "" )
1077+ config_classes = {"GenerateConfig" , "ParserConfig" , "ParseConfig" }
1078+ if module in {"datamodel_code_generator.config" , "config" } and class_name in config_classes :
1079+ from datamodel_code_generator .model .base import DataModel , DataModelFieldBase # noqa: PLC0415
1080+ from datamodel_code_generator .types import DataTypeManager , StrictTypes # noqa: PLC0415
1081+
1082+ try :
1083+ from datamodel_code_generator .model .pydantic_v2 import UnionMode # noqa: PLC0415
1084+ except ImportError : # pragma: no cover
1085+ from typing import Any # noqa: PLC0415
1086+
1087+ runtime_union_mode = Any
1088+ else :
1089+ runtime_union_mode = UnionMode
1090+
1091+ types_namespace = {
1092+ "Path" : Path ,
1093+ "DataModel" : DataModel ,
1094+ "DataModelFieldBase" : DataModelFieldBase ,
1095+ "DataTypeManager" : DataTypeManager ,
1096+ "StrictTypes" : StrictTypes ,
1097+ "UnionMode" : runtime_union_mode ,
1098+ }
1099+ obj .model_rebuild (_types_namespace = types_namespace )
1100+ else :
1101+ obj .model_rebuild ()
1102+
1103+
10731104def _load_model_schema ( # noqa: PLR0912, PLR0914, PLR0915
10741105 input_model : str ,
10751106 input_file_type : InputFileType ,
@@ -1151,6 +1182,8 @@ def _load_model_schema( # noqa: PLR0912, PLR0914, PLR0915
11511182 if not hasattr (obj , "model_json_schema" ):
11521183 msg = "--input-model with Pydantic model requires Pydantic v2 runtime. Please upgrade Pydantic to v2."
11531184 raise Error (msg )
1185+ if hasattr (obj , "model_rebuild" ): # pragma: no branch
1186+ _try_rebuild_model (obj )
11541187 schema_generator = _get_input_model_json_schema_class ()
11551188 schema = obj .model_json_schema (schema_generator = schema_generator )
11561189 schema = _add_python_type_for_unserializable (schema , obj )
0 commit comments