Generates JSON schema definitions from a list of core schemas, pairing the generated definitions with a mapping that links the input keys to the definition references. Args: inputs: A sequence of tuples, where: - The first element is a JSON schema key ty
(
self, inputs: Sequence[tuple[JsonSchemaKeyT, JsonSchemaMode, core_schema.CoreSchema]]
)
| 350 | return mapping |
| 351 | |
| 352 | def generate_definitions( |
| 353 | self, inputs: Sequence[tuple[JsonSchemaKeyT, JsonSchemaMode, core_schema.CoreSchema]] |
| 354 | ) -> tuple[dict[tuple[JsonSchemaKeyT, JsonSchemaMode], JsonSchemaValue], dict[DefsRef, JsonSchemaValue]]: |
| 355 | """Generates JSON schema definitions from a list of core schemas, pairing the generated definitions with a |
| 356 | mapping that links the input keys to the definition references. |
| 357 | |
| 358 | Args: |
| 359 | inputs: A sequence of tuples, where: |
| 360 | |
| 361 | - The first element is a JSON schema key type. |
| 362 | - The second element is the JSON mode: either 'validation' or 'serialization'. |
| 363 | - The third element is a core schema. |
| 364 | |
| 365 | Returns: |
| 366 | A tuple where: |
| 367 | |
| 368 | - The first element is a dictionary whose keys are tuples of JSON schema key type and JSON mode, and |
| 369 | whose values are the JSON schema corresponding to that pair of inputs. (These schemas may have |
| 370 | JsonRef references to definitions that are defined in the second returned element.) |
| 371 | - The second element is a dictionary whose keys are definition references for the JSON schemas |
| 372 | from the first returned element, and whose values are the actual JSON schema definitions. |
| 373 | |
| 374 | Raises: |
| 375 | PydanticUserError: Raised if the JSON schema generator has already been used to generate a JSON schema. |
| 376 | """ |
| 377 | if self._used: |
| 378 | raise PydanticUserError( |
| 379 | 'This JSON schema generator has already been used to generate a JSON schema. ' |
| 380 | f'You must create a new instance of {type(self).__name__} to generate a new JSON schema.', |
| 381 | code='json-schema-already-used', |
| 382 | ) |
| 383 | |
| 384 | for _, mode, schema in inputs: |
| 385 | self._mode = mode |
| 386 | self.generate_inner(schema) |
| 387 | |
| 388 | definitions_remapping = self._build_definitions_remapping() |
| 389 | |
| 390 | json_schemas_map: dict[tuple[JsonSchemaKeyT, JsonSchemaMode], DefsRef] = {} |
| 391 | for key, mode, schema in inputs: |
| 392 | self._mode = mode |
| 393 | json_schema = self.generate_inner(schema) |
| 394 | json_schemas_map[(key, mode)] = definitions_remapping.remap_json_schema(json_schema) |
| 395 | |
| 396 | json_schema = {'$defs': self.definitions} |
| 397 | json_schema = definitions_remapping.remap_json_schema(json_schema) |
| 398 | self._used = True |
| 399 | return json_schemas_map, self.sort(json_schema['$defs']) # type: ignore |
| 400 | |
| 401 | def generate(self, schema: CoreSchema, mode: JsonSchemaMode = 'validation') -> JsonSchemaValue: |
| 402 | """Generates a JSON schema for a specified schema in a specified mode. |