File tree Expand file tree Collapse file tree 1 file changed +16
-1
lines changed
Expand file tree Collapse file tree 1 file changed +16
-1
lines changed Original file line number Diff line number Diff line change 5353
5454if IS_PYDANTIC_V2 :
5555 from pydantic import SerializationInfo , model_serializer , model_validator
56+ from pydantic .errors import PydanticSchemaGenerationError
5657 from pydantic .json_schema import GenerateJsonSchema
5758else :
5859 from pydantic .class_validators import root_validator
@@ -160,7 +161,21 @@ class CustomGenerateJsonSchema(GenerateJsonSchema):
160161 """
161162
162163 def generate (self , * args , ** kwargs ):
163- model_cls = cast (Type [BaseModel ], args [0 ]["schema" ]["cls" ])
164+ model_cls : Optional [Type [BaseModel ]] = None
165+
166+ if "definitions" in args [0 ]:
167+ schema_ref = args [0 ]["schema" ]["schema_ref" ]
168+
169+ for definition in args [0 ]["definitions" ]:
170+ if definition ["ref" ] == schema_ref :
171+ model_cls = cast (Type [BaseModel ], definition ["schema" ]["cls" ])
172+ break
173+ else :
174+ model_cls = cast (Type [BaseModel ], args [0 ]["schema" ]["cls" ]) if "cls" in args [0 ]["schema" ] else None
175+
176+ if model_cls is None :
177+ raise PydanticSchemaGenerationError ("Could not find model class in definitions" )
178+
164179 generated_schema = super ().generate (* args , ** kwargs )
165180
166181 for field_name , field in get_model_fields (model_cls ).items ():
You can’t perform that action at this time.
0 commit comments