25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

1141 satır
45 KiB

  1. import re
  2. import warnings
  3. from collections import defaultdict
  4. from datetime import date, datetime, time, timedelta
  5. from decimal import Decimal
  6. from enum import Enum
  7. from ipaddress import IPv4Address, IPv4Interface, IPv4Network, IPv6Address, IPv6Interface, IPv6Network
  8. from pathlib import Path
  9. from typing import (
  10. TYPE_CHECKING,
  11. Any,
  12. Callable,
  13. Dict,
  14. FrozenSet,
  15. Generic,
  16. Iterable,
  17. List,
  18. Optional,
  19. Pattern,
  20. Sequence,
  21. Set,
  22. Tuple,
  23. Type,
  24. TypeVar,
  25. Union,
  26. cast,
  27. )
  28. from uuid import UUID
  29. from typing_extensions import Annotated, Literal
  30. from .fields import (
  31. MAPPING_LIKE_SHAPES,
  32. SHAPE_DEQUE,
  33. SHAPE_FROZENSET,
  34. SHAPE_GENERIC,
  35. SHAPE_ITERABLE,
  36. SHAPE_LIST,
  37. SHAPE_SEQUENCE,
  38. SHAPE_SET,
  39. SHAPE_SINGLETON,
  40. SHAPE_TUPLE,
  41. SHAPE_TUPLE_ELLIPSIS,
  42. FieldInfo,
  43. ModelField,
  44. )
  45. from .json import pydantic_encoder
  46. from .networks import AnyUrl, EmailStr
  47. from .types import (
  48. ConstrainedDecimal,
  49. ConstrainedFloat,
  50. ConstrainedFrozenSet,
  51. ConstrainedInt,
  52. ConstrainedList,
  53. ConstrainedSet,
  54. ConstrainedStr,
  55. SecretBytes,
  56. SecretStr,
  57. conbytes,
  58. condecimal,
  59. confloat,
  60. confrozenset,
  61. conint,
  62. conlist,
  63. conset,
  64. constr,
  65. )
  66. from .typing import (
  67. ForwardRef,
  68. all_literal_values,
  69. get_args,
  70. get_origin,
  71. get_sub_types,
  72. is_callable_type,
  73. is_literal_type,
  74. is_namedtuple,
  75. is_none_type,
  76. is_union,
  77. )
  78. from .utils import ROOT_KEY, get_model, lenient_issubclass, sequence_like
  79. if TYPE_CHECKING:
  80. from .dataclasses import Dataclass
  81. from .main import BaseModel
  82. default_prefix = '#/definitions/'
  83. default_ref_template = '#/definitions/{model}'
  84. TypeModelOrEnum = Union[Type['BaseModel'], Type[Enum]]
  85. TypeModelSet = Set[TypeModelOrEnum]
  86. def _apply_modify_schema(
  87. modify_schema: Callable[..., None], field: Optional[ModelField], field_schema: Dict[str, Any]
  88. ) -> None:
  89. from inspect import signature
  90. sig = signature(modify_schema)
  91. args = set(sig.parameters.keys())
  92. if 'field' in args or 'kwargs' in args:
  93. modify_schema(field_schema, field=field)
  94. else:
  95. modify_schema(field_schema)
  96. def schema(
  97. models: Sequence[Union[Type['BaseModel'], Type['Dataclass']]],
  98. *,
  99. by_alias: bool = True,
  100. title: Optional[str] = None,
  101. description: Optional[str] = None,
  102. ref_prefix: Optional[str] = None,
  103. ref_template: str = default_ref_template,
  104. ) -> Dict[str, Any]:
  105. """
  106. Process a list of models and generate a single JSON Schema with all of them defined in the ``definitions``
  107. top-level JSON key, including their sub-models.
  108. :param models: a list of models to include in the generated JSON Schema
  109. :param by_alias: generate the schemas using the aliases defined, if any
  110. :param title: title for the generated schema that includes the definitions
  111. :param description: description for the generated schema
  112. :param ref_prefix: the JSON Pointer prefix for schema references with ``$ref``, if None, will be set to the
  113. default of ``#/definitions/``. Update it if you want the schemas to reference the definitions somewhere
  114. else, e.g. for OpenAPI use ``#/components/schemas/``. The resulting generated schemas will still be at the
  115. top-level key ``definitions``, so you can extract them from there. But all the references will have the set
  116. prefix.
  117. :param ref_template: Use a ``string.format()`` template for ``$ref`` instead of a prefix. This can be useful
  118. for references that cannot be represented by ``ref_prefix`` such as a definition stored in another file. For
  119. a sibling json file in a ``/schemas`` directory use ``"/schemas/${model}.json#"``.
  120. :return: dict with the JSON Schema with a ``definitions`` top-level key including the schema definitions for
  121. the models and sub-models passed in ``models``.
  122. """
  123. clean_models = [get_model(model) for model in models]
  124. flat_models = get_flat_models_from_models(clean_models)
  125. model_name_map = get_model_name_map(flat_models)
  126. definitions = {}
  127. output_schema: Dict[str, Any] = {}
  128. if title:
  129. output_schema['title'] = title
  130. if description:
  131. output_schema['description'] = description
  132. for model in clean_models:
  133. m_schema, m_definitions, m_nested_models = model_process_schema(
  134. model,
  135. by_alias=by_alias,
  136. model_name_map=model_name_map,
  137. ref_prefix=ref_prefix,
  138. ref_template=ref_template,
  139. )
  140. definitions.update(m_definitions)
  141. model_name = model_name_map[model]
  142. definitions[model_name] = m_schema
  143. if definitions:
  144. output_schema['definitions'] = definitions
  145. return output_schema
  146. def model_schema(
  147. model: Union[Type['BaseModel'], Type['Dataclass']],
  148. by_alias: bool = True,
  149. ref_prefix: Optional[str] = None,
  150. ref_template: str = default_ref_template,
  151. ) -> Dict[str, Any]:
  152. """
  153. Generate a JSON Schema for one model. With all the sub-models defined in the ``definitions`` top-level
  154. JSON key.
  155. :param model: a Pydantic model (a class that inherits from BaseModel)
  156. :param by_alias: generate the schemas using the aliases defined, if any
  157. :param ref_prefix: the JSON Pointer prefix for schema references with ``$ref``, if None, will be set to the
  158. default of ``#/definitions/``. Update it if you want the schemas to reference the definitions somewhere
  159. else, e.g. for OpenAPI use ``#/components/schemas/``. The resulting generated schemas will still be at the
  160. top-level key ``definitions``, so you can extract them from there. But all the references will have the set
  161. prefix.
  162. :param ref_template: Use a ``string.format()`` template for ``$ref`` instead of a prefix. This can be useful for
  163. references that cannot be represented by ``ref_prefix`` such as a definition stored in another file. For a
  164. sibling json file in a ``/schemas`` directory use ``"/schemas/${model}.json#"``.
  165. :return: dict with the JSON Schema for the passed ``model``
  166. """
  167. model = get_model(model)
  168. flat_models = get_flat_models_from_model(model)
  169. model_name_map = get_model_name_map(flat_models)
  170. model_name = model_name_map[model]
  171. m_schema, m_definitions, nested_models = model_process_schema(
  172. model, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix, ref_template=ref_template
  173. )
  174. if model_name in nested_models:
  175. # model_name is in Nested models, it has circular references
  176. m_definitions[model_name] = m_schema
  177. m_schema = get_schema_ref(model_name, ref_prefix, ref_template, False)
  178. if m_definitions:
  179. m_schema.update({'definitions': m_definitions})
  180. return m_schema
  181. def get_field_info_schema(field: ModelField, schema_overrides: bool = False) -> Tuple[Dict[str, Any], bool]:
  182. # If no title is explicitly set, we don't set title in the schema for enums.
  183. # The behaviour is the same as `BaseModel` reference, where the default title
  184. # is in the definitions part of the schema.
  185. schema_: Dict[str, Any] = {}
  186. if field.field_info.title or not lenient_issubclass(field.type_, Enum):
  187. schema_['title'] = field.field_info.title or field.alias.title().replace('_', ' ')
  188. if field.field_info.title:
  189. schema_overrides = True
  190. if field.field_info.description:
  191. schema_['description'] = field.field_info.description
  192. schema_overrides = True
  193. if (
  194. not field.required
  195. and not field.field_info.const
  196. and field.default is not None
  197. and not is_callable_type(field.outer_type_)
  198. ):
  199. schema_['default'] = encode_default(field.default)
  200. schema_overrides = True
  201. return schema_, schema_overrides
  202. def field_schema(
  203. field: ModelField,
  204. *,
  205. by_alias: bool = True,
  206. model_name_map: Dict[TypeModelOrEnum, str],
  207. ref_prefix: Optional[str] = None,
  208. ref_template: str = default_ref_template,
  209. known_models: TypeModelSet = None,
  210. ) -> Tuple[Dict[str, Any], Dict[str, Any], Set[str]]:
  211. """
  212. Process a Pydantic field and return a tuple with a JSON Schema for it as the first item.
  213. Also return a dictionary of definitions with models as keys and their schemas as values. If the passed field
  214. is a model and has sub-models, and those sub-models don't have overrides (as ``title``, ``default``, etc), they
  215. will be included in the definitions and referenced in the schema instead of included recursively.
  216. :param field: a Pydantic ``ModelField``
  217. :param by_alias: use the defined alias (if any) in the returned schema
  218. :param model_name_map: used to generate the JSON Schema references to other models included in the definitions
  219. :param ref_prefix: the JSON Pointer prefix to use for references to other schemas, if None, the default of
  220. #/definitions/ will be used
  221. :param ref_template: Use a ``string.format()`` template for ``$ref`` instead of a prefix. This can be useful for
  222. references that cannot be represented by ``ref_prefix`` such as a definition stored in another file. For a
  223. sibling json file in a ``/schemas`` directory use ``"/schemas/${model}.json#"``.
  224. :param known_models: used to solve circular references
  225. :return: tuple of the schema for this field and additional definitions
  226. """
  227. s, schema_overrides = get_field_info_schema(field)
  228. validation_schema = get_field_schema_validations(field)
  229. if validation_schema:
  230. s.update(validation_schema)
  231. schema_overrides = True
  232. f_schema, f_definitions, f_nested_models = field_type_schema(
  233. field,
  234. by_alias=by_alias,
  235. model_name_map=model_name_map,
  236. schema_overrides=schema_overrides,
  237. ref_prefix=ref_prefix,
  238. ref_template=ref_template,
  239. known_models=known_models or set(),
  240. )
  241. # https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#discriminator-object
  242. if field.discriminator_key is not None:
  243. assert field.sub_fields_mapping is not None
  244. discriminator_models_refs: Dict[str, Union[str, Dict[str, Any]]] = {}
  245. for discriminator_value, sub_field in field.sub_fields_mapping.items():
  246. # sub_field is either a `BaseModel` or directly an `Annotated` `Union` of many
  247. if is_union(get_origin(sub_field.type_)):
  248. sub_models = get_sub_types(sub_field.type_)
  249. discriminator_models_refs[discriminator_value] = {
  250. model_name_map[sub_model]: get_schema_ref(
  251. model_name_map[sub_model], ref_prefix, ref_template, False
  252. )
  253. for sub_model in sub_models
  254. }
  255. else:
  256. sub_field_type = sub_field.type_
  257. if hasattr(sub_field_type, '__pydantic_model__'):
  258. sub_field_type = sub_field_type.__pydantic_model__
  259. discriminator_model_name = model_name_map[sub_field_type]
  260. discriminator_model_ref = get_schema_ref(discriminator_model_name, ref_prefix, ref_template, False)
  261. discriminator_models_refs[discriminator_value] = discriminator_model_ref['$ref']
  262. s['discriminator'] = {
  263. 'propertyName': field.discriminator_alias,
  264. 'mapping': discriminator_models_refs,
  265. }
  266. # $ref will only be returned when there are no schema_overrides
  267. if '$ref' in f_schema:
  268. return f_schema, f_definitions, f_nested_models
  269. else:
  270. s.update(f_schema)
  271. return s, f_definitions, f_nested_models
  272. numeric_types = (int, float, Decimal)
  273. _str_types_attrs: Tuple[Tuple[str, Union[type, Tuple[type, ...]], str], ...] = (
  274. ('max_length', numeric_types, 'maxLength'),
  275. ('min_length', numeric_types, 'minLength'),
  276. ('regex', str, 'pattern'),
  277. )
  278. _numeric_types_attrs: Tuple[Tuple[str, Union[type, Tuple[type, ...]], str], ...] = (
  279. ('gt', numeric_types, 'exclusiveMinimum'),
  280. ('lt', numeric_types, 'exclusiveMaximum'),
  281. ('ge', numeric_types, 'minimum'),
  282. ('le', numeric_types, 'maximum'),
  283. ('multiple_of', numeric_types, 'multipleOf'),
  284. )
  285. def get_field_schema_validations(field: ModelField) -> Dict[str, Any]:
  286. """
  287. Get the JSON Schema validation keywords for a ``field`` with an annotation of
  288. a Pydantic ``FieldInfo`` with validation arguments.
  289. """
  290. f_schema: Dict[str, Any] = {}
  291. if lenient_issubclass(field.type_, Enum):
  292. # schema is already updated by `enum_process_schema`; just update with field extra
  293. if field.field_info.extra:
  294. f_schema.update(field.field_info.extra)
  295. return f_schema
  296. if lenient_issubclass(field.type_, (str, bytes)):
  297. for attr_name, t, keyword in _str_types_attrs:
  298. attr = getattr(field.field_info, attr_name, None)
  299. if isinstance(attr, t):
  300. f_schema[keyword] = attr
  301. if lenient_issubclass(field.type_, numeric_types) and not issubclass(field.type_, bool):
  302. for attr_name, t, keyword in _numeric_types_attrs:
  303. attr = getattr(field.field_info, attr_name, None)
  304. if isinstance(attr, t):
  305. f_schema[keyword] = attr
  306. if field.field_info is not None and field.field_info.const:
  307. f_schema['const'] = field.default
  308. if field.field_info.extra:
  309. f_schema.update(field.field_info.extra)
  310. modify_schema = getattr(field.outer_type_, '__modify_schema__', None)
  311. if modify_schema:
  312. _apply_modify_schema(modify_schema, field, f_schema)
  313. return f_schema
  314. def get_model_name_map(unique_models: TypeModelSet) -> Dict[TypeModelOrEnum, str]:
  315. """
  316. Process a set of models and generate unique names for them to be used as keys in the JSON Schema
  317. definitions. By default the names are the same as the class name. But if two models in different Python
  318. modules have the same name (e.g. "users.Model" and "items.Model"), the generated names will be
  319. based on the Python module path for those conflicting models to prevent name collisions.
  320. :param unique_models: a Python set of models
  321. :return: dict mapping models to names
  322. """
  323. name_model_map = {}
  324. conflicting_names: Set[str] = set()
  325. for model in unique_models:
  326. model_name = normalize_name(model.__name__)
  327. if model_name in conflicting_names:
  328. model_name = get_long_model_name(model)
  329. name_model_map[model_name] = model
  330. elif model_name in name_model_map:
  331. conflicting_names.add(model_name)
  332. conflicting_model = name_model_map.pop(model_name)
  333. name_model_map[get_long_model_name(conflicting_model)] = conflicting_model
  334. name_model_map[get_long_model_name(model)] = model
  335. else:
  336. name_model_map[model_name] = model
  337. return {v: k for k, v in name_model_map.items()}
  338. def get_flat_models_from_model(model: Type['BaseModel'], known_models: TypeModelSet = None) -> TypeModelSet:
  339. """
  340. Take a single ``model`` and generate a set with itself and all the sub-models in the tree. I.e. if you pass
  341. model ``Foo`` (subclass of Pydantic ``BaseModel``) as ``model``, and it has a field of type ``Bar`` (also
  342. subclass of ``BaseModel``) and that model ``Bar`` has a field of type ``Baz`` (also subclass of ``BaseModel``),
  343. the return value will be ``set([Foo, Bar, Baz])``.
  344. :param model: a Pydantic ``BaseModel`` subclass
  345. :param known_models: used to solve circular references
  346. :return: a set with the initial model and all its sub-models
  347. """
  348. known_models = known_models or set()
  349. flat_models: TypeModelSet = set()
  350. flat_models.add(model)
  351. known_models |= flat_models
  352. fields = cast(Sequence[ModelField], model.__fields__.values())
  353. flat_models |= get_flat_models_from_fields(fields, known_models=known_models)
  354. return flat_models
  355. def get_flat_models_from_field(field: ModelField, known_models: TypeModelSet) -> TypeModelSet:
  356. """
  357. Take a single Pydantic ``ModelField`` (from a model) that could have been declared as a sublcass of BaseModel
  358. (so, it could be a submodel), and generate a set with its model and all the sub-models in the tree.
  359. I.e. if you pass a field that was declared to be of type ``Foo`` (subclass of BaseModel) as ``field``, and that
  360. model ``Foo`` has a field of type ``Bar`` (also subclass of ``BaseModel``) and that model ``Bar`` has a field of
  361. type ``Baz`` (also subclass of ``BaseModel``), the return value will be ``set([Foo, Bar, Baz])``.
  362. :param field: a Pydantic ``ModelField``
  363. :param known_models: used to solve circular references
  364. :return: a set with the model used in the declaration for this field, if any, and all its sub-models
  365. """
  366. from .dataclasses import dataclass, is_builtin_dataclass
  367. from .main import BaseModel
  368. flat_models: TypeModelSet = set()
  369. # Handle dataclass-based models
  370. if is_builtin_dataclass(field.type_):
  371. field.type_ = dataclass(field.type_)
  372. field_type = field.type_
  373. if lenient_issubclass(getattr(field_type, '__pydantic_model__', None), BaseModel):
  374. field_type = field_type.__pydantic_model__
  375. if field.sub_fields and not lenient_issubclass(field_type, BaseModel):
  376. flat_models |= get_flat_models_from_fields(field.sub_fields, known_models=known_models)
  377. elif lenient_issubclass(field_type, BaseModel) and field_type not in known_models:
  378. flat_models |= get_flat_models_from_model(field_type, known_models=known_models)
  379. elif lenient_issubclass(field_type, Enum):
  380. flat_models.add(field_type)
  381. return flat_models
  382. def get_flat_models_from_fields(fields: Sequence[ModelField], known_models: TypeModelSet) -> TypeModelSet:
  383. """
  384. Take a list of Pydantic ``ModelField``s (from a model) that could have been declared as subclasses of ``BaseModel``
  385. (so, any of them could be a submodel), and generate a set with their models and all the sub-models in the tree.
  386. I.e. if you pass a the fields of a model ``Foo`` (subclass of ``BaseModel``) as ``fields``, and on of them has a
  387. field of type ``Bar`` (also subclass of ``BaseModel``) and that model ``Bar`` has a field of type ``Baz`` (also
  388. subclass of ``BaseModel``), the return value will be ``set([Foo, Bar, Baz])``.
  389. :param fields: a list of Pydantic ``ModelField``s
  390. :param known_models: used to solve circular references
  391. :return: a set with any model declared in the fields, and all their sub-models
  392. """
  393. flat_models: TypeModelSet = set()
  394. for field in fields:
  395. flat_models |= get_flat_models_from_field(field, known_models=known_models)
  396. return flat_models
  397. def get_flat_models_from_models(models: Sequence[Type['BaseModel']]) -> TypeModelSet:
  398. """
  399. Take a list of ``models`` and generate a set with them and all their sub-models in their trees. I.e. if you pass
  400. a list of two models, ``Foo`` and ``Bar``, both subclasses of Pydantic ``BaseModel`` as models, and ``Bar`` has
  401. a field of type ``Baz`` (also subclass of ``BaseModel``), the return value will be ``set([Foo, Bar, Baz])``.
  402. """
  403. flat_models: TypeModelSet = set()
  404. for model in models:
  405. flat_models |= get_flat_models_from_model(model)
  406. return flat_models
  407. def get_long_model_name(model: TypeModelOrEnum) -> str:
  408. return f'{model.__module__}__{model.__qualname__}'.replace('.', '__')
  409. def field_type_schema(
  410. field: ModelField,
  411. *,
  412. by_alias: bool,
  413. model_name_map: Dict[TypeModelOrEnum, str],
  414. ref_template: str,
  415. schema_overrides: bool = False,
  416. ref_prefix: Optional[str] = None,
  417. known_models: TypeModelSet,
  418. ) -> Tuple[Dict[str, Any], Dict[str, Any], Set[str]]:
  419. """
  420. Used by ``field_schema()``, you probably should be using that function.
  421. Take a single ``field`` and generate the schema for its type only, not including additional
  422. information as title, etc. Also return additional schema definitions, from sub-models.
  423. """
  424. from .main import BaseModel # noqa: F811
  425. definitions = {}
  426. nested_models: Set[str] = set()
  427. f_schema: Dict[str, Any]
  428. if field.shape in {
  429. SHAPE_LIST,
  430. SHAPE_TUPLE_ELLIPSIS,
  431. SHAPE_SEQUENCE,
  432. SHAPE_SET,
  433. SHAPE_FROZENSET,
  434. SHAPE_ITERABLE,
  435. SHAPE_DEQUE,
  436. }:
  437. items_schema, f_definitions, f_nested_models = field_singleton_schema(
  438. field,
  439. by_alias=by_alias,
  440. model_name_map=model_name_map,
  441. ref_prefix=ref_prefix,
  442. ref_template=ref_template,
  443. known_models=known_models,
  444. )
  445. definitions.update(f_definitions)
  446. nested_models.update(f_nested_models)
  447. f_schema = {'type': 'array', 'items': items_schema}
  448. if field.shape in {SHAPE_SET, SHAPE_FROZENSET}:
  449. f_schema['uniqueItems'] = True
  450. elif field.shape in MAPPING_LIKE_SHAPES:
  451. f_schema = {'type': 'object'}
  452. key_field = cast(ModelField, field.key_field)
  453. regex = getattr(key_field.type_, 'regex', None)
  454. items_schema, f_definitions, f_nested_models = field_singleton_schema(
  455. field,
  456. by_alias=by_alias,
  457. model_name_map=model_name_map,
  458. ref_prefix=ref_prefix,
  459. ref_template=ref_template,
  460. known_models=known_models,
  461. )
  462. definitions.update(f_definitions)
  463. nested_models.update(f_nested_models)
  464. if regex:
  465. # Dict keys have a regex pattern
  466. # items_schema might be a schema or empty dict, add it either way
  467. f_schema['patternProperties'] = {regex.pattern: items_schema}
  468. elif items_schema:
  469. # The dict values are not simply Any, so they need a schema
  470. f_schema['additionalProperties'] = items_schema
  471. elif field.shape == SHAPE_TUPLE or (field.shape == SHAPE_GENERIC and not issubclass(field.type_, BaseModel)):
  472. sub_schema = []
  473. sub_fields = cast(List[ModelField], field.sub_fields)
  474. for sf in sub_fields:
  475. sf_schema, sf_definitions, sf_nested_models = field_type_schema(
  476. sf,
  477. by_alias=by_alias,
  478. model_name_map=model_name_map,
  479. ref_prefix=ref_prefix,
  480. ref_template=ref_template,
  481. known_models=known_models,
  482. )
  483. definitions.update(sf_definitions)
  484. nested_models.update(sf_nested_models)
  485. sub_schema.append(sf_schema)
  486. sub_fields_len = len(sub_fields)
  487. if field.shape == SHAPE_GENERIC:
  488. all_of_schemas = sub_schema[0] if sub_fields_len == 1 else {'type': 'array', 'items': sub_schema}
  489. f_schema = {'allOf': [all_of_schemas]}
  490. else:
  491. f_schema = {
  492. 'type': 'array',
  493. 'minItems': sub_fields_len,
  494. 'maxItems': sub_fields_len,
  495. }
  496. if sub_fields_len >= 1:
  497. f_schema['items'] = sub_schema
  498. else:
  499. assert field.shape in {SHAPE_SINGLETON, SHAPE_GENERIC}, field.shape
  500. f_schema, f_definitions, f_nested_models = field_singleton_schema(
  501. field,
  502. by_alias=by_alias,
  503. model_name_map=model_name_map,
  504. schema_overrides=schema_overrides,
  505. ref_prefix=ref_prefix,
  506. ref_template=ref_template,
  507. known_models=known_models,
  508. )
  509. definitions.update(f_definitions)
  510. nested_models.update(f_nested_models)
  511. # check field type to avoid repeated calls to the same __modify_schema__ method
  512. if field.type_ != field.outer_type_:
  513. if field.shape == SHAPE_GENERIC:
  514. field_type = field.type_
  515. else:
  516. field_type = field.outer_type_
  517. modify_schema = getattr(field_type, '__modify_schema__', None)
  518. if modify_schema:
  519. _apply_modify_schema(modify_schema, field, f_schema)
  520. return f_schema, definitions, nested_models
  521. def model_process_schema(
  522. model: TypeModelOrEnum,
  523. *,
  524. by_alias: bool = True,
  525. model_name_map: Dict[TypeModelOrEnum, str],
  526. ref_prefix: Optional[str] = None,
  527. ref_template: str = default_ref_template,
  528. known_models: TypeModelSet = None,
  529. field: Optional[ModelField] = None,
  530. ) -> Tuple[Dict[str, Any], Dict[str, Any], Set[str]]:
  531. """
  532. Used by ``model_schema()``, you probably should be using that function.
  533. Take a single ``model`` and generate its schema. Also return additional schema definitions, from sub-models. The
  534. sub-models of the returned schema will be referenced, but their definitions will not be included in the schema. All
  535. the definitions are returned as the second value.
  536. """
  537. from inspect import getdoc, signature
  538. known_models = known_models or set()
  539. if lenient_issubclass(model, Enum):
  540. model = cast(Type[Enum], model)
  541. s = enum_process_schema(model, field=field)
  542. return s, {}, set()
  543. model = cast(Type['BaseModel'], model)
  544. s = {'title': model.__config__.title or model.__name__}
  545. doc = getdoc(model)
  546. if doc:
  547. s['description'] = doc
  548. known_models.add(model)
  549. m_schema, m_definitions, nested_models = model_type_schema(
  550. model,
  551. by_alias=by_alias,
  552. model_name_map=model_name_map,
  553. ref_prefix=ref_prefix,
  554. ref_template=ref_template,
  555. known_models=known_models,
  556. )
  557. s.update(m_schema)
  558. schema_extra = model.__config__.schema_extra
  559. if callable(schema_extra):
  560. if len(signature(schema_extra).parameters) == 1:
  561. schema_extra(s)
  562. else:
  563. schema_extra(s, model)
  564. else:
  565. s.update(schema_extra)
  566. return s, m_definitions, nested_models
  567. def model_type_schema(
  568. model: Type['BaseModel'],
  569. *,
  570. by_alias: bool,
  571. model_name_map: Dict[TypeModelOrEnum, str],
  572. ref_template: str,
  573. ref_prefix: Optional[str] = None,
  574. known_models: TypeModelSet,
  575. ) -> Tuple[Dict[str, Any], Dict[str, Any], Set[str]]:
  576. """
  577. You probably should be using ``model_schema()``, this function is indirectly used by that function.
  578. Take a single ``model`` and generate the schema for its type only, not including additional
  579. information as title, etc. Also return additional schema definitions, from sub-models.
  580. """
  581. properties = {}
  582. required = []
  583. definitions: Dict[str, Any] = {}
  584. nested_models: Set[str] = set()
  585. for k, f in model.__fields__.items():
  586. try:
  587. f_schema, f_definitions, f_nested_models = field_schema(
  588. f,
  589. by_alias=by_alias,
  590. model_name_map=model_name_map,
  591. ref_prefix=ref_prefix,
  592. ref_template=ref_template,
  593. known_models=known_models,
  594. )
  595. except SkipField as skip:
  596. warnings.warn(skip.message, UserWarning)
  597. continue
  598. definitions.update(f_definitions)
  599. nested_models.update(f_nested_models)
  600. if by_alias:
  601. properties[f.alias] = f_schema
  602. if f.required:
  603. required.append(f.alias)
  604. else:
  605. properties[k] = f_schema
  606. if f.required:
  607. required.append(k)
  608. if ROOT_KEY in properties:
  609. out_schema = properties[ROOT_KEY]
  610. out_schema['title'] = model.__config__.title or model.__name__
  611. else:
  612. out_schema = {'type': 'object', 'properties': properties}
  613. if required:
  614. out_schema['required'] = required
  615. if model.__config__.extra == 'forbid':
  616. out_schema['additionalProperties'] = False
  617. return out_schema, definitions, nested_models
  618. def enum_process_schema(enum: Type[Enum], *, field: Optional[ModelField] = None) -> Dict[str, Any]:
  619. """
  620. Take a single `enum` and generate its schema.
  621. This is similar to the `model_process_schema` function, but applies to ``Enum`` objects.
  622. """
  623. from inspect import getdoc
  624. schema_: Dict[str, Any] = {
  625. 'title': enum.__name__,
  626. # Python assigns all enums a default docstring value of 'An enumeration', so
  627. # all enums will have a description field even if not explicitly provided.
  628. 'description': getdoc(enum),
  629. # Add enum values and the enum field type to the schema.
  630. 'enum': [item.value for item in cast(Iterable[Enum], enum)],
  631. }
  632. add_field_type_to_schema(enum, schema_)
  633. modify_schema = getattr(enum, '__modify_schema__', None)
  634. if modify_schema:
  635. _apply_modify_schema(modify_schema, field, schema_)
  636. return schema_
  637. def field_singleton_sub_fields_schema(
  638. sub_fields: Sequence[ModelField],
  639. *,
  640. by_alias: bool,
  641. model_name_map: Dict[TypeModelOrEnum, str],
  642. ref_template: str,
  643. schema_overrides: bool = False,
  644. ref_prefix: Optional[str] = None,
  645. known_models: TypeModelSet,
  646. ) -> Tuple[Dict[str, Any], Dict[str, Any], Set[str]]:
  647. """
  648. This function is indirectly used by ``field_schema()``, you probably should be using that function.
  649. Take a list of Pydantic ``ModelField`` from the declaration of a type with parameters, and generate their
  650. schema. I.e., fields used as "type parameters", like ``str`` and ``int`` in ``Tuple[str, int]``.
  651. """
  652. definitions = {}
  653. nested_models: Set[str] = set()
  654. if len(sub_fields) == 1:
  655. return field_type_schema(
  656. sub_fields[0],
  657. by_alias=by_alias,
  658. model_name_map=model_name_map,
  659. schema_overrides=schema_overrides,
  660. ref_prefix=ref_prefix,
  661. ref_template=ref_template,
  662. known_models=known_models,
  663. )
  664. else:
  665. sub_field_schemas = []
  666. for sf in sub_fields:
  667. sub_schema, sub_definitions, sub_nested_models = field_type_schema(
  668. sf,
  669. by_alias=by_alias,
  670. model_name_map=model_name_map,
  671. schema_overrides=schema_overrides,
  672. ref_prefix=ref_prefix,
  673. ref_template=ref_template,
  674. known_models=known_models,
  675. )
  676. definitions.update(sub_definitions)
  677. if schema_overrides and 'allOf' in sub_schema:
  678. # if the sub_field is a referenced schema we only need the referenced
  679. # object. Otherwise we will end up with several allOf inside anyOf.
  680. # See https://github.com/samuelcolvin/pydantic/issues/1209
  681. sub_schema = sub_schema['allOf'][0]
  682. sub_field_schemas.append(sub_schema)
  683. nested_models.update(sub_nested_models)
  684. return {'anyOf': sub_field_schemas}, definitions, nested_models
  685. # Order is important, e.g. subclasses of str must go before str
  686. # this is used only for standard library types, custom types should use __modify_schema__ instead
  687. field_class_to_schema: Tuple[Tuple[Any, Dict[str, Any]], ...] = (
  688. (Path, {'type': 'string', 'format': 'path'}),
  689. (datetime, {'type': 'string', 'format': 'date-time'}),
  690. (date, {'type': 'string', 'format': 'date'}),
  691. (time, {'type': 'string', 'format': 'time'}),
  692. (timedelta, {'type': 'number', 'format': 'time-delta'}),
  693. (IPv4Network, {'type': 'string', 'format': 'ipv4network'}),
  694. (IPv6Network, {'type': 'string', 'format': 'ipv6network'}),
  695. (IPv4Interface, {'type': 'string', 'format': 'ipv4interface'}),
  696. (IPv6Interface, {'type': 'string', 'format': 'ipv6interface'}),
  697. (IPv4Address, {'type': 'string', 'format': 'ipv4'}),
  698. (IPv6Address, {'type': 'string', 'format': 'ipv6'}),
  699. (Pattern, {'type': 'string', 'format': 'regex'}),
  700. (str, {'type': 'string'}),
  701. (bytes, {'type': 'string', 'format': 'binary'}),
  702. (bool, {'type': 'boolean'}),
  703. (int, {'type': 'integer'}),
  704. (float, {'type': 'number'}),
  705. (Decimal, {'type': 'number'}),
  706. (UUID, {'type': 'string', 'format': 'uuid'}),
  707. (dict, {'type': 'object'}),
  708. (list, {'type': 'array', 'items': {}}),
  709. (tuple, {'type': 'array', 'items': {}}),
  710. (set, {'type': 'array', 'items': {}, 'uniqueItems': True}),
  711. (frozenset, {'type': 'array', 'items': {}, 'uniqueItems': True}),
  712. )
  713. json_scheme = {'type': 'string', 'format': 'json-string'}
  714. def add_field_type_to_schema(field_type: Any, schema_: Dict[str, Any]) -> None:
  715. """
  716. Update the given `schema` with the type-specific metadata for the given `field_type`.
  717. This function looks through `field_class_to_schema` for a class that matches the given `field_type`,
  718. and then modifies the given `schema` with the information from that type.
  719. """
  720. for type_, t_schema in field_class_to_schema:
  721. # Fallback for `typing.Pattern` as it is not a valid class
  722. if lenient_issubclass(field_type, type_) or field_type is type_ is Pattern:
  723. schema_.update(t_schema)
  724. break
  725. def get_schema_ref(name: str, ref_prefix: Optional[str], ref_template: str, schema_overrides: bool) -> Dict[str, Any]:
  726. if ref_prefix:
  727. schema_ref = {'$ref': ref_prefix + name}
  728. else:
  729. schema_ref = {'$ref': ref_template.format(model=name)}
  730. return {'allOf': [schema_ref]} if schema_overrides else schema_ref
  731. def field_singleton_schema( # noqa: C901 (ignore complexity)
  732. field: ModelField,
  733. *,
  734. by_alias: bool,
  735. model_name_map: Dict[TypeModelOrEnum, str],
  736. ref_template: str,
  737. schema_overrides: bool = False,
  738. ref_prefix: Optional[str] = None,
  739. known_models: TypeModelSet,
  740. ) -> Tuple[Dict[str, Any], Dict[str, Any], Set[str]]:
  741. """
  742. This function is indirectly used by ``field_schema()``, you should probably be using that function.
  743. Take a single Pydantic ``ModelField``, and return its schema and any additional definitions from sub-models.
  744. """
  745. from .main import BaseModel
  746. definitions: Dict[str, Any] = {}
  747. nested_models: Set[str] = set()
  748. field_type = field.type_
  749. # Recurse into this field if it contains sub_fields and is NOT a
  750. # BaseModel OR that BaseModel is a const
  751. if field.sub_fields and (
  752. (field.field_info and field.field_info.const) or not lenient_issubclass(field_type, BaseModel)
  753. ):
  754. return field_singleton_sub_fields_schema(
  755. field.sub_fields,
  756. by_alias=by_alias,
  757. model_name_map=model_name_map,
  758. schema_overrides=schema_overrides,
  759. ref_prefix=ref_prefix,
  760. ref_template=ref_template,
  761. known_models=known_models,
  762. )
  763. if field_type is Any or field_type is object or field_type.__class__ == TypeVar:
  764. return {}, definitions, nested_models # no restrictions
  765. if is_none_type(field_type):
  766. return {'type': 'null'}, definitions, nested_models
  767. if is_callable_type(field_type):
  768. raise SkipField(f'Callable {field.name} was excluded from schema since JSON schema has no equivalent type.')
  769. f_schema: Dict[str, Any] = {}
  770. if field.field_info is not None and field.field_info.const:
  771. f_schema['const'] = field.default
  772. if is_literal_type(field_type):
  773. values = all_literal_values(field_type)
  774. if len({v.__class__ for v in values}) > 1:
  775. return field_schema(
  776. multitypes_literal_field_for_schema(values, field),
  777. by_alias=by_alias,
  778. model_name_map=model_name_map,
  779. ref_prefix=ref_prefix,
  780. ref_template=ref_template,
  781. known_models=known_models,
  782. )
  783. # All values have the same type
  784. field_type = values[0].__class__
  785. f_schema['enum'] = list(values)
  786. add_field_type_to_schema(field_type, f_schema)
  787. elif lenient_issubclass(field_type, Enum):
  788. enum_name = model_name_map[field_type]
  789. f_schema, schema_overrides = get_field_info_schema(field, schema_overrides)
  790. f_schema.update(get_schema_ref(enum_name, ref_prefix, ref_template, schema_overrides))
  791. definitions[enum_name] = enum_process_schema(field_type, field=field)
  792. elif is_namedtuple(field_type):
  793. sub_schema, *_ = model_process_schema(
  794. field_type.__pydantic_model__,
  795. by_alias=by_alias,
  796. model_name_map=model_name_map,
  797. ref_prefix=ref_prefix,
  798. ref_template=ref_template,
  799. known_models=known_models,
  800. field=field,
  801. )
  802. items_schemas = list(sub_schema['properties'].values())
  803. f_schema.update(
  804. {
  805. 'type': 'array',
  806. 'items': items_schemas,
  807. 'minItems': len(items_schemas),
  808. 'maxItems': len(items_schemas),
  809. }
  810. )
  811. elif not hasattr(field_type, '__pydantic_model__'):
  812. add_field_type_to_schema(field_type, f_schema)
  813. modify_schema = getattr(field_type, '__modify_schema__', None)
  814. if modify_schema:
  815. _apply_modify_schema(modify_schema, field, f_schema)
  816. if f_schema:
  817. return f_schema, definitions, nested_models
  818. # Handle dataclass-based models
  819. if lenient_issubclass(getattr(field_type, '__pydantic_model__', None), BaseModel):
  820. field_type = field_type.__pydantic_model__
  821. if issubclass(field_type, BaseModel):
  822. model_name = model_name_map[field_type]
  823. if field_type not in known_models:
  824. sub_schema, sub_definitions, sub_nested_models = model_process_schema(
  825. field_type,
  826. by_alias=by_alias,
  827. model_name_map=model_name_map,
  828. ref_prefix=ref_prefix,
  829. ref_template=ref_template,
  830. known_models=known_models,
  831. field=field,
  832. )
  833. definitions.update(sub_definitions)
  834. definitions[model_name] = sub_schema
  835. nested_models.update(sub_nested_models)
  836. else:
  837. nested_models.add(model_name)
  838. schema_ref = get_schema_ref(model_name, ref_prefix, ref_template, schema_overrides)
  839. return schema_ref, definitions, nested_models
  840. # For generics with no args
  841. args = get_args(field_type)
  842. if args is not None and not args and Generic in field_type.__bases__:
  843. return f_schema, definitions, nested_models
  844. raise ValueError(f'Value not declarable with JSON Schema, field: {field}')
  845. def multitypes_literal_field_for_schema(values: Tuple[Any, ...], field: ModelField) -> ModelField:
  846. """
  847. To support `Literal` with values of different types, we split it into multiple `Literal` with same type
  848. e.g. `Literal['qwe', 'asd', 1, 2]` becomes `Union[Literal['qwe', 'asd'], Literal[1, 2]]`
  849. """
  850. literal_distinct_types = defaultdict(list)
  851. for v in values:
  852. literal_distinct_types[v.__class__].append(v)
  853. distinct_literals = (Literal[tuple(same_type_values)] for same_type_values in literal_distinct_types.values())
  854. return ModelField(
  855. name=field.name,
  856. type_=Union[tuple(distinct_literals)], # type: ignore
  857. class_validators=field.class_validators,
  858. model_config=field.model_config,
  859. default=field.default,
  860. required=field.required,
  861. alias=field.alias,
  862. field_info=field.field_info,
  863. )
  864. def encode_default(dft: Any) -> Any:
  865. if isinstance(dft, Enum):
  866. return dft.value
  867. elif isinstance(dft, (int, float, str)):
  868. return dft
  869. elif sequence_like(dft):
  870. t = dft.__class__
  871. seq_args = (encode_default(v) for v in dft)
  872. return t(*seq_args) if is_namedtuple(t) else t(seq_args)
  873. elif isinstance(dft, dict):
  874. return {encode_default(k): encode_default(v) for k, v in dft.items()}
  875. elif dft is None:
  876. return None
  877. else:
  878. return pydantic_encoder(dft)
  879. _map_types_constraint: Dict[Any, Callable[..., type]] = {int: conint, float: confloat, Decimal: condecimal}
  880. def get_annotation_from_field_info(
  881. annotation: Any, field_info: FieldInfo, field_name: str, validate_assignment: bool = False
  882. ) -> Type[Any]:
  883. """
  884. Get an annotation with validation implemented for numbers and strings based on the field_info.
  885. :param annotation: an annotation from a field specification, as ``str``, ``ConstrainedStr``
  886. :param field_info: an instance of FieldInfo, possibly with declarations for validations and JSON Schema
  887. :param field_name: name of the field for use in error messages
  888. :param validate_assignment: default False, flag for BaseModel Config value of validate_assignment
  889. :return: the same ``annotation`` if unmodified or a new annotation with validation in place
  890. """
  891. constraints = field_info.get_constraints()
  892. used_constraints: Set[str] = set()
  893. if constraints:
  894. annotation, used_constraints = get_annotation_with_constraints(annotation, field_info)
  895. if validate_assignment:
  896. used_constraints.add('allow_mutation')
  897. unused_constraints = constraints - used_constraints
  898. if unused_constraints:
  899. raise ValueError(
  900. f'On field "{field_name}" the following field constraints are set but not enforced: '
  901. f'{", ".join(unused_constraints)}. '
  902. f'\nFor more details see https://pydantic-docs.helpmanual.io/usage/schema/#unenforced-field-constraints'
  903. )
  904. return annotation
  905. def get_annotation_with_constraints(annotation: Any, field_info: FieldInfo) -> Tuple[Type[Any], Set[str]]: # noqa: C901
  906. """
  907. Get an annotation with used constraints implemented for numbers and strings based on the field_info.
  908. :param annotation: an annotation from a field specification, as ``str``, ``ConstrainedStr``
  909. :param field_info: an instance of FieldInfo, possibly with declarations for validations and JSON Schema
  910. :return: the same ``annotation`` if unmodified or a new annotation along with the used constraints.
  911. """
  912. used_constraints: Set[str] = set()
  913. def go(type_: Any) -> Type[Any]:
  914. if (
  915. is_literal_type(type_)
  916. or isinstance(type_, ForwardRef)
  917. or lenient_issubclass(type_, (ConstrainedList, ConstrainedSet, ConstrainedFrozenSet))
  918. ):
  919. return type_
  920. origin = get_origin(type_)
  921. if origin is not None:
  922. args: Tuple[Any, ...] = get_args(type_)
  923. if any(isinstance(a, ForwardRef) for a in args):
  924. # forward refs cause infinite recursion below
  925. return type_
  926. if origin is Annotated:
  927. return go(args[0])
  928. if is_union(origin):
  929. return Union[tuple(go(a) for a in args)] # type: ignore
  930. if issubclass(origin, List) and (
  931. field_info.min_items is not None
  932. or field_info.max_items is not None
  933. or field_info.unique_items is not None
  934. ):
  935. used_constraints.update({'min_items', 'max_items', 'unique_items'})
  936. return conlist(
  937. go(args[0]),
  938. min_items=field_info.min_items,
  939. max_items=field_info.max_items,
  940. unique_items=field_info.unique_items,
  941. )
  942. if issubclass(origin, Set) and (field_info.min_items is not None or field_info.max_items is not None):
  943. used_constraints.update({'min_items', 'max_items'})
  944. return conset(go(args[0]), min_items=field_info.min_items, max_items=field_info.max_items)
  945. if issubclass(origin, FrozenSet) and (field_info.min_items is not None or field_info.max_items is not None):
  946. used_constraints.update({'min_items', 'max_items'})
  947. return confrozenset(go(args[0]), min_items=field_info.min_items, max_items=field_info.max_items)
  948. for t in (Tuple, List, Set, FrozenSet, Sequence):
  949. if issubclass(origin, t): # type: ignore
  950. return t[tuple(go(a) for a in args)] # type: ignore
  951. if issubclass(origin, Dict):
  952. return Dict[args[0], go(args[1])] # type: ignore
  953. attrs: Optional[Tuple[str, ...]] = None
  954. constraint_func: Optional[Callable[..., type]] = None
  955. if isinstance(type_, type):
  956. if issubclass(type_, (SecretStr, SecretBytes)):
  957. attrs = ('max_length', 'min_length')
  958. def constraint_func(**kw: Any) -> Type[Any]:
  959. return type(type_.__name__, (type_,), kw)
  960. elif issubclass(type_, str) and not issubclass(type_, (EmailStr, AnyUrl, ConstrainedStr)):
  961. attrs = ('max_length', 'min_length', 'regex')
  962. constraint_func = constr
  963. elif issubclass(type_, bytes):
  964. attrs = ('max_length', 'min_length', 'regex')
  965. constraint_func = conbytes
  966. elif issubclass(type_, numeric_types) and not issubclass(
  967. type_,
  968. (
  969. ConstrainedInt,
  970. ConstrainedFloat,
  971. ConstrainedDecimal,
  972. ConstrainedList,
  973. ConstrainedSet,
  974. ConstrainedFrozenSet,
  975. bool,
  976. ),
  977. ):
  978. # Is numeric type
  979. attrs = ('gt', 'lt', 'ge', 'le', 'multiple_of')
  980. if issubclass(type_, Decimal):
  981. attrs += ('max_digits', 'decimal_places')
  982. numeric_type = next(t for t in numeric_types if issubclass(type_, t)) # pragma: no branch
  983. constraint_func = _map_types_constraint[numeric_type]
  984. if attrs:
  985. used_constraints.update(set(attrs))
  986. kwargs = {
  987. attr_name: attr
  988. for attr_name, attr in ((attr_name, getattr(field_info, attr_name)) for attr_name in attrs)
  989. if attr is not None
  990. }
  991. if kwargs:
  992. constraint_func = cast(Callable[..., type], constraint_func)
  993. return constraint_func(**kwargs)
  994. return type_
  995. return go(annotation), used_constraints
  996. def normalize_name(name: str) -> str:
  997. """
  998. Normalizes the given name. This can be applied to either a model *or* enum.
  999. """
  1000. return re.sub(r'[^a-zA-Z0-9.\-_]', '_', name)
  1001. class SkipField(Exception):
  1002. """
  1003. Utility exception used to exclude fields from schema.
  1004. """
  1005. def __init__(self, message: str) -> None:
  1006. self.message = message