MCPcopy
hub / github.com/pydantic/pydantic / handle_ref_overrides

Method handle_ref_overrides

pydantic/json_schema.py:2294–2319  ·  view source on GitHub ↗

Remove any sibling keys that are redundant with the referenced schema. Args: json_schema: The schema to remove redundant sibling keys from. Returns: The schema with redundant sibling keys removed.

(self, json_schema: JsonSchemaValue)

Source from the content-addressed store, hash-verified

2292 return defs_ref, ref_json_schema
2293
2294 def handle_ref_overrides(self, json_schema: JsonSchemaValue) -> JsonSchemaValue:
2295 """Remove any sibling keys that are redundant with the referenced schema.
2296
2297 Args:
2298 json_schema: The schema to remove redundant sibling keys from.
2299
2300 Returns:
2301 The schema with redundant sibling keys removed.
2302 """
2303 if '$ref' in json_schema:
2304 # prevent modifications to the input; this copy may be safe to drop if there is significant overhead
2305 json_schema = json_schema.copy()
2306
2307 referenced_json_schema = self.get_schema_from_definitions(JsonRef(json_schema['$ref']))
2308 if referenced_json_schema is None:
2309 # This can happen when building schemas for models with not-yet-defined references.
2310 # It may be a good idea to do a recursive pass at the end of the generation to remove
2311 # any redundant override keys.
2312 return json_schema
2313 for k, v in list(json_schema.items()):
2314 if k == '$ref':
2315 continue
2316 if k in referenced_json_schema and referenced_json_schema[k] == v:
2317 del json_schema[k] # redundant key
2318
2319 return json_schema
2320
2321 def get_schema_from_definitions(self, json_ref: JsonRef) -> JsonSchemaValue | None:
2322 try:

Callers 1

Calls 3

copyMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected