MCPcopy
hub / github.com/pydantic/pydantic / get_union_of_schemas

Method get_union_of_schemas

pydantic/json_schema.py:1290–1324  ·  view source on GitHub ↗

Returns the JSON Schema representation for the union of the provided JSON Schemas. The result depends on the configured `'union_format'`. Args: schemas: The list of JSON Schemas to be included in the union. Returns: The JSON Schema representing the

(self, schemas: list[JsonSchemaValue])

Source from the content-addressed store, hash-verified

1288 return self.get_union_of_schemas(generated)
1289
1290 def get_union_of_schemas(self, schemas: list[JsonSchemaValue]) -> JsonSchemaValue:
1291 """Returns the JSON Schema representation for the union of the provided JSON Schemas.
1292
1293 The result depends on the configured `'union_format'`.
1294
1295 Args:
1296 schemas: The list of JSON Schemas to be included in the union.
1297
1298 Returns:
1299 The JSON Schema representing the union of schemas.
1300 """
1301 if self.union_format == 'primitive_type_array':
1302 types: list[str] = []
1303 for schema in schemas:
1304 schema_types: list[str] | str | None = schema.get('type')
1305 if schema_types is None:
1306 # No type, meaning it can be a ref or an empty schema.
1307 break
1308 if not isinstance(schema_types, list):
1309 schema_types = [schema_types]
1310 if not all(t in _PRIMITIVE_JSON_SCHEMA_TYPES for t in schema_types):
1311 break
1312 if len(schema) != 1:
1313 # We only want to include types that don't have any constraints. For instance,
1314 # if `schemas = [{'type': 'string', 'maxLength': 3}, {'type': 'string', 'minLength': 5}]`,
1315 # we don't want to produce `{'type': 'string', 'maxLength': 3, 'minLength': 5}`.
1316 # Same if we have some metadata (e.g. `title`) on a specific union member, we want to preserve it.
1317 break
1318
1319 types.extend(schema_types)
1320 else:
1321 # If we got there, all the schemas where valid to be used with the `'primitive_type_array` format
1322 return {'type': list(dict.fromkeys(types))}
1323
1324 return self.get_flattened_anyof(schemas)
1325
1326 def tagged_union_schema(self, schema: core_schema.TaggedUnionSchema) -> JsonSchemaValue:
1327 """Generates a JSON schema that matches a schema that allows values matching any of the given schemas, where

Callers 3

handler_funcMethod · 0.95
nullable_schemaMethod · 0.95
union_schemaMethod · 0.95

Calls 2

get_flattened_anyofMethod · 0.95
getMethod · 0.45

Tested by

no test coverage detected