Used by ``model_schema()``, you probably should be using that function. Take a single ``model`` and generate its schema. Also return additional schema definitions, from sub-models. The sub-models of the returned schema will be referenced, but their definitions will not be included in t
(
model: TypeModelOrEnum,
*,
by_alias: bool = True,
model_name_map: Dict[TypeModelOrEnum, str],
ref_prefix: Optional[str] = None,
ref_template: str = default_ref_template,
known_models: Optional[TypeModelSet] = None,
field: Optional[ModelField] = None,
)
| 549 | |
| 550 | |
| 551 | def model_process_schema( |
| 552 | model: TypeModelOrEnum, |
| 553 | *, |
| 554 | by_alias: bool = True, |
| 555 | model_name_map: Dict[TypeModelOrEnum, str], |
| 556 | ref_prefix: Optional[str] = None, |
| 557 | ref_template: str = default_ref_template, |
| 558 | known_models: Optional[TypeModelSet] = None, |
| 559 | field: Optional[ModelField] = None, |
| 560 | ) -> Tuple[Dict[str, Any], Dict[str, Any], Set[str]]: |
| 561 | """ |
| 562 | Used by ``model_schema()``, you probably should be using that function. |
| 563 | |
| 564 | Take a single ``model`` and generate its schema. Also return additional schema definitions, from sub-models. The |
| 565 | sub-models of the returned schema will be referenced, but their definitions will not be included in the schema. All |
| 566 | the definitions are returned as the second value. |
| 567 | """ |
| 568 | from inspect import getdoc, signature |
| 569 | |
| 570 | known_models = known_models or set() |
| 571 | if lenient_issubclass(model, Enum): |
| 572 | model = cast(Type[Enum], model) |
| 573 | s = enum_process_schema(model, field=field) |
| 574 | return s, {}, set() |
| 575 | model = cast(Type['BaseModel'], model) |
| 576 | s = {'title': model.__config__.title or model.__name__} |
| 577 | doc = getdoc(model) |
| 578 | if doc: |
| 579 | s['description'] = doc |
| 580 | known_models.add(model) |
| 581 | m_schema, m_definitions, nested_models = model_type_schema( |
| 582 | model, |
| 583 | by_alias=by_alias, |
| 584 | model_name_map=model_name_map, |
| 585 | ref_prefix=ref_prefix, |
| 586 | ref_template=ref_template, |
| 587 | known_models=known_models, |
| 588 | ) |
| 589 | s.update(m_schema) |
| 590 | schema_extra = model.__config__.schema_extra |
| 591 | if callable(schema_extra): |
| 592 | if len(signature(schema_extra).parameters) == 1: |
| 593 | schema_extra(s) |
| 594 | else: |
| 595 | schema_extra(s, model) |
| 596 | else: |
| 597 | s.update(schema_extra) |
| 598 | return s, m_definitions, nested_models |
| 599 | |
| 600 | |
| 601 | def model_type_schema( |
no test coverage detected