Generate a JSON Schema for one model. With all the sub-models defined in the ``definitions`` top-level JSON key. :param model: a Pydantic model (a class that inherits from BaseModel) :param by_alias: generate the schemas using the aliases defined, if any :param ref_prefix: the
(
model: Union[Type['BaseModel'], Type['Dataclass']],
by_alias: bool = True,
ref_prefix: Optional[str] = None,
ref_template: str = default_ref_template,
)
| 160 | |
| 161 | |
| 162 | def model_schema( |
| 163 | model: Union[Type['BaseModel'], Type['Dataclass']], |
| 164 | by_alias: bool = True, |
| 165 | ref_prefix: Optional[str] = None, |
| 166 | ref_template: str = default_ref_template, |
| 167 | ) -> Dict[str, Any]: |
| 168 | """ |
| 169 | Generate a JSON Schema for one model. With all the sub-models defined in the ``definitions`` top-level |
| 170 | JSON key. |
| 171 | |
| 172 | :param model: a Pydantic model (a class that inherits from BaseModel) |
| 173 | :param by_alias: generate the schemas using the aliases defined, if any |
| 174 | :param ref_prefix: the JSON Pointer prefix for schema references with ``$ref``, if None, will be set to the |
| 175 | default of ``#/definitions/``. Update it if you want the schemas to reference the definitions somewhere |
| 176 | else, e.g. for OpenAPI use ``#/components/schemas/``. The resulting generated schemas will still be at the |
| 177 | top-level key ``definitions``, so you can extract them from there. But all the references will have the set |
| 178 | prefix. |
| 179 | :param ref_template: Use a ``string.format()`` template for ``$ref`` instead of a prefix. This can be useful for |
| 180 | references that cannot be represented by ``ref_prefix`` such as a definition stored in another file. For a |
| 181 | sibling json file in a ``/schemas`` directory use ``"/schemas/${model}.json#"``. |
| 182 | :return: dict with the JSON Schema for the passed ``model`` |
| 183 | """ |
| 184 | model = get_model(model) |
| 185 | flat_models = get_flat_models_from_model(model) |
| 186 | model_name_map = get_model_name_map(flat_models) |
| 187 | model_name = model_name_map[model] |
| 188 | m_schema, m_definitions, nested_models = model_process_schema( |
| 189 | model, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix, ref_template=ref_template |
| 190 | ) |
| 191 | if model_name in nested_models: |
| 192 | # model_name is in Nested models, it has circular references |
| 193 | m_definitions[model_name] = m_schema |
| 194 | m_schema = get_schema_ref(model_name, ref_prefix, ref_template, False) |
| 195 | if m_definitions: |
| 196 | m_schema.update({'definitions': m_definitions}) |
| 197 | return m_schema |
| 198 | |
| 199 | |
| 200 | def get_field_info_schema(field: ModelField, schema_overrides: bool = False) -> Tuple[Dict[str, Any], bool]: |
no test coverage detected