Skip to content

Commit 41878dd

Browse files
committed
Fix schema generation for newer pydantic version
1 parent b5435b7 commit 41878dd

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

generate_or_validate_json_schemas.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,21 @@ def get_schema_json_dict(cls: Any) -> dict[str, Any]:
117117
reference_match = reference_pattern.fullmatch(schema_json_dict["allOf"][0]["$ref"])
118118
assert (
119119
reference_match is not None
120-
), "Internal Error: Reference string has unexpected format: {schema_json_dict['allOf'][0]['$ref']}"
120+
), f"Internal Error: Reference string has unexpected format: {schema_json_dict['allOf'][0]['$ref']}"
121121
schema_json_dict_to_merge = schema_json_dict["$defs"][reference_match.group("cls_name")]
122122
del schema_json_dict["allOf"]
123123
schema_json_dict.update(schema_json_dict_to_merge)
124+
if {"$ref", "$defs"} == set(schema_json_dict.keys()):
125+
# The newer version of pydantic sometimes generates a schema with only a $ref and a $defs key where the $ref
126+
# field points to the actual schema definition in the $defs field.
127+
reference_pattern = re.compile(r"^#/\$defs/(?P<cls_name>\w+)$")
128+
reference_match = reference_pattern.fullmatch(schema_json_dict["$ref"])
129+
assert (
130+
reference_match is not None
131+
), f"Internal Error: Reference string has unexpected format: {schema_json_dict['$ref']}"
132+
schema_json_dict_to_merge = schema_json_dict["$defs"][reference_match.group("cls_name")]
133+
del schema_json_dict["$ref"]
134+
schema_json_dict.update(schema_json_dict_to_merge)
124135
if "$defs" in schema_json_dict:
125136
del schema_json_dict["$defs"]
126137
return schema_json_dict

0 commit comments

Comments
 (0)