json_schema.py 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854
  1. """!!! abstract "Usage Documentation"
  2. [JSON Schema](../concepts/json_schema.md)
  3. The `json_schema` module contains classes and functions to allow the way [JSON Schema](https://json-schema.org/)
  4. is generated to be customized.
  5. In general you shouldn't need to use this module directly; instead, you can use
  6. [`BaseModel.model_json_schema`][pydantic.BaseModel.model_json_schema] and
  7. [`TypeAdapter.json_schema`][pydantic.TypeAdapter.json_schema].
  8. """
  9. from __future__ import annotations as _annotations
  10. import dataclasses
  11. import inspect
  12. import math
  13. import os
  14. import re
  15. import warnings
  16. from collections import Counter, defaultdict
  17. from collections.abc import Hashable, Iterable, Sequence
  18. from copy import deepcopy
  19. from enum import Enum
  20. from re import Pattern
  21. from typing import (
  22. TYPE_CHECKING,
  23. Annotated,
  24. Any,
  25. Callable,
  26. Literal,
  27. NewType,
  28. TypeVar,
  29. Union,
  30. cast,
  31. overload,
  32. )
  33. import pydantic_core
  34. from pydantic_core import MISSING, CoreSchema, PydanticOmit, core_schema, to_jsonable_python
  35. from pydantic_core.core_schema import ComputedField
  36. from typing_extensions import TypeAlias, assert_never, deprecated, final
  37. from typing_inspection.introspection import get_literal_values
  38. from pydantic.warnings import PydanticDeprecatedSince26, PydanticDeprecatedSince29
  39. from ._internal import (
  40. _config,
  41. _core_metadata,
  42. _core_utils,
  43. _decorators,
  44. _internal_dataclass,
  45. _mock_val_ser,
  46. _schema_generation_shared,
  47. )
  48. from .annotated_handlers import GetJsonSchemaHandler
  49. from .config import JsonDict, JsonValue
  50. from .errors import PydanticInvalidForJsonSchema, PydanticSchemaGenerationError, PydanticUserError
  51. if TYPE_CHECKING:
  52. from . import ConfigDict
  53. from ._internal._core_utils import CoreSchemaField, CoreSchemaOrField
  54. from ._internal._dataclasses import PydanticDataclass
  55. from ._internal._schema_generation_shared import GetJsonSchemaFunction
  56. from .main import BaseModel
  57. CoreSchemaOrFieldType = Literal[core_schema.CoreSchemaType, core_schema.CoreSchemaFieldType]
  58. """
  59. A type alias for defined schema types that represents a union of
  60. `core_schema.CoreSchemaType` and
  61. `core_schema.CoreSchemaFieldType`.
  62. """
  63. JsonSchemaValue = dict[str, Any]
  64. """
  65. A type alias for a JSON schema value. This is a dictionary of string keys to arbitrary JSON values.
  66. """
  67. JsonSchemaMode = Literal['validation', 'serialization']
  68. """
  69. A type alias that represents the mode of a JSON schema; either 'validation' or 'serialization'.
  70. For some types, the inputs to validation differ from the outputs of serialization. For example,
  71. computed fields will only be present when serializing, and should not be provided when
  72. validating. This flag provides a way to indicate whether you want the JSON schema required
  73. for validation inputs, or that will be matched by serialization outputs.
  74. """
  75. _MODE_TITLE_MAPPING: dict[JsonSchemaMode, str] = {'validation': 'Input', 'serialization': 'Output'}
  76. JsonSchemaWarningKind = Literal['skipped-choice', 'non-serializable-default', 'skipped-discriminator']
  77. """
  78. A type alias representing the kinds of warnings that can be emitted during JSON schema generation.
  79. See [`GenerateJsonSchema.render_warning_message`][pydantic.json_schema.GenerateJsonSchema.render_warning_message]
  80. for more details.
  81. """
  82. class PydanticJsonSchemaWarning(UserWarning):
  83. """This class is used to emit warnings produced during JSON schema generation.
  84. See the [`GenerateJsonSchema.emit_warning`][pydantic.json_schema.GenerateJsonSchema.emit_warning] and
  85. [`GenerateJsonSchema.render_warning_message`][pydantic.json_schema.GenerateJsonSchema.render_warning_message]
  86. methods for more details; these can be overridden to control warning behavior.
  87. """
  88. NoDefault = object()
  89. """A sentinel value used to indicate that no default value should be used when generating a JSON Schema
  90. for a core schema with a default value.
  91. """
  92. # ##### JSON Schema Generation #####
  93. DEFAULT_REF_TEMPLATE = '#/$defs/{model}'
  94. """The default format string used to generate reference names."""
  95. # There are three types of references relevant to building JSON schemas:
  96. # 1. core_schema "ref" values; these are not exposed as part of the JSON schema
  97. # * these might look like the fully qualified path of a model, its id, or something similar
  98. CoreRef = NewType('CoreRef', str)
  99. # 2. keys of the "definitions" object that will eventually go into the JSON schema
  100. # * by default, these look like "MyModel", though may change in the presence of collisions
  101. # * eventually, we may want to make it easier to modify the way these names are generated
  102. DefsRef = NewType('DefsRef', str)
  103. # 3. the values corresponding to the "$ref" key in the schema
  104. # * By default, these look like "#/$defs/MyModel", as in {"$ref": "#/$defs/MyModel"}
  105. JsonRef = NewType('JsonRef', str)
  106. CoreModeRef = tuple[CoreRef, JsonSchemaMode]
  107. JsonSchemaKeyT = TypeVar('JsonSchemaKeyT', bound=Hashable)
  108. _PRIMITIVE_JSON_SCHEMA_TYPES = ('string', 'boolean', 'null', 'integer', 'number')
  109. @dataclasses.dataclass(**_internal_dataclass.slots_true)
  110. class _DefinitionsRemapping:
  111. defs_remapping: dict[DefsRef, DefsRef]
  112. json_remapping: dict[JsonRef, JsonRef]
  113. @staticmethod
  114. def from_prioritized_choices(
  115. prioritized_choices: dict[DefsRef, list[DefsRef]],
  116. defs_to_json: dict[DefsRef, JsonRef],
  117. definitions: dict[DefsRef, JsonSchemaValue],
  118. ) -> _DefinitionsRemapping:
  119. """
  120. This function should produce a remapping that replaces complex DefsRef with the simpler ones from the
  121. prioritized_choices such that applying the name remapping would result in an equivalent JSON schema.
  122. """
  123. # We need to iteratively simplify the definitions until we reach a fixed point.
  124. # The reason for this is that outer definitions may reference inner definitions that get simplified
  125. # into an equivalent reference, and the outer definitions won't be equivalent until we've simplified
  126. # the inner definitions.
  127. copied_definitions = deepcopy(definitions)
  128. definitions_schema = {'$defs': copied_definitions}
  129. for _iter in range(100): # prevent an infinite loop in the case of a bug, 100 iterations should be enough
  130. # For every possible remapped DefsRef, collect all schemas that that DefsRef might be used for:
  131. schemas_for_alternatives: dict[DefsRef, list[JsonSchemaValue]] = defaultdict(list)
  132. for defs_ref in copied_definitions:
  133. alternatives = prioritized_choices[defs_ref]
  134. for alternative in alternatives:
  135. schemas_for_alternatives[alternative].append(copied_definitions[defs_ref])
  136. # Deduplicate the schemas for each alternative; the idea is that we only want to remap to a new DefsRef
  137. # if it introduces no ambiguity, i.e., there is only one distinct schema for that DefsRef.
  138. for defs_ref in schemas_for_alternatives:
  139. schemas_for_alternatives[defs_ref] = _deduplicate_schemas(schemas_for_alternatives[defs_ref])
  140. # Build the remapping
  141. defs_remapping: dict[DefsRef, DefsRef] = {}
  142. json_remapping: dict[JsonRef, JsonRef] = {}
  143. for original_defs_ref in definitions:
  144. alternatives = prioritized_choices[original_defs_ref]
  145. # Pick the first alternative that has only one schema, since that means there is no collision
  146. remapped_defs_ref = next(x for x in alternatives if len(schemas_for_alternatives[x]) == 1)
  147. defs_remapping[original_defs_ref] = remapped_defs_ref
  148. json_remapping[defs_to_json[original_defs_ref]] = defs_to_json[remapped_defs_ref]
  149. remapping = _DefinitionsRemapping(defs_remapping, json_remapping)
  150. new_definitions_schema = remapping.remap_json_schema({'$defs': copied_definitions})
  151. if definitions_schema == new_definitions_schema:
  152. # We've reached the fixed point
  153. return remapping
  154. definitions_schema = new_definitions_schema
  155. raise PydanticInvalidForJsonSchema('Failed to simplify the JSON schema definitions')
  156. def remap_defs_ref(self, ref: DefsRef) -> DefsRef:
  157. return self.defs_remapping.get(ref, ref)
  158. def remap_json_ref(self, ref: JsonRef) -> JsonRef:
  159. return self.json_remapping.get(ref, ref)
  160. def remap_json_schema(self, schema: Any) -> Any:
  161. """
  162. Recursively update the JSON schema replacing all $refs
  163. """
  164. if isinstance(schema, str):
  165. # Note: this may not really be a JsonRef; we rely on having no collisions between JsonRefs and other strings
  166. return self.remap_json_ref(JsonRef(schema))
  167. elif isinstance(schema, list):
  168. return [self.remap_json_schema(item) for item in schema]
  169. elif isinstance(schema, dict):
  170. for key, value in schema.items():
  171. if key == '$ref' and isinstance(value, str):
  172. schema['$ref'] = self.remap_json_ref(JsonRef(value))
  173. elif key == '$defs':
  174. schema['$defs'] = {
  175. self.remap_defs_ref(DefsRef(key)): self.remap_json_schema(value)
  176. for key, value in schema['$defs'].items()
  177. }
  178. else:
  179. schema[key] = self.remap_json_schema(value)
  180. return schema
  181. class GenerateJsonSchema:
  182. """!!! abstract "Usage Documentation"
  183. [Customizing the JSON Schema Generation Process](../concepts/json_schema.md#customizing-the-json-schema-generation-process)
  184. A class for generating JSON schemas.
  185. This class generates JSON schemas based on configured parameters. The default schema dialect
  186. is [https://json-schema.org/draft/2020-12/schema](https://json-schema.org/draft/2020-12/schema).
  187. The class uses `by_alias` to configure how fields with
  188. multiple names are handled and `ref_template` to format reference names.
  189. Attributes:
  190. schema_dialect: The JSON schema dialect used to generate the schema. See
  191. [Declaring a Dialect](https://json-schema.org/understanding-json-schema/reference/schema.html#id4)
  192. in the JSON Schema documentation for more information about dialects.
  193. ignored_warning_kinds: Warnings to ignore when generating the schema. `self.render_warning_message` will
  194. do nothing if its argument `kind` is in `ignored_warning_kinds`;
  195. this value can be modified on subclasses to easily control which warnings are emitted.
  196. by_alias: Whether to use field aliases when generating the schema.
  197. ref_template: The format string used when generating reference names.
  198. core_to_json_refs: A mapping of core refs to JSON refs.
  199. core_to_defs_refs: A mapping of core refs to definition refs.
  200. defs_to_core_refs: A mapping of definition refs to core refs.
  201. json_to_defs_refs: A mapping of JSON refs to definition refs.
  202. definitions: Definitions in the schema.
  203. Args:
  204. by_alias: Whether to use field aliases in the generated schemas.
  205. ref_template: The format string to use when generating reference names.
  206. union_format: The format to use when combining schemas from unions together. Can be one of:
  207. - `'any_of'`: Use the [`anyOf`](https://json-schema.org/understanding-json-schema/reference/combining#anyOf)
  208. keyword to combine schemas (the default).
  209. - `'primitive_type_array'`: Use the [`type`](https://json-schema.org/understanding-json-schema/reference/type)
  210. keyword as an array of strings, containing each type of the combination. If any of the schemas is not a primitive
  211. type (`string`, `boolean`, `null`, `integer` or `number`) or contains constraints/metadata, falls back to
  212. `any_of`.
  213. Raises:
  214. JsonSchemaError: If the instance of the class is inadvertently reused after generating a schema.
  215. """
  216. schema_dialect = 'https://json-schema.org/draft/2020-12/schema'
  217. # `self.render_warning_message` will do nothing if its argument `kind` is in `ignored_warning_kinds`;
  218. # this value can be modified on subclasses to easily control which warnings are emitted
  219. ignored_warning_kinds: set[JsonSchemaWarningKind] = {'skipped-choice'}
  220. def __init__(
  221. self,
  222. by_alias: bool = True,
  223. ref_template: str = DEFAULT_REF_TEMPLATE,
  224. union_format: Literal['any_of', 'primitive_type_array'] = 'any_of',
  225. ) -> None:
  226. self.by_alias = by_alias
  227. self.ref_template = ref_template
  228. self.union_format: Literal['any_of', 'primitive_type_array'] = union_format
  229. self.core_to_json_refs: dict[CoreModeRef, JsonRef] = {}
  230. self.core_to_defs_refs: dict[CoreModeRef, DefsRef] = {}
  231. self.defs_to_core_refs: dict[DefsRef, CoreModeRef] = {}
  232. self.json_to_defs_refs: dict[JsonRef, DefsRef] = {}
  233. self.definitions: dict[DefsRef, JsonSchemaValue] = {}
  234. self._config_wrapper_stack = _config.ConfigWrapperStack(_config.ConfigWrapper({}))
  235. self._mode: JsonSchemaMode = 'validation'
  236. # The following includes a mapping of a fully-unique defs ref choice to a list of preferred
  237. # alternatives, which are generally simpler, such as only including the class name.
  238. # At the end of schema generation, we use these to produce a JSON schema with more human-readable
  239. # definitions, which would also work better in a generated OpenAPI client, etc.
  240. self._prioritized_defsref_choices: dict[DefsRef, list[DefsRef]] = {}
  241. self._collision_counter: dict[str, int] = defaultdict(int)
  242. self._collision_index: dict[str, int] = {}
  243. self._schema_type_to_method = self.build_schema_type_to_method()
  244. # When we encounter definitions we need to try to build them immediately
  245. # so that they are available schemas that reference them
  246. # But it's possible that CoreSchema was never going to be used
  247. # (e.g. because the CoreSchema that references short circuits is JSON schema generation without needing
  248. # the reference) so instead of failing altogether if we can't build a definition we
  249. # store the error raised and re-throw it if we end up needing that def
  250. self._core_defs_invalid_for_json_schema: dict[DefsRef, PydanticInvalidForJsonSchema] = {}
  251. # This changes to True after generating a schema, to prevent issues caused by accidental reuse
  252. # of a single instance of a schema generator
  253. self._used = False
  254. @property
  255. def _config(self) -> _config.ConfigWrapper:
  256. return self._config_wrapper_stack.tail
  257. @property
  258. def mode(self) -> JsonSchemaMode:
  259. if self._config.json_schema_mode_override is not None:
  260. return self._config.json_schema_mode_override
  261. else:
  262. return self._mode
  263. def build_schema_type_to_method(
  264. self,
  265. ) -> dict[CoreSchemaOrFieldType, Callable[[CoreSchemaOrField], JsonSchemaValue]]:
  266. """Builds a dictionary mapping fields to methods for generating JSON schemas.
  267. Returns:
  268. A dictionary containing the mapping of `CoreSchemaOrFieldType` to a handler method.
  269. Raises:
  270. TypeError: If no method has been defined for generating a JSON schema for a given pydantic core schema type.
  271. """
  272. mapping: dict[CoreSchemaOrFieldType, Callable[[CoreSchemaOrField], JsonSchemaValue]] = {}
  273. core_schema_types: list[CoreSchemaOrFieldType] = list(get_literal_values(CoreSchemaOrFieldType))
  274. for key in core_schema_types:
  275. method_name = f'{key.replace("-", "_")}_schema'
  276. try:
  277. mapping[key] = getattr(self, method_name)
  278. except AttributeError as e: # pragma: no cover
  279. if os.getenv('PYDANTIC_PRIVATE_ALLOW_UNHANDLED_SCHEMA_TYPES'):
  280. continue
  281. raise TypeError(
  282. f'No method for generating JsonSchema for core_schema.type={key!r} '
  283. f'(expected: {type(self).__name__}.{method_name})'
  284. ) from e
  285. return mapping
  286. def generate_definitions(
  287. self, inputs: Sequence[tuple[JsonSchemaKeyT, JsonSchemaMode, core_schema.CoreSchema]]
  288. ) -> tuple[dict[tuple[JsonSchemaKeyT, JsonSchemaMode], JsonSchemaValue], dict[DefsRef, JsonSchemaValue]]:
  289. """Generates JSON schema definitions from a list of core schemas, pairing the generated definitions with a
  290. mapping that links the input keys to the definition references.
  291. Args:
  292. inputs: A sequence of tuples, where:
  293. - The first element is a JSON schema key type.
  294. - The second element is the JSON mode: either 'validation' or 'serialization'.
  295. - The third element is a core schema.
  296. Returns:
  297. A tuple where:
  298. - The first element is a dictionary whose keys are tuples of JSON schema key type and JSON mode, and
  299. whose values are the JSON schema corresponding to that pair of inputs. (These schemas may have
  300. JsonRef references to definitions that are defined in the second returned element.)
  301. - The second element is a dictionary whose keys are definition references for the JSON schemas
  302. from the first returned element, and whose values are the actual JSON schema definitions.
  303. Raises:
  304. PydanticUserError: Raised if the JSON schema generator has already been used to generate a JSON schema.
  305. """
  306. if self._used:
  307. raise PydanticUserError(
  308. 'This JSON schema generator has already been used to generate a JSON schema. '
  309. f'You must create a new instance of {type(self).__name__} to generate a new JSON schema.',
  310. code='json-schema-already-used',
  311. )
  312. for _, mode, schema in inputs:
  313. self._mode = mode
  314. self.generate_inner(schema)
  315. definitions_remapping = self._build_definitions_remapping()
  316. json_schemas_map: dict[tuple[JsonSchemaKeyT, JsonSchemaMode], DefsRef] = {}
  317. for key, mode, schema in inputs:
  318. self._mode = mode
  319. json_schema = self.generate_inner(schema)
  320. json_schemas_map[(key, mode)] = definitions_remapping.remap_json_schema(json_schema)
  321. json_schema = {'$defs': self.definitions}
  322. json_schema = definitions_remapping.remap_json_schema(json_schema)
  323. self._used = True
  324. return json_schemas_map, self.sort(json_schema['$defs']) # type: ignore
  325. def generate(self, schema: CoreSchema, mode: JsonSchemaMode = 'validation') -> JsonSchemaValue:
  326. """Generates a JSON schema for a specified schema in a specified mode.
  327. Args:
  328. schema: A Pydantic model.
  329. mode: The mode in which to generate the schema. Defaults to 'validation'.
  330. Returns:
  331. A JSON schema representing the specified schema.
  332. Raises:
  333. PydanticUserError: If the JSON schema generator has already been used to generate a JSON schema.
  334. """
  335. self._mode = mode
  336. if self._used:
  337. raise PydanticUserError(
  338. 'This JSON schema generator has already been used to generate a JSON schema. '
  339. f'You must create a new instance of {type(self).__name__} to generate a new JSON schema.',
  340. code='json-schema-already-used',
  341. )
  342. json_schema: JsonSchemaValue = self.generate_inner(schema)
  343. json_ref_counts = self.get_json_ref_counts(json_schema)
  344. ref = cast(JsonRef, json_schema.get('$ref'))
  345. while ref is not None: # may need to unpack multiple levels
  346. ref_json_schema = self.get_schema_from_definitions(ref)
  347. if json_ref_counts[ref] == 1 and ref_json_schema is not None and len(json_schema) == 1:
  348. # "Unpack" the ref since this is the only reference and there are no sibling keys
  349. json_schema = ref_json_schema.copy() # copy to prevent recursive dict reference
  350. json_ref_counts[ref] -= 1
  351. ref = cast(JsonRef, json_schema.get('$ref'))
  352. ref = None
  353. self._garbage_collect_definitions(json_schema)
  354. definitions_remapping = self._build_definitions_remapping()
  355. if self.definitions:
  356. json_schema['$defs'] = self.definitions
  357. json_schema = definitions_remapping.remap_json_schema(json_schema)
  358. # For now, we will not set the $schema key. However, if desired, this can be easily added by overriding
  359. # this method and adding the following line after a call to super().generate(schema):
  360. # json_schema['$schema'] = self.schema_dialect
  361. self._used = True
  362. return self.sort(json_schema)
  363. def generate_inner(self, schema: CoreSchemaOrField) -> JsonSchemaValue: # noqa: C901
  364. """Generates a JSON schema for a given core schema.
  365. Args:
  366. schema: The given core schema.
  367. Returns:
  368. The generated JSON schema.
  369. TODO: the nested function definitions here seem like bad practice, I'd like to unpack these
  370. in a future PR. It'd be great if we could shorten the call stack a bit for JSON schema generation,
  371. and I think there's potential for that here.
  372. """
  373. # If a schema with the same CoreRef has been handled, just return a reference to it
  374. # Note that this assumes that it will _never_ be the case that the same CoreRef is used
  375. # on types that should have different JSON schemas
  376. if 'ref' in schema:
  377. core_ref = CoreRef(schema['ref']) # type: ignore[typeddict-item]
  378. core_mode_ref = (core_ref, self.mode)
  379. if core_mode_ref in self.core_to_defs_refs and self.core_to_defs_refs[core_mode_ref] in self.definitions:
  380. return {'$ref': self.core_to_json_refs[core_mode_ref]}
  381. def populate_defs(core_schema: CoreSchema, json_schema: JsonSchemaValue) -> JsonSchemaValue:
  382. if 'ref' in core_schema:
  383. core_ref = CoreRef(core_schema['ref']) # type: ignore[typeddict-item]
  384. defs_ref, ref_json_schema = self.get_cache_defs_ref_schema(core_ref)
  385. json_ref = JsonRef(ref_json_schema['$ref'])
  386. # Replace the schema if it's not a reference to itself
  387. # What we want to avoid is having the def be just a ref to itself
  388. # which is what would happen if we blindly assigned any
  389. if json_schema.get('$ref', None) != json_ref:
  390. self.definitions[defs_ref] = json_schema
  391. self._core_defs_invalid_for_json_schema.pop(defs_ref, None)
  392. json_schema = ref_json_schema
  393. return json_schema
  394. def handler_func(schema_or_field: CoreSchemaOrField) -> JsonSchemaValue:
  395. """Generate a JSON schema based on the input schema.
  396. Args:
  397. schema_or_field: The core schema to generate a JSON schema from.
  398. Returns:
  399. The generated JSON schema.
  400. Raises:
  401. TypeError: If an unexpected schema type is encountered.
  402. """
  403. # Generate the core-schema-type-specific bits of the schema generation:
  404. json_schema: JsonSchemaValue | None = None
  405. if self.mode == 'serialization' and 'serialization' in schema_or_field:
  406. # In this case, we skip the JSON Schema generation of the schema
  407. # and use the `'serialization'` schema instead (canonical example:
  408. # `Annotated[int, PlainSerializer(str)]`).
  409. ser_schema = schema_or_field['serialization'] # type: ignore
  410. json_schema = self.ser_schema(ser_schema)
  411. # It might be that the 'serialization'` is skipped depending on `when_used`.
  412. # This is only relevant for `nullable` schemas though, so we special case here.
  413. if (
  414. json_schema is not None
  415. and ser_schema.get('when_used') in ('unless-none', 'json-unless-none')
  416. and schema_or_field['type'] == 'nullable'
  417. ):
  418. json_schema = self.get_union_of_schemas([{'type': 'null'}, json_schema])
  419. if json_schema is None:
  420. if _core_utils.is_core_schema(schema_or_field) or _core_utils.is_core_schema_field(schema_or_field):
  421. generate_for_schema_type = self._schema_type_to_method[schema_or_field['type']]
  422. json_schema = generate_for_schema_type(schema_or_field)
  423. else:
  424. raise TypeError(f'Unexpected schema type: schema={schema_or_field}')
  425. return json_schema
  426. current_handler = _schema_generation_shared.GenerateJsonSchemaHandler(self, handler_func)
  427. metadata = cast(_core_metadata.CoreMetadata, schema.get('metadata', {}))
  428. # TODO: I dislike that we have to wrap these basic dict updates in callables, is there any way around this?
  429. if js_updates := metadata.get('pydantic_js_updates'):
  430. def js_updates_handler_func(
  431. schema_or_field: CoreSchemaOrField,
  432. current_handler: GetJsonSchemaHandler = current_handler,
  433. ) -> JsonSchemaValue:
  434. json_schema = {**current_handler(schema_or_field), **js_updates}
  435. return json_schema
  436. current_handler = _schema_generation_shared.GenerateJsonSchemaHandler(self, js_updates_handler_func)
  437. if js_extra := metadata.get('pydantic_js_extra'):
  438. def js_extra_handler_func(
  439. schema_or_field: CoreSchemaOrField,
  440. current_handler: GetJsonSchemaHandler = current_handler,
  441. ) -> JsonSchemaValue:
  442. json_schema = current_handler(schema_or_field)
  443. if isinstance(js_extra, dict):
  444. json_schema.update(to_jsonable_python(js_extra))
  445. elif callable(js_extra):
  446. # similar to typing issue in _update_class_schema when we're working with callable js extra
  447. js_extra(json_schema) # type: ignore
  448. return json_schema
  449. current_handler = _schema_generation_shared.GenerateJsonSchemaHandler(self, js_extra_handler_func)
  450. for js_modify_function in metadata.get('pydantic_js_functions', ()):
  451. def new_handler_func(
  452. schema_or_field: CoreSchemaOrField,
  453. current_handler: GetJsonSchemaHandler = current_handler,
  454. js_modify_function: GetJsonSchemaFunction = js_modify_function,
  455. ) -> JsonSchemaValue:
  456. json_schema = js_modify_function(schema_or_field, current_handler)
  457. if _core_utils.is_core_schema(schema_or_field):
  458. json_schema = populate_defs(schema_or_field, json_schema)
  459. original_schema = current_handler.resolve_ref_schema(json_schema)
  460. ref = json_schema.pop('$ref', None)
  461. if ref and json_schema:
  462. original_schema.update(json_schema)
  463. return original_schema
  464. current_handler = _schema_generation_shared.GenerateJsonSchemaHandler(self, new_handler_func)
  465. for js_modify_function in metadata.get('pydantic_js_annotation_functions', ()):
  466. def new_handler_func(
  467. schema_or_field: CoreSchemaOrField,
  468. current_handler: GetJsonSchemaHandler = current_handler,
  469. js_modify_function: GetJsonSchemaFunction = js_modify_function,
  470. ) -> JsonSchemaValue:
  471. return js_modify_function(schema_or_field, current_handler)
  472. current_handler = _schema_generation_shared.GenerateJsonSchemaHandler(self, new_handler_func)
  473. json_schema = current_handler(schema)
  474. if _core_utils.is_core_schema(schema):
  475. json_schema = populate_defs(schema, json_schema)
  476. return json_schema
  477. def sort(self, value: JsonSchemaValue, parent_key: str | None = None) -> JsonSchemaValue:
  478. """Override this method to customize the sorting of the JSON schema (e.g., don't sort at all, sort all keys unconditionally, etc.)
  479. By default, alphabetically sort the keys in the JSON schema, skipping the 'properties' and 'default' keys to preserve field definition order.
  480. This sort is recursive, so it will sort all nested dictionaries as well.
  481. """
  482. sorted_dict: dict[str, JsonSchemaValue] = {}
  483. keys = value.keys()
  484. if parent_key not in ('properties', 'default'):
  485. keys = sorted(keys)
  486. for key in keys:
  487. sorted_dict[key] = self._sort_recursive(value[key], parent_key=key)
  488. return sorted_dict
  489. def _sort_recursive(self, value: Any, parent_key: str | None = None) -> Any:
  490. """Recursively sort a JSON schema value."""
  491. if isinstance(value, dict):
  492. sorted_dict: dict[str, JsonSchemaValue] = {}
  493. keys = value.keys()
  494. if parent_key not in ('properties', 'default'):
  495. keys = sorted(keys)
  496. for key in keys:
  497. sorted_dict[key] = self._sort_recursive(value[key], parent_key=key)
  498. return sorted_dict
  499. elif isinstance(value, list):
  500. sorted_list: list[JsonSchemaValue] = [self._sort_recursive(item, parent_key) for item in value]
  501. return sorted_list
  502. else:
  503. return value
  504. # ### Schema generation methods
  505. def invalid_schema(self, schema: core_schema.InvalidSchema) -> JsonSchemaValue:
  506. """Placeholder - should never be called."""
  507. raise RuntimeError('Cannot generate schema for invalid_schema. This is a bug! Please report it.')
  508. def any_schema(self, schema: core_schema.AnySchema) -> JsonSchemaValue:
  509. """Generates a JSON schema that matches any value.
  510. Args:
  511. schema: The core schema.
  512. Returns:
  513. The generated JSON schema.
  514. """
  515. return {}
  516. def none_schema(self, schema: core_schema.NoneSchema) -> JsonSchemaValue:
  517. """Generates a JSON schema that matches `None`.
  518. Args:
  519. schema: The core schema.
  520. Returns:
  521. The generated JSON schema.
  522. """
  523. return {'type': 'null'}
  524. def bool_schema(self, schema: core_schema.BoolSchema) -> JsonSchemaValue:
  525. """Generates a JSON schema that matches a bool value.
  526. Args:
  527. schema: The core schema.
  528. Returns:
  529. The generated JSON schema.
  530. """
  531. return {'type': 'boolean'}
  532. def int_schema(self, schema: core_schema.IntSchema) -> JsonSchemaValue:
  533. """Generates a JSON schema that matches an int value.
  534. Args:
  535. schema: The core schema.
  536. Returns:
  537. The generated JSON schema.
  538. """
  539. json_schema: dict[str, Any] = {'type': 'integer'}
  540. self.update_with_validations(json_schema, schema, self.ValidationsMapping.numeric)
  541. json_schema = {k: v for k, v in json_schema.items() if v not in {math.inf, -math.inf}}
  542. return json_schema
  543. def float_schema(self, schema: core_schema.FloatSchema) -> JsonSchemaValue:
  544. """Generates a JSON schema that matches a float value.
  545. Args:
  546. schema: The core schema.
  547. Returns:
  548. The generated JSON schema.
  549. """
  550. json_schema: dict[str, Any] = {'type': 'number'}
  551. self.update_with_validations(json_schema, schema, self.ValidationsMapping.numeric)
  552. json_schema = {k: v for k, v in json_schema.items() if v not in {math.inf, -math.inf}}
  553. return json_schema
  554. def decimal_schema(self, schema: core_schema.DecimalSchema) -> JsonSchemaValue:
  555. """Generates a JSON schema that matches a decimal value.
  556. Args:
  557. schema: The core schema.
  558. Returns:
  559. The generated JSON schema.
  560. """
  561. def get_decimal_pattern(schema: core_schema.DecimalSchema) -> str:
  562. max_digits = schema.get('max_digits')
  563. decimal_places = schema.get('decimal_places')
  564. pattern = (
  565. r'^(?!^[-+.]*$)[+-]?0*' # check it is not empty string and not one or sequence of ".+-" characters.
  566. )
  567. # Case 1: Both max_digits and decimal_places are set
  568. if max_digits is not None and decimal_places is not None:
  569. integer_places = max(0, max_digits - decimal_places)
  570. pattern += (
  571. rf'(?:'
  572. rf'\d{{0,{integer_places}}}'
  573. rf'|'
  574. rf'(?=[\d.]{{1,{max_digits + 1}}}0*$)'
  575. rf'\d{{0,{integer_places}}}\.\d{{0,{decimal_places}}}0*$'
  576. rf')'
  577. )
  578. # Case 2: Only max_digits is set
  579. elif max_digits is not None and decimal_places is None:
  580. pattern += (
  581. rf'(?:'
  582. rf'\d{{0,{max_digits}}}'
  583. rf'|'
  584. rf'(?=[\d.]{{1,{max_digits + 1}}}0*$)'
  585. rf'\d*\.\d*0*$'
  586. rf')'
  587. )
  588. # Case 3: Only decimal_places is set
  589. elif max_digits is None and decimal_places is not None:
  590. pattern += rf'\d*\.?\d{{0,{decimal_places}}}0*$'
  591. # Case 4: Both are None (no restrictions)
  592. else:
  593. pattern += r'\d*\.?\d*$' # look for arbitrary integer or decimal
  594. return pattern
  595. json_schema = self.str_schema(core_schema.str_schema(pattern=get_decimal_pattern(schema)))
  596. if self.mode == 'validation':
  597. multiple_of = schema.get('multiple_of')
  598. le = schema.get('le')
  599. ge = schema.get('ge')
  600. lt = schema.get('lt')
  601. gt = schema.get('gt')
  602. json_schema = {
  603. 'anyOf': [
  604. self.float_schema(
  605. core_schema.float_schema(
  606. allow_inf_nan=schema.get('allow_inf_nan'),
  607. multiple_of=None if multiple_of is None else float(multiple_of),
  608. le=None if le is None else float(le),
  609. ge=None if ge is None else float(ge),
  610. lt=None if lt is None else float(lt),
  611. gt=None if gt is None else float(gt),
  612. )
  613. ),
  614. json_schema,
  615. ],
  616. }
  617. return json_schema
  618. def str_schema(self, schema: core_schema.StringSchema) -> JsonSchemaValue:
  619. """Generates a JSON schema that matches a string value.
  620. Args:
  621. schema: The core schema.
  622. Returns:
  623. The generated JSON schema.
  624. """
  625. json_schema = {'type': 'string'}
  626. self.update_with_validations(json_schema, schema, self.ValidationsMapping.string)
  627. if isinstance(json_schema.get('pattern'), Pattern):
  628. # TODO: should we add regex flags to the pattern?
  629. json_schema['pattern'] = json_schema.get('pattern').pattern # type: ignore
  630. return json_schema
  631. def bytes_schema(self, schema: core_schema.BytesSchema) -> JsonSchemaValue:
  632. """Generates a JSON schema that matches a bytes value.
  633. Args:
  634. schema: The core schema.
  635. Returns:
  636. The generated JSON schema.
  637. """
  638. json_schema = {'type': 'string', 'format': 'base64url' if self._config.ser_json_bytes == 'base64' else 'binary'}
  639. self.update_with_validations(json_schema, schema, self.ValidationsMapping.bytes)
  640. return json_schema
  641. def date_schema(self, schema: core_schema.DateSchema) -> JsonSchemaValue:
  642. """Generates a JSON schema that matches a date value.
  643. Args:
  644. schema: The core schema.
  645. Returns:
  646. The generated JSON schema.
  647. """
  648. return {'type': 'string', 'format': 'date'}
  649. def time_schema(self, schema: core_schema.TimeSchema) -> JsonSchemaValue:
  650. """Generates a JSON schema that matches a time value.
  651. Args:
  652. schema: The core schema.
  653. Returns:
  654. The generated JSON schema.
  655. """
  656. return {'type': 'string', 'format': 'time'}
  657. def datetime_schema(self, schema: core_schema.DatetimeSchema) -> JsonSchemaValue:
  658. """Generates a JSON schema that matches a datetime value.
  659. Args:
  660. schema: The core schema.
  661. Returns:
  662. The generated JSON schema.
  663. """
  664. return {'type': 'string', 'format': 'date-time'}
  665. def timedelta_schema(self, schema: core_schema.TimedeltaSchema) -> JsonSchemaValue:
  666. """Generates a JSON schema that matches a timedelta value.
  667. Args:
  668. schema: The core schema.
  669. Returns:
  670. The generated JSON schema.
  671. """
  672. if self._config.ser_json_timedelta == 'float':
  673. return {'type': 'number'}
  674. return {'type': 'string', 'format': 'duration'}
  675. def literal_schema(self, schema: core_schema.LiteralSchema) -> JsonSchemaValue:
  676. """Generates a JSON schema that matches a literal value.
  677. Args:
  678. schema: The core schema.
  679. Returns:
  680. The generated JSON schema.
  681. """
  682. expected = [to_jsonable_python(v.value if isinstance(v, Enum) else v) for v in schema['expected']]
  683. result: dict[str, Any] = {}
  684. if len(expected) == 1:
  685. result['const'] = expected[0]
  686. else:
  687. result['enum'] = expected
  688. types = {type(e) for e in expected}
  689. if types == {str}:
  690. result['type'] = 'string'
  691. elif types == {int}:
  692. result['type'] = 'integer'
  693. elif types == {float}:
  694. result['type'] = 'number'
  695. elif types == {bool}:
  696. result['type'] = 'boolean'
  697. elif types == {list}:
  698. result['type'] = 'array'
  699. elif types == {type(None)}:
  700. result['type'] = 'null'
  701. return result
  702. def missing_sentinel_schema(self, schema: core_schema.MissingSentinelSchema) -> JsonSchemaValue:
  703. """Generates a JSON schema that matches the `MISSING` sentinel value.
  704. Args:
  705. schema: The core schema.
  706. Returns:
  707. The generated JSON schema.
  708. """
  709. raise PydanticOmit
  710. def enum_schema(self, schema: core_schema.EnumSchema) -> JsonSchemaValue:
  711. """Generates a JSON schema that matches an Enum value.
  712. Args:
  713. schema: The core schema.
  714. Returns:
  715. The generated JSON schema.
  716. """
  717. enum_type = schema['cls']
  718. description = None if not enum_type.__doc__ else inspect.cleandoc(enum_type.__doc__)
  719. if (
  720. description == 'An enumeration.'
  721. ): # This is the default value provided by enum.EnumMeta.__new__; don't use it
  722. description = None
  723. result: dict[str, Any] = {'title': enum_type.__name__, 'description': description}
  724. result = {k: v for k, v in result.items() if v is not None}
  725. expected = [to_jsonable_python(v.value) for v in schema['members']]
  726. result['enum'] = expected
  727. types = {type(e) for e in expected}
  728. if isinstance(enum_type, str) or types == {str}:
  729. result['type'] = 'string'
  730. elif isinstance(enum_type, int) or types == {int}:
  731. result['type'] = 'integer'
  732. elif isinstance(enum_type, float) or types == {float}:
  733. result['type'] = 'number'
  734. elif types == {bool}:
  735. result['type'] = 'boolean'
  736. elif types == {list}:
  737. result['type'] = 'array'
  738. return result
  739. def is_instance_schema(self, schema: core_schema.IsInstanceSchema) -> JsonSchemaValue:
  740. """Handles JSON schema generation for a core schema that checks if a value is an instance of a class.
  741. Unless overridden in a subclass, this raises an error.
  742. Args:
  743. schema: The core schema.
  744. Returns:
  745. The generated JSON schema.
  746. """
  747. return self.handle_invalid_for_json_schema(schema, f'core_schema.IsInstanceSchema ({schema["cls"]})')
  748. def is_subclass_schema(self, schema: core_schema.IsSubclassSchema) -> JsonSchemaValue:
  749. """Handles JSON schema generation for a core schema that checks if a value is a subclass of a class.
  750. For backwards compatibility with v1, this does not raise an error, but can be overridden to change this.
  751. Args:
  752. schema: The core schema.
  753. Returns:
  754. The generated JSON schema.
  755. """
  756. # Note: This is for compatibility with V1; you can override if you want different behavior.
  757. return {}
  758. def callable_schema(self, schema: core_schema.CallableSchema) -> JsonSchemaValue:
  759. """Generates a JSON schema that matches a callable value.
  760. Unless overridden in a subclass, this raises an error.
  761. Args:
  762. schema: The core schema.
  763. Returns:
  764. The generated JSON schema.
  765. """
  766. return self.handle_invalid_for_json_schema(schema, 'core_schema.CallableSchema')
  767. def list_schema(self, schema: core_schema.ListSchema) -> JsonSchemaValue:
  768. """Returns a schema that matches a list schema.
  769. Args:
  770. schema: The core schema.
  771. Returns:
  772. The generated JSON schema.
  773. """
  774. items_schema = {} if 'items_schema' not in schema else self.generate_inner(schema['items_schema'])
  775. json_schema = {'type': 'array', 'items': items_schema}
  776. self.update_with_validations(json_schema, schema, self.ValidationsMapping.array)
  777. return json_schema
  778. @deprecated('`tuple_positional_schema` is deprecated. Use `tuple_schema` instead.', category=None)
  779. @final
  780. def tuple_positional_schema(self, schema: core_schema.TupleSchema) -> JsonSchemaValue:
  781. """Replaced by `tuple_schema`."""
  782. warnings.warn(
  783. '`tuple_positional_schema` is deprecated. Use `tuple_schema` instead.',
  784. PydanticDeprecatedSince26,
  785. stacklevel=2,
  786. )
  787. return self.tuple_schema(schema)
  788. @deprecated('`tuple_variable_schema` is deprecated. Use `tuple_schema` instead.', category=None)
  789. @final
  790. def tuple_variable_schema(self, schema: core_schema.TupleSchema) -> JsonSchemaValue:
  791. """Replaced by `tuple_schema`."""
  792. warnings.warn(
  793. '`tuple_variable_schema` is deprecated. Use `tuple_schema` instead.',
  794. PydanticDeprecatedSince26,
  795. stacklevel=2,
  796. )
  797. return self.tuple_schema(schema)
  798. def tuple_schema(self, schema: core_schema.TupleSchema) -> JsonSchemaValue:
  799. """Generates a JSON schema that matches a tuple schema e.g. `tuple[int,
  800. str, bool]` or `tuple[int, ...]`.
  801. Args:
  802. schema: The core schema.
  803. Returns:
  804. The generated JSON schema.
  805. """
  806. json_schema: JsonSchemaValue = {'type': 'array'}
  807. if 'variadic_item_index' in schema:
  808. variadic_item_index = schema['variadic_item_index']
  809. if variadic_item_index > 0:
  810. json_schema['minItems'] = variadic_item_index
  811. json_schema['prefixItems'] = [
  812. self.generate_inner(item) for item in schema['items_schema'][:variadic_item_index]
  813. ]
  814. if variadic_item_index + 1 == len(schema['items_schema']):
  815. # if the variadic item is the last item, then represent it faithfully
  816. json_schema['items'] = self.generate_inner(schema['items_schema'][variadic_item_index])
  817. else:
  818. # otherwise, 'items' represents the schema for the variadic
  819. # item plus the suffix, so just allow anything for simplicity
  820. # for now
  821. json_schema['items'] = True
  822. else:
  823. prefixItems = [self.generate_inner(item) for item in schema['items_schema']]
  824. if prefixItems:
  825. json_schema['prefixItems'] = prefixItems
  826. json_schema['minItems'] = len(prefixItems)
  827. json_schema['maxItems'] = len(prefixItems)
  828. self.update_with_validations(json_schema, schema, self.ValidationsMapping.array)
  829. return json_schema
  830. def set_schema(self, schema: core_schema.SetSchema) -> JsonSchemaValue:
  831. """Generates a JSON schema that matches a set schema.
  832. Args:
  833. schema: The core schema.
  834. Returns:
  835. The generated JSON schema.
  836. """
  837. return self._common_set_schema(schema)
  838. def frozenset_schema(self, schema: core_schema.FrozenSetSchema) -> JsonSchemaValue:
  839. """Generates a JSON schema that matches a frozenset schema.
  840. Args:
  841. schema: The core schema.
  842. Returns:
  843. The generated JSON schema.
  844. """
  845. return self._common_set_schema(schema)
  846. def _common_set_schema(self, schema: core_schema.SetSchema | core_schema.FrozenSetSchema) -> JsonSchemaValue:
  847. items_schema = {} if 'items_schema' not in schema else self.generate_inner(schema['items_schema'])
  848. json_schema = {'type': 'array', 'uniqueItems': True, 'items': items_schema}
  849. self.update_with_validations(json_schema, schema, self.ValidationsMapping.array)
  850. return json_schema
  851. def generator_schema(self, schema: core_schema.GeneratorSchema) -> JsonSchemaValue:
  852. """Returns a JSON schema that represents the provided GeneratorSchema.
  853. Args:
  854. schema: The schema.
  855. Returns:
  856. The generated JSON schema.
  857. """
  858. items_schema = {} if 'items_schema' not in schema else self.generate_inner(schema['items_schema'])
  859. json_schema = {'type': 'array', 'items': items_schema}
  860. self.update_with_validations(json_schema, schema, self.ValidationsMapping.array)
  861. return json_schema
  862. def dict_schema(self, schema: core_schema.DictSchema) -> JsonSchemaValue:
  863. """Generates a JSON schema that matches a dict schema.
  864. Args:
  865. schema: The core schema.
  866. Returns:
  867. The generated JSON schema.
  868. """
  869. json_schema: JsonSchemaValue = {'type': 'object'}
  870. keys_schema = self.generate_inner(schema['keys_schema']).copy() if 'keys_schema' in schema else {}
  871. if '$ref' not in keys_schema:
  872. keys_pattern = keys_schema.pop('pattern', None)
  873. # Don't give a title to patternProperties/propertyNames:
  874. keys_schema.pop('title', None)
  875. else:
  876. # Here, we assume that if the keys schema is a definition reference,
  877. # it can't be a simple string core schema (and thus no pattern can exist).
  878. # However, this is only in practice (in theory, a definition reference core
  879. # schema could be generated for a simple string schema).
  880. # Note that we avoid calling `self.resolve_ref_schema`, as it might not exist yet.
  881. keys_pattern = None
  882. values_schema = self.generate_inner(schema['values_schema']).copy() if 'values_schema' in schema else {}
  883. # don't give a title to additionalProperties:
  884. values_schema.pop('title', None)
  885. if values_schema or keys_pattern is not None:
  886. if keys_pattern is None:
  887. json_schema['additionalProperties'] = values_schema
  888. else:
  889. json_schema['patternProperties'] = {keys_pattern: values_schema}
  890. else: # for `dict[str, Any]`, we allow any key and any value, since `str` is the default key type
  891. json_schema['additionalProperties'] = True
  892. if (
  893. # The len check indicates that constraints are probably present:
  894. (keys_schema.get('type') == 'string' and len(keys_schema) > 1)
  895. # If this is a definition reference schema, it most likely has constraints:
  896. or '$ref' in keys_schema
  897. ):
  898. keys_schema.pop('type', None)
  899. json_schema['propertyNames'] = keys_schema
  900. self.update_with_validations(json_schema, schema, self.ValidationsMapping.object)
  901. return json_schema
  902. def function_before_schema(self, schema: core_schema.BeforeValidatorFunctionSchema) -> JsonSchemaValue:
  903. """Generates a JSON schema that matches a function-before schema.
  904. Args:
  905. schema: The core schema.
  906. Returns:
  907. The generated JSON schema.
  908. """
  909. if self.mode == 'validation' and (input_schema := schema.get('json_schema_input_schema')):
  910. return self.generate_inner(input_schema)
  911. return self.generate_inner(schema['schema'])
  912. def function_after_schema(self, schema: core_schema.AfterValidatorFunctionSchema) -> JsonSchemaValue:
  913. """Generates a JSON schema that matches a function-after schema.
  914. Args:
  915. schema: The core schema.
  916. Returns:
  917. The generated JSON schema.
  918. """
  919. return self.generate_inner(schema['schema'])
  920. def function_plain_schema(self, schema: core_schema.PlainValidatorFunctionSchema) -> JsonSchemaValue:
  921. """Generates a JSON schema that matches a function-plain schema.
  922. Args:
  923. schema: The core schema.
  924. Returns:
  925. The generated JSON schema.
  926. """
  927. if self.mode == 'validation' and (input_schema := schema.get('json_schema_input_schema')):
  928. return self.generate_inner(input_schema)
  929. return self.handle_invalid_for_json_schema(
  930. schema, f'core_schema.PlainValidatorFunctionSchema ({schema["function"]})'
  931. )
  932. def function_wrap_schema(self, schema: core_schema.WrapValidatorFunctionSchema) -> JsonSchemaValue:
  933. """Generates a JSON schema that matches a function-wrap schema.
  934. Args:
  935. schema: The core schema.
  936. Returns:
  937. The generated JSON schema.
  938. """
  939. if self.mode == 'validation' and (input_schema := schema.get('json_schema_input_schema')):
  940. return self.generate_inner(input_schema)
  941. return self.generate_inner(schema['schema'])
  942. def default_schema(self, schema: core_schema.WithDefaultSchema) -> JsonSchemaValue:
  943. """Generates a JSON schema that matches a schema with a default value.
  944. Args:
  945. schema: The core schema.
  946. Returns:
  947. The generated JSON schema.
  948. """
  949. json_schema = self.generate_inner(schema['schema'])
  950. default = self.get_default_value(schema)
  951. if default is NoDefault or default is MISSING:
  952. return json_schema
  953. # we reflect the application of custom plain, no-info serializers to defaults for
  954. # JSON Schemas viewed in serialization mode:
  955. # TODO: improvements along with https://github.com/pydantic/pydantic/issues/8208
  956. if self.mode == 'serialization':
  957. # `_get_ser_schema_for_default_value()` is used to unpack potentially nested validator schemas:
  958. ser_schema = _get_ser_schema_for_default_value(schema['schema'])
  959. if (
  960. ser_schema is not None
  961. and (ser_func := ser_schema.get('function'))
  962. and not (default is None and ser_schema.get('when_used') in ('unless-none', 'json-unless-none'))
  963. ):
  964. try:
  965. default = ser_func(default) # type: ignore
  966. except Exception:
  967. # It might be that the provided default needs to be validated (read: parsed) first
  968. # (assuming `validate_default` is enabled). However, we can't perform
  969. # such validation during JSON Schema generation so we don't support
  970. # this pattern for now.
  971. # (One example is when using `foo: ByteSize = '1MB'`, which validates and
  972. # serializes as an int. In this case, `ser_func` is `int` and `int('1MB')` fails).
  973. self.emit_warning(
  974. 'non-serializable-default',
  975. f'Unable to serialize value {default!r} with the plain serializer; excluding default from JSON schema',
  976. )
  977. return json_schema
  978. try:
  979. encoded_default = self.encode_default(default)
  980. except pydantic_core.PydanticSerializationError:
  981. self.emit_warning(
  982. 'non-serializable-default',
  983. f'Default value {default} is not JSON serializable; excluding default from JSON schema',
  984. )
  985. # Return the inner schema, as though there was no default
  986. return json_schema
  987. json_schema['default'] = encoded_default
  988. return json_schema
  989. def get_default_value(self, schema: core_schema.WithDefaultSchema) -> Any:
  990. """Get the default value to be used when generating a JSON Schema for a core schema with a default.
  991. The default implementation is to use the statically defined default value. This method can be overridden
  992. if you want to make use of the default factory.
  993. Args:
  994. schema: The `'with-default'` core schema.
  995. Returns:
  996. The default value to use, or [`NoDefault`][pydantic.json_schema.NoDefault] if no default
  997. value is available.
  998. """
  999. return schema.get('default', NoDefault)
  1000. def nullable_schema(self, schema: core_schema.NullableSchema) -> JsonSchemaValue:
  1001. """Generates a JSON schema that matches a schema that allows null values.
  1002. Args:
  1003. schema: The core schema.
  1004. Returns:
  1005. The generated JSON schema.
  1006. """
  1007. null_schema = {'type': 'null'}
  1008. inner_json_schema = self.generate_inner(schema['schema'])
  1009. if inner_json_schema == null_schema:
  1010. return null_schema
  1011. else:
  1012. return self.get_union_of_schemas([inner_json_schema, null_schema])
  1013. def union_schema(self, schema: core_schema.UnionSchema) -> JsonSchemaValue:
  1014. """Generates a JSON schema that matches a schema that allows values matching any of the given schemas.
  1015. Args:
  1016. schema: The core schema.
  1017. Returns:
  1018. The generated JSON schema.
  1019. """
  1020. generated: list[JsonSchemaValue] = []
  1021. choices = schema['choices']
  1022. for choice in choices:
  1023. # choice will be a tuple if an explicit label was provided
  1024. choice_schema = choice[0] if isinstance(choice, tuple) else choice
  1025. try:
  1026. generated.append(self.generate_inner(choice_schema))
  1027. except PydanticOmit:
  1028. continue
  1029. except PydanticInvalidForJsonSchema as exc:
  1030. self.emit_warning('skipped-choice', exc.message)
  1031. if len(generated) == 1:
  1032. return generated[0]
  1033. return self.get_union_of_schemas(generated)
  1034. def get_union_of_schemas(self, schemas: list[JsonSchemaValue]) -> JsonSchemaValue:
  1035. """Returns the JSON Schema representation for the union of the provided JSON Schemas.
  1036. The result depends on the configured `'union_format'`.
  1037. Args:
  1038. schemas: The list of JSON Schemas to be included in the union.
  1039. Returns:
  1040. The JSON Schema representing the union of schemas.
  1041. """
  1042. if self.union_format == 'primitive_type_array':
  1043. types: list[str] = []
  1044. for schema in schemas:
  1045. schema_types: list[str] | str | None = schema.get('type')
  1046. if schema_types is None:
  1047. # No type, meaning it can be a ref or an empty schema.
  1048. break
  1049. if not isinstance(schema_types, list):
  1050. schema_types = [schema_types]
  1051. if not all(t in _PRIMITIVE_JSON_SCHEMA_TYPES for t in schema_types):
  1052. break
  1053. if len(schema) != 1:
  1054. # We only want to include types that don't have any constraints. For instance,
  1055. # if `schemas = [{'type': 'string', 'maxLength': 3}, {'type': 'string', 'minLength': 5}]`,
  1056. # we don't want to produce `{'type': 'string', 'maxLength': 3, 'minLength': 5}`.
  1057. # Same if we have some metadata (e.g. `title`) on a specific union member, we want to preserve it.
  1058. break
  1059. types.extend(schema_types)
  1060. else:
  1061. # If we got there, all the schemas where valid to be used with the `'primitive_type_array` format
  1062. return {'type': list(dict.fromkeys(types))}
  1063. return self.get_flattened_anyof(schemas)
  1064. def tagged_union_schema(self, schema: core_schema.TaggedUnionSchema) -> JsonSchemaValue:
  1065. """Generates a JSON schema that matches a schema that allows values matching any of the given schemas, where
  1066. the schemas are tagged with a discriminator field that indicates which schema should be used to validate
  1067. the value.
  1068. Args:
  1069. schema: The core schema.
  1070. Returns:
  1071. The generated JSON schema.
  1072. """
  1073. generated: dict[str, JsonSchemaValue] = {}
  1074. for k, v in schema['choices'].items():
  1075. if isinstance(k, Enum):
  1076. k = k.value
  1077. try:
  1078. # Use str(k) since keys must be strings for json; while not technically correct,
  1079. # it's the closest that can be represented in valid JSON
  1080. generated[str(k)] = self.generate_inner(v).copy()
  1081. except PydanticOmit:
  1082. continue
  1083. except PydanticInvalidForJsonSchema as exc:
  1084. self.emit_warning('skipped-choice', exc.message)
  1085. one_of_choices = _deduplicate_schemas(generated.values())
  1086. json_schema: JsonSchemaValue = {'oneOf': one_of_choices}
  1087. # This reflects the v1 behavior; TODO: we should make it possible to exclude OpenAPI stuff from the JSON schema
  1088. openapi_discriminator = self._extract_discriminator(schema, one_of_choices)
  1089. if openapi_discriminator is not None:
  1090. json_schema['discriminator'] = {
  1091. 'propertyName': openapi_discriminator,
  1092. 'mapping': {k: v.get('$ref', v) for k, v in generated.items()},
  1093. }
  1094. return json_schema
  1095. def _extract_discriminator(
  1096. self, schema: core_schema.TaggedUnionSchema, one_of_choices: list[JsonDict]
  1097. ) -> str | None:
  1098. """Extract a compatible OpenAPI discriminator from the schema and one_of choices that end up in the final
  1099. schema."""
  1100. openapi_discriminator: str | None = None
  1101. if isinstance(schema['discriminator'], str):
  1102. return schema['discriminator']
  1103. if isinstance(schema['discriminator'], list):
  1104. # If the discriminator is a single item list containing a string, that is equivalent to the string case
  1105. if len(schema['discriminator']) == 1 and isinstance(schema['discriminator'][0], str):
  1106. return schema['discriminator'][0]
  1107. # When an alias is used that is different from the field name, the discriminator will be a list of single
  1108. # str lists, one for the attribute and one for the actual alias. The logic here will work even if there is
  1109. # more than one possible attribute, and looks for whether a single alias choice is present as a documented
  1110. # property on all choices. If so, that property will be used as the OpenAPI discriminator.
  1111. for alias_path in schema['discriminator']:
  1112. if not isinstance(alias_path, list):
  1113. break # this means that the discriminator is not a list of alias paths
  1114. if len(alias_path) != 1:
  1115. continue # this means that the "alias" does not represent a single field
  1116. alias = alias_path[0]
  1117. if not isinstance(alias, str):
  1118. continue # this means that the "alias" does not represent a field
  1119. alias_is_present_on_all_choices = True
  1120. for choice in one_of_choices:
  1121. try:
  1122. choice = self.resolve_ref_schema(choice)
  1123. except RuntimeError as exc:
  1124. # TODO: fixme - this is a workaround for the fact that we can't always resolve refs
  1125. # for tagged union choices at this point in the schema gen process, we might need to do
  1126. # another pass at the end like we do for core schemas
  1127. self.emit_warning('skipped-discriminator', str(exc))
  1128. choice = {}
  1129. properties = choice.get('properties', {})
  1130. if not isinstance(properties, dict) or alias not in properties:
  1131. alias_is_present_on_all_choices = False
  1132. break
  1133. if alias_is_present_on_all_choices:
  1134. openapi_discriminator = alias
  1135. break
  1136. return openapi_discriminator
  1137. def chain_schema(self, schema: core_schema.ChainSchema) -> JsonSchemaValue:
  1138. """Generates a JSON schema that matches a core_schema.ChainSchema.
  1139. When generating a schema for validation, we return the validation JSON schema for the first step in the chain.
  1140. For serialization, we return the serialization JSON schema for the last step in the chain.
  1141. Args:
  1142. schema: The core schema.
  1143. Returns:
  1144. The generated JSON schema.
  1145. """
  1146. step_index = 0 if self.mode == 'validation' else -1 # use first step for validation, last for serialization
  1147. return self.generate_inner(schema['steps'][step_index])
  1148. def lax_or_strict_schema(self, schema: core_schema.LaxOrStrictSchema) -> JsonSchemaValue:
  1149. """Generates a JSON schema that matches a schema that allows values matching either the lax schema or the
  1150. strict schema.
  1151. Args:
  1152. schema: The core schema.
  1153. Returns:
  1154. The generated JSON schema.
  1155. """
  1156. # TODO: Need to read the default value off of model config or whatever
  1157. use_strict = schema.get('strict', False) # TODO: replace this default False
  1158. # If your JSON schema fails to generate it is probably
  1159. # because one of the following two branches failed.
  1160. if use_strict:
  1161. return self.generate_inner(schema['strict_schema'])
  1162. else:
  1163. return self.generate_inner(schema['lax_schema'])
  1164. def json_or_python_schema(self, schema: core_schema.JsonOrPythonSchema) -> JsonSchemaValue:
  1165. """Generates a JSON schema that matches a schema that allows values matching either the JSON schema or the
  1166. Python schema.
  1167. The JSON schema is used instead of the Python schema. If you want to use the Python schema, you should override
  1168. this method.
  1169. Args:
  1170. schema: The core schema.
  1171. Returns:
  1172. The generated JSON schema.
  1173. """
  1174. return self.generate_inner(schema['json_schema'])
  1175. def typed_dict_schema(self, schema: core_schema.TypedDictSchema) -> JsonSchemaValue:
  1176. """Generates a JSON schema that matches a schema that defines a typed dict.
  1177. Args:
  1178. schema: The core schema.
  1179. Returns:
  1180. The generated JSON schema.
  1181. """
  1182. total = schema.get('total', True)
  1183. named_required_fields: list[tuple[str, bool, CoreSchemaField]] = [
  1184. (name, self.field_is_required(field, total), field)
  1185. for name, field in schema['fields'].items()
  1186. if self.field_is_present(field)
  1187. ]
  1188. if self.mode == 'serialization':
  1189. named_required_fields.extend(self._name_required_computed_fields(schema.get('computed_fields', [])))
  1190. cls = schema.get('cls')
  1191. config = _get_typed_dict_config(cls)
  1192. with self._config_wrapper_stack.push(config):
  1193. json_schema = self._named_required_fields_schema(named_required_fields)
  1194. # There's some duplication between `extra_behavior` and
  1195. # the config's `extra`/core config's `extra_fields_behavior`.
  1196. # However, it is common to manually create TypedDictSchemas,
  1197. # where you don't necessarily have a class.
  1198. # At runtime, `extra_behavior` takes priority over the config
  1199. # for validation, so follow the same for the JSON Schema:
  1200. if schema.get('extra_behavior') == 'forbid':
  1201. json_schema['additionalProperties'] = False
  1202. elif schema.get('extra_behavior') == 'allow':
  1203. if 'extras_schema' in schema and schema['extras_schema'] != {'type': 'any'}:
  1204. json_schema['additionalProperties'] = self.generate_inner(schema['extras_schema'])
  1205. else:
  1206. json_schema['additionalProperties'] = True
  1207. if cls is not None:
  1208. # `_update_class_schema()` will not override
  1209. # `additionalProperties` if already present:
  1210. self._update_class_schema(json_schema, cls, config)
  1211. elif 'additionalProperties' not in json_schema:
  1212. extra = schema.get('config', {}).get('extra_fields_behavior')
  1213. if extra == 'forbid':
  1214. json_schema['additionalProperties'] = False
  1215. elif extra == 'allow':
  1216. json_schema['additionalProperties'] = True
  1217. return json_schema
  1218. @staticmethod
  1219. def _name_required_computed_fields(
  1220. computed_fields: list[ComputedField],
  1221. ) -> list[tuple[str, bool, core_schema.ComputedField]]:
  1222. return [(field['property_name'], True, field) for field in computed_fields]
  1223. def _named_required_fields_schema(
  1224. self, named_required_fields: Sequence[tuple[str, bool, CoreSchemaField]]
  1225. ) -> JsonSchemaValue:
  1226. properties: dict[str, JsonSchemaValue] = {}
  1227. required_fields: list[str] = []
  1228. for name, required, field in named_required_fields:
  1229. if self.by_alias:
  1230. name = self._get_alias_name(field, name)
  1231. try:
  1232. field_json_schema = self.generate_inner(field).copy()
  1233. except PydanticOmit:
  1234. continue
  1235. if 'title' not in field_json_schema and self.field_title_should_be_set(field):
  1236. title = self.get_title_from_name(name)
  1237. field_json_schema['title'] = title
  1238. field_json_schema = self.handle_ref_overrides(field_json_schema)
  1239. properties[name] = field_json_schema
  1240. if required:
  1241. required_fields.append(name)
  1242. json_schema = {'type': 'object', 'properties': properties}
  1243. if required_fields:
  1244. json_schema['required'] = required_fields
  1245. return json_schema
  1246. def _get_alias_name(self, field: CoreSchemaField, name: str) -> str:
  1247. if field['type'] == 'computed-field':
  1248. alias: Any = field.get('alias', name)
  1249. elif self.mode == 'validation':
  1250. alias = field.get('validation_alias', name)
  1251. else:
  1252. alias = field.get('serialization_alias', name)
  1253. if isinstance(alias, str):
  1254. name = alias
  1255. elif isinstance(alias, list):
  1256. alias = cast('list[str] | str', alias)
  1257. for path in alias:
  1258. if isinstance(path, list) and len(path) == 1 and isinstance(path[0], str):
  1259. # Use the first valid single-item string path; the code that constructs the alias array
  1260. # should ensure the first such item is what belongs in the JSON schema
  1261. name = path[0]
  1262. break
  1263. else:
  1264. assert_never(alias)
  1265. return name
  1266. def typed_dict_field_schema(self, schema: core_schema.TypedDictField) -> JsonSchemaValue:
  1267. """Generates a JSON schema that matches a schema that defines a typed dict field.
  1268. Args:
  1269. schema: The core schema.
  1270. Returns:
  1271. The generated JSON schema.
  1272. """
  1273. return self.generate_inner(schema['schema'])
  1274. def dataclass_field_schema(self, schema: core_schema.DataclassField) -> JsonSchemaValue:
  1275. """Generates a JSON schema that matches a schema that defines a dataclass field.
  1276. Args:
  1277. schema: The core schema.
  1278. Returns:
  1279. The generated JSON schema.
  1280. """
  1281. return self.generate_inner(schema['schema'])
  1282. def model_field_schema(self, schema: core_schema.ModelField) -> JsonSchemaValue:
  1283. """Generates a JSON schema that matches a schema that defines a model field.
  1284. Args:
  1285. schema: The core schema.
  1286. Returns:
  1287. The generated JSON schema.
  1288. """
  1289. return self.generate_inner(schema['schema'])
  1290. def computed_field_schema(self, schema: core_schema.ComputedField) -> JsonSchemaValue:
  1291. """Generates a JSON schema that matches a schema that defines a computed field.
  1292. Args:
  1293. schema: The core schema.
  1294. Returns:
  1295. The generated JSON schema.
  1296. """
  1297. return self.generate_inner(schema['return_schema'])
  1298. def model_schema(self, schema: core_schema.ModelSchema) -> JsonSchemaValue:
  1299. """Generates a JSON schema that matches a schema that defines a model.
  1300. Args:
  1301. schema: The core schema.
  1302. Returns:
  1303. The generated JSON schema.
  1304. """
  1305. # We do not use schema['model'].model_json_schema() here
  1306. # because it could lead to inconsistent refs handling, etc.
  1307. cls = cast('type[BaseModel]', schema['cls'])
  1308. config = cls.model_config
  1309. with self._config_wrapper_stack.push(config):
  1310. json_schema = self.generate_inner(schema['schema'])
  1311. self._update_class_schema(json_schema, cls, config)
  1312. return json_schema
  1313. def _update_class_schema(self, json_schema: JsonSchemaValue, cls: type[Any], config: ConfigDict) -> None:
  1314. """Update json_schema with the following, extracted from `config` and `cls`:
  1315. * title
  1316. * description
  1317. * additional properties
  1318. * json_schema_extra
  1319. * deprecated
  1320. Done in place, hence there's no return value as the original json_schema is mutated.
  1321. No ref resolving is involved here, as that's not appropriate for simple updates.
  1322. """
  1323. from .main import BaseModel
  1324. from .root_model import RootModel
  1325. if (config_title := config.get('title')) is not None:
  1326. json_schema.setdefault('title', config_title)
  1327. elif model_title_generator := config.get('model_title_generator'):
  1328. title = model_title_generator(cls)
  1329. if not isinstance(title, str):
  1330. raise TypeError(f'model_title_generator {model_title_generator} must return str, not {title.__class__}')
  1331. json_schema.setdefault('title', title)
  1332. if 'title' not in json_schema:
  1333. json_schema['title'] = cls.__name__
  1334. # BaseModel and dataclasses; don't use cls.__doc__ as it will contain the verbose class signature by default
  1335. docstring = None if cls is BaseModel or dataclasses.is_dataclass(cls) else cls.__doc__
  1336. if docstring:
  1337. json_schema.setdefault('description', inspect.cleandoc(docstring))
  1338. elif issubclass(cls, RootModel) and (root_description := cls.__pydantic_fields__['root'].description):
  1339. json_schema.setdefault('description', root_description)
  1340. extra = config.get('extra')
  1341. if 'additionalProperties' not in json_schema: # This check is particularly important for `typed_dict_schema()`
  1342. if extra == 'allow':
  1343. json_schema['additionalProperties'] = True
  1344. elif extra == 'forbid':
  1345. json_schema['additionalProperties'] = False
  1346. json_schema_extra = config.get('json_schema_extra')
  1347. if issubclass(cls, BaseModel) and cls.__pydantic_root_model__:
  1348. root_json_schema_extra = cls.model_fields['root'].json_schema_extra
  1349. if json_schema_extra and root_json_schema_extra:
  1350. raise ValueError(
  1351. '"model_config[\'json_schema_extra\']" and "Field.json_schema_extra" on "RootModel.root"'
  1352. ' field must not be set simultaneously'
  1353. )
  1354. if root_json_schema_extra:
  1355. json_schema_extra = root_json_schema_extra
  1356. if isinstance(json_schema_extra, (staticmethod, classmethod)):
  1357. # In older versions of python, this is necessary to ensure staticmethod/classmethods are callable
  1358. json_schema_extra = json_schema_extra.__get__(cls)
  1359. if isinstance(json_schema_extra, dict):
  1360. json_schema.update(json_schema_extra)
  1361. elif callable(json_schema_extra):
  1362. # FIXME: why are there type ignores here? We support two signatures for json_schema_extra callables...
  1363. if len(inspect.signature(json_schema_extra).parameters) > 1:
  1364. json_schema_extra(json_schema, cls) # type: ignore
  1365. else:
  1366. json_schema_extra(json_schema) # type: ignore
  1367. elif json_schema_extra is not None:
  1368. raise ValueError(
  1369. f"model_config['json_schema_extra']={json_schema_extra} should be a dict, callable, or None"
  1370. )
  1371. if hasattr(cls, '__deprecated__'):
  1372. json_schema['deprecated'] = True
  1373. def resolve_ref_schema(self, json_schema: JsonSchemaValue) -> JsonSchemaValue:
  1374. """Resolve a JsonSchemaValue to the non-ref schema if it is a $ref schema.
  1375. Args:
  1376. json_schema: The schema to resolve.
  1377. Returns:
  1378. The resolved schema.
  1379. Raises:
  1380. RuntimeError: If the schema reference can't be found in definitions.
  1381. """
  1382. while '$ref' in json_schema:
  1383. ref = json_schema['$ref']
  1384. schema_to_update = self.get_schema_from_definitions(JsonRef(ref))
  1385. if schema_to_update is None:
  1386. raise RuntimeError(f'Cannot update undefined schema for $ref={ref}')
  1387. json_schema = schema_to_update
  1388. return json_schema
  1389. def model_fields_schema(self, schema: core_schema.ModelFieldsSchema) -> JsonSchemaValue:
  1390. """Generates a JSON schema that matches a schema that defines a model's fields.
  1391. Args:
  1392. schema: The core schema.
  1393. Returns:
  1394. The generated JSON schema.
  1395. """
  1396. named_required_fields: list[tuple[str, bool, CoreSchemaField]] = [
  1397. (name, self.field_is_required(field, total=True), field)
  1398. for name, field in schema['fields'].items()
  1399. if self.field_is_present(field)
  1400. ]
  1401. if self.mode == 'serialization':
  1402. named_required_fields.extend(self._name_required_computed_fields(schema.get('computed_fields', [])))
  1403. json_schema = self._named_required_fields_schema(named_required_fields)
  1404. extras_schema = schema.get('extras_schema', None)
  1405. if extras_schema is not None:
  1406. schema_to_update = self.resolve_ref_schema(json_schema)
  1407. schema_to_update['additionalProperties'] = self.generate_inner(extras_schema)
  1408. return json_schema
  1409. def field_is_present(self, field: CoreSchemaField) -> bool:
  1410. """Whether the field should be included in the generated JSON schema.
  1411. Args:
  1412. field: The schema for the field itself.
  1413. Returns:
  1414. `True` if the field should be included in the generated JSON schema, `False` otherwise.
  1415. """
  1416. if self.mode == 'serialization':
  1417. # If you still want to include the field in the generated JSON schema,
  1418. # override this method and return True
  1419. return not field.get('serialization_exclude')
  1420. elif self.mode == 'validation':
  1421. return True
  1422. else:
  1423. assert_never(self.mode)
  1424. def field_is_required(
  1425. self,
  1426. field: core_schema.ModelField | core_schema.DataclassField | core_schema.TypedDictField,
  1427. total: bool,
  1428. ) -> bool:
  1429. """Whether the field should be marked as required in the generated JSON schema.
  1430. (Note that this is irrelevant if the field is not present in the JSON schema.).
  1431. Args:
  1432. field: The schema for the field itself.
  1433. total: Only applies to `TypedDictField`s.
  1434. Indicates if the `TypedDict` this field belongs to is total, in which case any fields that don't
  1435. explicitly specify `required=False` are required.
  1436. Returns:
  1437. `True` if the field should be marked as required in the generated JSON schema, `False` otherwise.
  1438. """
  1439. if field['type'] == 'typed-dict-field':
  1440. required = field.get('required', total)
  1441. else:
  1442. required = field['schema']['type'] != 'default'
  1443. if self.mode == 'serialization':
  1444. has_exclude_if = field.get('serialization_exclude_if') is not None
  1445. if self._config.json_schema_serialization_defaults_required:
  1446. return not has_exclude_if
  1447. else:
  1448. return required and not has_exclude_if
  1449. else:
  1450. return required
  1451. def dataclass_args_schema(self, schema: core_schema.DataclassArgsSchema) -> JsonSchemaValue:
  1452. """Generates a JSON schema that matches a schema that defines a dataclass's constructor arguments.
  1453. Args:
  1454. schema: The core schema.
  1455. Returns:
  1456. The generated JSON schema.
  1457. """
  1458. named_required_fields: list[tuple[str, bool, CoreSchemaField]] = [
  1459. (field['name'], self.field_is_required(field, total=True), field)
  1460. for field in schema['fields']
  1461. if self.field_is_present(field)
  1462. ]
  1463. if self.mode == 'serialization':
  1464. named_required_fields.extend(self._name_required_computed_fields(schema.get('computed_fields', [])))
  1465. return self._named_required_fields_schema(named_required_fields)
  1466. def dataclass_schema(self, schema: core_schema.DataclassSchema) -> JsonSchemaValue:
  1467. """Generates a JSON schema that matches a schema that defines a dataclass.
  1468. Args:
  1469. schema: The core schema.
  1470. Returns:
  1471. The generated JSON schema.
  1472. """
  1473. from ._internal._dataclasses import is_stdlib_dataclass
  1474. cls = schema['cls']
  1475. config: ConfigDict = getattr(cls, '__pydantic_config__', cast('ConfigDict', {}))
  1476. with self._config_wrapper_stack.push(config):
  1477. json_schema = self.generate_inner(schema['schema']).copy()
  1478. self._update_class_schema(json_schema, cls, config)
  1479. # Dataclass-specific handling of description
  1480. if is_stdlib_dataclass(cls):
  1481. # vanilla dataclass; don't use cls.__doc__ as it will contain the class signature by default
  1482. description = None
  1483. else:
  1484. description = None if cls.__doc__ is None else inspect.cleandoc(cls.__doc__)
  1485. if description:
  1486. json_schema['description'] = description
  1487. return json_schema
  1488. def arguments_schema(self, schema: core_schema.ArgumentsSchema) -> JsonSchemaValue:
  1489. """Generates a JSON schema that matches a schema that defines a function's arguments.
  1490. Args:
  1491. schema: The core schema.
  1492. Returns:
  1493. The generated JSON schema.
  1494. """
  1495. prefer_positional = schema.get('metadata', {}).get('pydantic_js_prefer_positional_arguments')
  1496. arguments = schema['arguments_schema']
  1497. kw_only_arguments = [a for a in arguments if a.get('mode') == 'keyword_only']
  1498. kw_or_p_arguments = [a for a in arguments if a.get('mode') in {'positional_or_keyword', None}]
  1499. p_only_arguments = [a for a in arguments if a.get('mode') == 'positional_only']
  1500. var_args_schema = schema.get('var_args_schema')
  1501. var_kwargs_schema = schema.get('var_kwargs_schema')
  1502. if prefer_positional:
  1503. positional_possible = not kw_only_arguments and not var_kwargs_schema
  1504. if positional_possible:
  1505. return self.p_arguments_schema(p_only_arguments + kw_or_p_arguments, var_args_schema)
  1506. keyword_possible = not p_only_arguments and not var_args_schema
  1507. if keyword_possible:
  1508. return self.kw_arguments_schema(kw_or_p_arguments + kw_only_arguments, var_kwargs_schema)
  1509. if not prefer_positional:
  1510. positional_possible = not kw_only_arguments and not var_kwargs_schema
  1511. if positional_possible:
  1512. return self.p_arguments_schema(p_only_arguments + kw_or_p_arguments, var_args_schema)
  1513. raise PydanticInvalidForJsonSchema(
  1514. 'Unable to generate JSON schema for arguments validator with positional-only and keyword-only arguments'
  1515. )
  1516. def kw_arguments_schema(
  1517. self, arguments: list[core_schema.ArgumentsParameter], var_kwargs_schema: CoreSchema | None
  1518. ) -> JsonSchemaValue:
  1519. """Generates a JSON schema that matches a schema that defines a function's keyword arguments.
  1520. Args:
  1521. arguments: The core schema.
  1522. Returns:
  1523. The generated JSON schema.
  1524. """
  1525. properties: dict[str, JsonSchemaValue] = {}
  1526. required: list[str] = []
  1527. for argument in arguments:
  1528. name = self.get_argument_name(argument)
  1529. argument_schema = self.generate_inner(argument['schema']).copy()
  1530. if 'title' not in argument_schema and self.field_title_should_be_set(argument['schema']):
  1531. argument_schema['title'] = self.get_title_from_name(name)
  1532. properties[name] = argument_schema
  1533. if argument['schema']['type'] != 'default':
  1534. # This assumes that if the argument has a default value,
  1535. # the inner schema must be of type WithDefaultSchema.
  1536. # I believe this is true, but I am not 100% sure
  1537. required.append(name)
  1538. json_schema: JsonSchemaValue = {'type': 'object', 'properties': properties}
  1539. if required:
  1540. json_schema['required'] = required
  1541. if var_kwargs_schema:
  1542. additional_properties_schema = self.generate_inner(var_kwargs_schema)
  1543. if additional_properties_schema:
  1544. json_schema['additionalProperties'] = additional_properties_schema
  1545. else:
  1546. json_schema['additionalProperties'] = False
  1547. return json_schema
  1548. def p_arguments_schema(
  1549. self, arguments: list[core_schema.ArgumentsParameter], var_args_schema: CoreSchema | None
  1550. ) -> JsonSchemaValue:
  1551. """Generates a JSON schema that matches a schema that defines a function's positional arguments.
  1552. Args:
  1553. arguments: The core schema.
  1554. Returns:
  1555. The generated JSON schema.
  1556. """
  1557. prefix_items: list[JsonSchemaValue] = []
  1558. min_items = 0
  1559. for argument in arguments:
  1560. name = self.get_argument_name(argument)
  1561. argument_schema = self.generate_inner(argument['schema']).copy()
  1562. if 'title' not in argument_schema and self.field_title_should_be_set(argument['schema']):
  1563. argument_schema['title'] = self.get_title_from_name(name)
  1564. prefix_items.append(argument_schema)
  1565. if argument['schema']['type'] != 'default':
  1566. # This assumes that if the argument has a default value,
  1567. # the inner schema must be of type WithDefaultSchema.
  1568. # I believe this is true, but I am not 100% sure
  1569. min_items += 1
  1570. json_schema: JsonSchemaValue = {'type': 'array'}
  1571. if prefix_items:
  1572. json_schema['prefixItems'] = prefix_items
  1573. if min_items:
  1574. json_schema['minItems'] = min_items
  1575. if var_args_schema:
  1576. items_schema = self.generate_inner(var_args_schema)
  1577. if items_schema:
  1578. json_schema['items'] = items_schema
  1579. else:
  1580. json_schema['maxItems'] = len(prefix_items)
  1581. return json_schema
  1582. def get_argument_name(self, argument: core_schema.ArgumentsParameter | core_schema.ArgumentsV3Parameter) -> str:
  1583. """Retrieves the name of an argument.
  1584. Args:
  1585. argument: The core schema.
  1586. Returns:
  1587. The name of the argument.
  1588. """
  1589. name = argument['name']
  1590. if self.by_alias:
  1591. alias = argument.get('alias')
  1592. if isinstance(alias, str):
  1593. name = alias
  1594. else:
  1595. pass # might want to do something else?
  1596. return name
  1597. def arguments_v3_schema(self, schema: core_schema.ArgumentsV3Schema) -> JsonSchemaValue:
  1598. """Generates a JSON schema that matches a schema that defines a function's arguments.
  1599. Args:
  1600. schema: The core schema.
  1601. Returns:
  1602. The generated JSON schema.
  1603. """
  1604. arguments = schema['arguments_schema']
  1605. properties: dict[str, JsonSchemaValue] = {}
  1606. required: list[str] = []
  1607. for argument in arguments:
  1608. mode = argument.get('mode', 'positional_or_keyword')
  1609. name = self.get_argument_name(argument)
  1610. argument_schema = self.generate_inner(argument['schema']).copy()
  1611. if mode == 'var_args':
  1612. argument_schema = {'type': 'array', 'items': argument_schema}
  1613. elif mode == 'var_kwargs_uniform':
  1614. argument_schema = {'type': 'object', 'additionalProperties': argument_schema}
  1615. argument_schema.setdefault('title', self.get_title_from_name(name))
  1616. properties[name] = argument_schema
  1617. if (
  1618. (mode == 'var_kwargs_unpacked_typed_dict' and 'required' in argument_schema)
  1619. or mode not in {'var_args', 'var_kwargs_uniform', 'var_kwargs_unpacked_typed_dict'}
  1620. and argument['schema']['type'] != 'default'
  1621. ):
  1622. # This assumes that if the argument has a default value,
  1623. # the inner schema must be of type WithDefaultSchema.
  1624. # I believe this is true, but I am not 100% sure
  1625. required.append(name)
  1626. json_schema: JsonSchemaValue = {'type': 'object', 'properties': properties}
  1627. if required:
  1628. json_schema['required'] = required
  1629. return json_schema
  1630. def call_schema(self, schema: core_schema.CallSchema) -> JsonSchemaValue:
  1631. """Generates a JSON schema that matches a schema that defines a function call.
  1632. Args:
  1633. schema: The core schema.
  1634. Returns:
  1635. The generated JSON schema.
  1636. """
  1637. return self.generate_inner(schema['arguments_schema'])
  1638. def custom_error_schema(self, schema: core_schema.CustomErrorSchema) -> JsonSchemaValue:
  1639. """Generates a JSON schema that matches a schema that defines a custom error.
  1640. Args:
  1641. schema: The core schema.
  1642. Returns:
  1643. The generated JSON schema.
  1644. """
  1645. return self.generate_inner(schema['schema'])
  1646. def json_schema(self, schema: core_schema.JsonSchema) -> JsonSchemaValue:
  1647. """Generates a JSON schema that matches a schema that defines a JSON object.
  1648. Args:
  1649. schema: The core schema.
  1650. Returns:
  1651. The generated JSON schema.
  1652. """
  1653. content_core_schema = schema.get('schema') or core_schema.any_schema()
  1654. content_json_schema = self.generate_inner(content_core_schema)
  1655. if self.mode == 'validation':
  1656. return {'type': 'string', 'contentMediaType': 'application/json', 'contentSchema': content_json_schema}
  1657. else:
  1658. # self.mode == 'serialization'
  1659. return content_json_schema
  1660. def url_schema(self, schema: core_schema.UrlSchema) -> JsonSchemaValue:
  1661. """Generates a JSON schema that matches a schema that defines a URL.
  1662. Args:
  1663. schema: The core schema.
  1664. Returns:
  1665. The generated JSON schema.
  1666. """
  1667. json_schema = {'type': 'string', 'format': 'uri', 'minLength': 1}
  1668. self.update_with_validations(json_schema, schema, self.ValidationsMapping.string)
  1669. return json_schema
  1670. def multi_host_url_schema(self, schema: core_schema.MultiHostUrlSchema) -> JsonSchemaValue:
  1671. """Generates a JSON schema that matches a schema that defines a URL that can be used with multiple hosts.
  1672. Args:
  1673. schema: The core schema.
  1674. Returns:
  1675. The generated JSON schema.
  1676. """
  1677. # Note: 'multi-host-uri' is a custom/pydantic-specific format, not part of the JSON Schema spec
  1678. json_schema = {'type': 'string', 'format': 'multi-host-uri', 'minLength': 1}
  1679. self.update_with_validations(json_schema, schema, self.ValidationsMapping.string)
  1680. return json_schema
  1681. def uuid_schema(self, schema: core_schema.UuidSchema) -> JsonSchemaValue:
  1682. """Generates a JSON schema that matches a UUID.
  1683. Args:
  1684. schema: The core schema.
  1685. Returns:
  1686. The generated JSON schema.
  1687. """
  1688. return {'type': 'string', 'format': 'uuid'}
  1689. def definitions_schema(self, schema: core_schema.DefinitionsSchema) -> JsonSchemaValue:
  1690. """Generates a JSON schema that matches a schema that defines a JSON object with definitions.
  1691. Args:
  1692. schema: The core schema.
  1693. Returns:
  1694. The generated JSON schema.
  1695. """
  1696. for definition in schema['definitions']:
  1697. try:
  1698. self.generate_inner(definition)
  1699. except PydanticInvalidForJsonSchema as e: # noqa: PERF203
  1700. core_ref: CoreRef = CoreRef(definition['ref']) # type: ignore
  1701. self._core_defs_invalid_for_json_schema[self.get_defs_ref((core_ref, self.mode))] = e
  1702. continue
  1703. return self.generate_inner(schema['schema'])
  1704. def definition_ref_schema(self, schema: core_schema.DefinitionReferenceSchema) -> JsonSchemaValue:
  1705. """Generates a JSON schema that matches a schema that references a definition.
  1706. Args:
  1707. schema: The core schema.
  1708. Returns:
  1709. The generated JSON schema.
  1710. """
  1711. core_ref = CoreRef(schema['schema_ref'])
  1712. _, ref_json_schema = self.get_cache_defs_ref_schema(core_ref)
  1713. return ref_json_schema
  1714. def ser_schema(
  1715. self, schema: core_schema.SerSchema | core_schema.IncExSeqSerSchema | core_schema.IncExDictSerSchema
  1716. ) -> JsonSchemaValue | None:
  1717. """Generates a JSON schema that matches a schema that defines a serialized object.
  1718. Args:
  1719. schema: The core schema.
  1720. Returns:
  1721. The generated JSON schema.
  1722. """
  1723. schema_type = schema['type']
  1724. if schema_type == 'function-plain' or schema_type == 'function-wrap':
  1725. # PlainSerializerFunctionSerSchema or WrapSerializerFunctionSerSchema
  1726. return_schema = schema.get('return_schema')
  1727. if return_schema is not None:
  1728. return self.generate_inner(return_schema)
  1729. elif schema_type == 'format' or schema_type == 'to-string':
  1730. # FormatSerSchema or ToStringSerSchema
  1731. return self.str_schema(core_schema.str_schema())
  1732. elif schema['type'] == 'model':
  1733. # ModelSerSchema
  1734. return self.generate_inner(schema['schema'])
  1735. return None
  1736. def complex_schema(self, schema: core_schema.ComplexSchema) -> JsonSchemaValue:
  1737. """Generates a JSON schema that matches a complex number.
  1738. JSON has no standard way to represent complex numbers. Complex number is not a numeric
  1739. type. Here we represent complex number as strings following the rule defined by Python.
  1740. For instance, '1+2j' is an accepted complex string. Details can be found in
  1741. [Python's `complex` documentation][complex].
  1742. Args:
  1743. schema: The core schema.
  1744. Returns:
  1745. The generated JSON schema.
  1746. """
  1747. return {'type': 'string'}
  1748. # ### Utility methods
  1749. def get_title_from_name(self, name: str) -> str:
  1750. """Retrieves a title from a name.
  1751. Args:
  1752. name: The name to retrieve a title from.
  1753. Returns:
  1754. The title.
  1755. """
  1756. return name.title().replace('_', ' ').strip()
  1757. def field_title_should_be_set(self, schema: CoreSchemaOrField) -> bool:
  1758. """Returns true if a field with the given schema should have a title set based on the field name.
  1759. Intuitively, we want this to return true for schemas that wouldn't otherwise provide their own title
  1760. (e.g., int, float, str), and false for those that would (e.g., BaseModel subclasses).
  1761. Args:
  1762. schema: The schema to check.
  1763. Returns:
  1764. `True` if the field should have a title set, `False` otherwise.
  1765. """
  1766. if _core_utils.is_core_schema_field(schema):
  1767. if schema['type'] == 'computed-field':
  1768. field_schema = schema['return_schema']
  1769. else:
  1770. field_schema = schema['schema']
  1771. return self.field_title_should_be_set(field_schema)
  1772. elif _core_utils.is_core_schema(schema):
  1773. if schema.get('ref'): # things with refs, such as models and enums, should not have titles set
  1774. return False
  1775. if schema['type'] in {'default', 'nullable', 'definitions'}:
  1776. return self.field_title_should_be_set(schema['schema']) # type: ignore[typeddict-item]
  1777. if _core_utils.is_function_with_inner_schema(schema):
  1778. return self.field_title_should_be_set(schema['schema'])
  1779. if schema['type'] == 'definition-ref':
  1780. # Referenced schemas should not have titles set for the same reason
  1781. # schemas with refs should not
  1782. return False
  1783. return True # anything else should have title set
  1784. else:
  1785. raise PydanticInvalidForJsonSchema(f'Unexpected schema type: schema={schema}') # pragma: no cover
  1786. def normalize_name(self, name: str) -> str:
  1787. """Normalizes a name to be used as a key in a dictionary.
  1788. Args:
  1789. name: The name to normalize.
  1790. Returns:
  1791. The normalized name.
  1792. """
  1793. return re.sub(r'[^a-zA-Z0-9.\-_]', '_', name).replace('.', '__')
  1794. def get_defs_ref(self, core_mode_ref: CoreModeRef) -> DefsRef:
  1795. """Override this method to change the way that definitions keys are generated from a core reference.
  1796. Args:
  1797. core_mode_ref: The core reference.
  1798. Returns:
  1799. The definitions key.
  1800. """
  1801. # Split the core ref into "components"; generic origins and arguments are each separate components
  1802. core_ref, mode = core_mode_ref
  1803. components = re.split(r'([\][,])', core_ref)
  1804. # Remove IDs from each component
  1805. components = [x.rsplit(':', 1)[0] for x in components]
  1806. core_ref_no_id = ''.join(components)
  1807. # Remove everything before the last period from each "component"
  1808. components = [re.sub(r'(?:[^.[\]]+\.)+((?:[^.[\]]+))', r'\1', x) for x in components]
  1809. short_ref = ''.join(components)
  1810. mode_title = _MODE_TITLE_MAPPING[mode]
  1811. # It is important that the generated defs_ref values be such that at least one choice will not
  1812. # be generated for any other core_ref. Currently, this should be the case because we include
  1813. # the id of the source type in the core_ref
  1814. name = DefsRef(self.normalize_name(short_ref))
  1815. name_mode = DefsRef(self.normalize_name(short_ref) + f'-{mode_title}')
  1816. module_qualname = DefsRef(self.normalize_name(core_ref_no_id))
  1817. module_qualname_mode = DefsRef(f'{module_qualname}-{mode_title}')
  1818. module_qualname_id = DefsRef(self.normalize_name(core_ref))
  1819. occurrence_index = self._collision_index.get(module_qualname_id)
  1820. if occurrence_index is None:
  1821. self._collision_counter[module_qualname] += 1
  1822. occurrence_index = self._collision_index[module_qualname_id] = self._collision_counter[module_qualname]
  1823. module_qualname_occurrence = DefsRef(f'{module_qualname}__{occurrence_index}')
  1824. module_qualname_occurrence_mode = DefsRef(f'{module_qualname_mode}__{occurrence_index}')
  1825. self._prioritized_defsref_choices[module_qualname_occurrence_mode] = [
  1826. name,
  1827. name_mode,
  1828. module_qualname,
  1829. module_qualname_mode,
  1830. module_qualname_occurrence,
  1831. module_qualname_occurrence_mode,
  1832. ]
  1833. return module_qualname_occurrence_mode
  1834. def get_cache_defs_ref_schema(self, core_ref: CoreRef) -> tuple[DefsRef, JsonSchemaValue]:
  1835. """This method wraps the get_defs_ref method with some cache-lookup/population logic,
  1836. and returns both the produced defs_ref and the JSON schema that will refer to the right definition.
  1837. Args:
  1838. core_ref: The core reference to get the definitions reference for.
  1839. Returns:
  1840. A tuple of the definitions reference and the JSON schema that will refer to it.
  1841. """
  1842. core_mode_ref = (core_ref, self.mode)
  1843. maybe_defs_ref = self.core_to_defs_refs.get(core_mode_ref)
  1844. if maybe_defs_ref is not None:
  1845. json_ref = self.core_to_json_refs[core_mode_ref]
  1846. return maybe_defs_ref, {'$ref': json_ref}
  1847. defs_ref = self.get_defs_ref(core_mode_ref)
  1848. # populate the ref translation mappings
  1849. self.core_to_defs_refs[core_mode_ref] = defs_ref
  1850. self.defs_to_core_refs[defs_ref] = core_mode_ref
  1851. json_ref = JsonRef(self.ref_template.format(model=defs_ref))
  1852. self.core_to_json_refs[core_mode_ref] = json_ref
  1853. self.json_to_defs_refs[json_ref] = defs_ref
  1854. ref_json_schema = {'$ref': json_ref}
  1855. return defs_ref, ref_json_schema
  1856. def handle_ref_overrides(self, json_schema: JsonSchemaValue) -> JsonSchemaValue:
  1857. """Remove any sibling keys that are redundant with the referenced schema.
  1858. Args:
  1859. json_schema: The schema to remove redundant sibling keys from.
  1860. Returns:
  1861. The schema with redundant sibling keys removed.
  1862. """
  1863. if '$ref' in json_schema:
  1864. # prevent modifications to the input; this copy may be safe to drop if there is significant overhead
  1865. json_schema = json_schema.copy()
  1866. referenced_json_schema = self.get_schema_from_definitions(JsonRef(json_schema['$ref']))
  1867. if referenced_json_schema is None:
  1868. # This can happen when building schemas for models with not-yet-defined references.
  1869. # It may be a good idea to do a recursive pass at the end of the generation to remove
  1870. # any redundant override keys.
  1871. return json_schema
  1872. for k, v in list(json_schema.items()):
  1873. if k == '$ref':
  1874. continue
  1875. if k in referenced_json_schema and referenced_json_schema[k] == v:
  1876. del json_schema[k] # redundant key
  1877. return json_schema
  1878. def get_schema_from_definitions(self, json_ref: JsonRef) -> JsonSchemaValue | None:
  1879. try:
  1880. def_ref = self.json_to_defs_refs[json_ref]
  1881. if def_ref in self._core_defs_invalid_for_json_schema:
  1882. raise self._core_defs_invalid_for_json_schema[def_ref]
  1883. return self.definitions.get(def_ref, None)
  1884. except KeyError:
  1885. if json_ref.startswith(('http://', 'https://')):
  1886. return None
  1887. raise
  1888. def encode_default(self, dft: Any) -> Any:
  1889. """Encode a default value to a JSON-serializable value.
  1890. This is used to encode default values for fields in the generated JSON schema.
  1891. Args:
  1892. dft: The default value to encode.
  1893. Returns:
  1894. The encoded default value.
  1895. """
  1896. from .type_adapter import TypeAdapter, _type_has_config
  1897. config = self._config
  1898. try:
  1899. default = (
  1900. dft
  1901. if _type_has_config(type(dft))
  1902. else TypeAdapter(type(dft), config=config.config_dict).dump_python(
  1903. dft, by_alias=self.by_alias, mode='json'
  1904. )
  1905. )
  1906. except PydanticSchemaGenerationError:
  1907. raise pydantic_core.PydanticSerializationError(f'Unable to encode default value {dft}')
  1908. return pydantic_core.to_jsonable_python(
  1909. default, timedelta_mode=config.ser_json_timedelta, bytes_mode=config.ser_json_bytes, by_alias=self.by_alias
  1910. )
  1911. def update_with_validations(
  1912. self, json_schema: JsonSchemaValue, core_schema: CoreSchema, mapping: dict[str, str]
  1913. ) -> None:
  1914. """Update the json_schema with the corresponding validations specified in the core_schema,
  1915. using the provided mapping to translate keys in core_schema to the appropriate keys for a JSON schema.
  1916. Args:
  1917. json_schema: The JSON schema to update.
  1918. core_schema: The core schema to get the validations from.
  1919. mapping: A mapping from core_schema attribute names to the corresponding JSON schema attribute names.
  1920. """
  1921. for core_key, json_schema_key in mapping.items():
  1922. if core_key in core_schema:
  1923. json_schema[json_schema_key] = core_schema[core_key]
  1924. class ValidationsMapping:
  1925. """This class just contains mappings from core_schema attribute names to the corresponding
  1926. JSON schema attribute names. While I suspect it is unlikely to be necessary, you can in
  1927. principle override this class in a subclass of GenerateJsonSchema (by inheriting from
  1928. GenerateJsonSchema.ValidationsMapping) to change these mappings.
  1929. """
  1930. numeric = {
  1931. 'multiple_of': 'multipleOf',
  1932. 'le': 'maximum',
  1933. 'ge': 'minimum',
  1934. 'lt': 'exclusiveMaximum',
  1935. 'gt': 'exclusiveMinimum',
  1936. }
  1937. bytes = {
  1938. 'min_length': 'minLength',
  1939. 'max_length': 'maxLength',
  1940. }
  1941. string = {
  1942. 'min_length': 'minLength',
  1943. 'max_length': 'maxLength',
  1944. 'pattern': 'pattern',
  1945. }
  1946. array = {
  1947. 'min_length': 'minItems',
  1948. 'max_length': 'maxItems',
  1949. }
  1950. object = {
  1951. 'min_length': 'minProperties',
  1952. 'max_length': 'maxProperties',
  1953. }
  1954. def get_flattened_anyof(self, schemas: list[JsonSchemaValue]) -> JsonSchemaValue:
  1955. members = []
  1956. for schema in schemas:
  1957. if len(schema) == 1 and 'anyOf' in schema:
  1958. members.extend(schema['anyOf'])
  1959. else:
  1960. members.append(schema)
  1961. members = _deduplicate_schemas(members)
  1962. if len(members) == 1:
  1963. return members[0]
  1964. return {'anyOf': members}
  1965. def get_json_ref_counts(self, json_schema: JsonSchemaValue) -> dict[JsonRef, int]:
  1966. """Get all values corresponding to the key '$ref' anywhere in the json_schema."""
  1967. json_refs: dict[JsonRef, int] = Counter()
  1968. def _add_json_refs(schema: Any) -> None:
  1969. if isinstance(schema, dict):
  1970. if '$ref' in schema:
  1971. json_ref = JsonRef(schema['$ref'])
  1972. if not isinstance(json_ref, str):
  1973. return # in this case, '$ref' might have been the name of a property
  1974. already_visited = json_ref in json_refs
  1975. json_refs[json_ref] += 1
  1976. if already_visited:
  1977. return # prevent recursion on a definition that was already visited
  1978. try:
  1979. defs_ref = self.json_to_defs_refs[json_ref]
  1980. if defs_ref in self._core_defs_invalid_for_json_schema:
  1981. raise self._core_defs_invalid_for_json_schema[defs_ref]
  1982. _add_json_refs(self.definitions[defs_ref])
  1983. except KeyError:
  1984. if not json_ref.startswith(('http://', 'https://')):
  1985. raise
  1986. for k, v in schema.items():
  1987. if k == 'examples' and isinstance(v, list):
  1988. # Skip examples that may contain arbitrary values and references
  1989. # (see the comment in `_get_all_json_refs` for more details).
  1990. continue
  1991. _add_json_refs(v)
  1992. elif isinstance(schema, list):
  1993. for v in schema:
  1994. _add_json_refs(v)
  1995. _add_json_refs(json_schema)
  1996. return json_refs
  1997. def handle_invalid_for_json_schema(self, schema: CoreSchemaOrField, error_info: str) -> JsonSchemaValue:
  1998. raise PydanticInvalidForJsonSchema(f'Cannot generate a JsonSchema for {error_info}')
  1999. def emit_warning(self, kind: JsonSchemaWarningKind, detail: str) -> None:
  2000. """This method simply emits PydanticJsonSchemaWarnings based on handling in the `warning_message` method."""
  2001. message = self.render_warning_message(kind, detail)
  2002. if message is not None:
  2003. warnings.warn(message, PydanticJsonSchemaWarning)
  2004. def render_warning_message(self, kind: JsonSchemaWarningKind, detail: str) -> str | None:
  2005. """This method is responsible for ignoring warnings as desired, and for formatting the warning messages.
  2006. You can override the value of `ignored_warning_kinds` in a subclass of GenerateJsonSchema
  2007. to modify what warnings are generated. If you want more control, you can override this method;
  2008. just return None in situations where you don't want warnings to be emitted.
  2009. Args:
  2010. kind: The kind of warning to render. It can be one of the following:
  2011. - 'skipped-choice': A choice field was skipped because it had no valid choices.
  2012. - 'non-serializable-default': A default value was skipped because it was not JSON-serializable.
  2013. detail: A string with additional details about the warning.
  2014. Returns:
  2015. The formatted warning message, or `None` if no warning should be emitted.
  2016. """
  2017. if kind in self.ignored_warning_kinds:
  2018. return None
  2019. return f'{detail} [{kind}]'
  2020. def _build_definitions_remapping(self) -> _DefinitionsRemapping:
  2021. defs_to_json: dict[DefsRef, JsonRef] = {}
  2022. for defs_refs in self._prioritized_defsref_choices.values():
  2023. for defs_ref in defs_refs:
  2024. json_ref = JsonRef(self.ref_template.format(model=defs_ref))
  2025. defs_to_json[defs_ref] = json_ref
  2026. return _DefinitionsRemapping.from_prioritized_choices(
  2027. self._prioritized_defsref_choices, defs_to_json, self.definitions
  2028. )
  2029. def _garbage_collect_definitions(self, schema: JsonSchemaValue) -> None:
  2030. visited_defs_refs: set[DefsRef] = set()
  2031. unvisited_json_refs = _get_all_json_refs(schema)
  2032. while unvisited_json_refs:
  2033. next_json_ref = unvisited_json_refs.pop()
  2034. try:
  2035. next_defs_ref = self.json_to_defs_refs[next_json_ref]
  2036. if next_defs_ref in visited_defs_refs:
  2037. continue
  2038. visited_defs_refs.add(next_defs_ref)
  2039. unvisited_json_refs.update(_get_all_json_refs(self.definitions[next_defs_ref]))
  2040. except KeyError:
  2041. if not next_json_ref.startswith(('http://', 'https://')):
  2042. raise
  2043. self.definitions = {k: v for k, v in self.definitions.items() if k in visited_defs_refs}
  2044. # ##### Start JSON Schema Generation Functions #####
  2045. def model_json_schema(
  2046. cls: type[BaseModel] | type[PydanticDataclass],
  2047. by_alias: bool = True,
  2048. ref_template: str = DEFAULT_REF_TEMPLATE,
  2049. union_format: Literal['any_of', 'primitive_type_array'] = 'any_of',
  2050. schema_generator: type[GenerateJsonSchema] = GenerateJsonSchema,
  2051. mode: JsonSchemaMode = 'validation',
  2052. ) -> dict[str, Any]:
  2053. """Utility function to generate a JSON Schema for a model.
  2054. Args:
  2055. cls: The model class to generate a JSON Schema for.
  2056. by_alias: If `True` (the default), fields will be serialized according to their alias.
  2057. If `False`, fields will be serialized according to their attribute name.
  2058. ref_template: The template to use for generating JSON Schema references.
  2059. union_format: The format to use when combining schemas from unions together. Can be one of:
  2060. - `'any_of'`: Use the [`anyOf`](https://json-schema.org/understanding-json-schema/reference/combining#anyOf)
  2061. keyword to combine schemas (the default).
  2062. - `'primitive_type_array'`: Use the [`type`](https://json-schema.org/understanding-json-schema/reference/type)
  2063. keyword as an array of strings, containing each type of the combination. If any of the schemas is not a primitive
  2064. type (`string`, `boolean`, `null`, `integer` or `number`) or contains constraints/metadata, falls back to
  2065. `any_of`.
  2066. schema_generator: The class to use for generating the JSON Schema.
  2067. mode: The mode to use for generating the JSON Schema. It can be one of the following:
  2068. - 'validation': Generate a JSON Schema for validating data.
  2069. - 'serialization': Generate a JSON Schema for serializing data.
  2070. Returns:
  2071. The generated JSON Schema.
  2072. """
  2073. from .main import BaseModel
  2074. schema_generator_instance = schema_generator(
  2075. by_alias=by_alias, ref_template=ref_template, union_format=union_format
  2076. )
  2077. if isinstance(cls.__pydantic_core_schema__, _mock_val_ser.MockCoreSchema):
  2078. cls.__pydantic_core_schema__.rebuild()
  2079. if cls is BaseModel:
  2080. raise AttributeError('model_json_schema() must be called on a subclass of BaseModel, not BaseModel itself.')
  2081. assert not isinstance(cls.__pydantic_core_schema__, _mock_val_ser.MockCoreSchema), 'this is a bug! please report it'
  2082. return schema_generator_instance.generate(cls.__pydantic_core_schema__, mode=mode)
  2083. def models_json_schema(
  2084. models: Sequence[tuple[type[BaseModel] | type[PydanticDataclass], JsonSchemaMode]],
  2085. *,
  2086. by_alias: bool = True,
  2087. title: str | None = None,
  2088. description: str | None = None,
  2089. ref_template: str = DEFAULT_REF_TEMPLATE,
  2090. union_format: Literal['any_of', 'primitive_type_array'] = 'any_of',
  2091. schema_generator: type[GenerateJsonSchema] = GenerateJsonSchema,
  2092. ) -> tuple[dict[tuple[type[BaseModel] | type[PydanticDataclass], JsonSchemaMode], JsonSchemaValue], JsonSchemaValue]:
  2093. """Utility function to generate a JSON Schema for multiple models.
  2094. Args:
  2095. models: A sequence of tuples of the form (model, mode).
  2096. by_alias: Whether field aliases should be used as keys in the generated JSON Schema.
  2097. title: The title of the generated JSON Schema.
  2098. description: The description of the generated JSON Schema.
  2099. ref_template: The reference template to use for generating JSON Schema references.
  2100. union_format: The format to use when combining schemas from unions together. Can be one of:
  2101. - `'any_of'`: Use the [`anyOf`](https://json-schema.org/understanding-json-schema/reference/combining#anyOf)
  2102. keyword to combine schemas (the default).
  2103. - `'primitive_type_array'`: Use the [`type`](https://json-schema.org/understanding-json-schema/reference/type)
  2104. keyword as an array of strings, containing each type of the combination. If any of the schemas is not a primitive
  2105. type (`string`, `boolean`, `null`, `integer` or `number`) or contains constraints/metadata, falls back to
  2106. `any_of`.
  2107. schema_generator: The schema generator to use for generating the JSON Schema.
  2108. Returns:
  2109. A tuple where:
  2110. - The first element is a dictionary whose keys are tuples of JSON schema key type and JSON mode, and
  2111. whose values are the JSON schema corresponding to that pair of inputs. (These schemas may have
  2112. JsonRef references to definitions that are defined in the second returned element.)
  2113. - The second element is a JSON schema containing all definitions referenced in the first returned
  2114. element, along with the optional title and description keys.
  2115. """
  2116. for cls, _ in models:
  2117. if isinstance(cls.__pydantic_core_schema__, _mock_val_ser.MockCoreSchema):
  2118. cls.__pydantic_core_schema__.rebuild()
  2119. instance = schema_generator(by_alias=by_alias, ref_template=ref_template, union_format=union_format)
  2120. inputs: list[tuple[type[BaseModel] | type[PydanticDataclass], JsonSchemaMode, CoreSchema]] = [
  2121. (m, mode, m.__pydantic_core_schema__) for m, mode in models
  2122. ]
  2123. json_schemas_map, definitions = instance.generate_definitions(inputs)
  2124. json_schema: dict[str, Any] = {}
  2125. if definitions:
  2126. json_schema['$defs'] = definitions
  2127. if title:
  2128. json_schema['title'] = title
  2129. if description:
  2130. json_schema['description'] = description
  2131. return json_schemas_map, json_schema
  2132. # ##### End JSON Schema Generation Functions #####
  2133. _HashableJsonValue: TypeAlias = Union[
  2134. int, float, str, bool, None, tuple['_HashableJsonValue', ...], tuple[tuple[str, '_HashableJsonValue'], ...]
  2135. ]
  2136. def _deduplicate_schemas(schemas: Iterable[JsonDict]) -> list[JsonDict]:
  2137. return list({_make_json_hashable(schema): schema for schema in schemas}.values())
  2138. def _make_json_hashable(value: JsonValue) -> _HashableJsonValue:
  2139. if isinstance(value, dict):
  2140. return tuple(sorted((k, _make_json_hashable(v)) for k, v in value.items()))
  2141. elif isinstance(value, list):
  2142. return tuple(_make_json_hashable(v) for v in value)
  2143. else:
  2144. return value
  2145. @dataclasses.dataclass(**_internal_dataclass.slots_true)
  2146. class WithJsonSchema:
  2147. """!!! abstract "Usage Documentation"
  2148. [`WithJsonSchema` Annotation](../concepts/json_schema.md#withjsonschema-annotation)
  2149. Add this as an annotation on a field to override the (base) JSON schema that would be generated for that field.
  2150. This provides a way to set a JSON schema for types that would otherwise raise errors when producing a JSON schema,
  2151. such as Callable, or types that have an is-instance core schema, without needing to go so far as creating a
  2152. custom subclass of pydantic.json_schema.GenerateJsonSchema.
  2153. Note that any _modifications_ to the schema that would normally be made (such as setting the title for model fields)
  2154. will still be performed.
  2155. If `mode` is set this will only apply to that schema generation mode, allowing you
  2156. to set different json schemas for validation and serialization.
  2157. """
  2158. json_schema: JsonSchemaValue | None
  2159. mode: Literal['validation', 'serialization'] | None = None
  2160. def __get_pydantic_json_schema__(
  2161. self, core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler
  2162. ) -> JsonSchemaValue:
  2163. mode = self.mode or handler.mode
  2164. if mode != handler.mode:
  2165. return handler(core_schema)
  2166. if self.json_schema is None:
  2167. # This exception is handled in pydantic.json_schema.GenerateJsonSchema._named_required_fields_schema
  2168. raise PydanticOmit
  2169. else:
  2170. return self.json_schema.copy()
  2171. def __hash__(self) -> int:
  2172. return hash(type(self.mode))
  2173. class Examples:
  2174. """Add examples to a JSON schema.
  2175. If the JSON Schema already contains examples, the provided examples
  2176. will be appended.
  2177. If `mode` is set this will only apply to that schema generation mode,
  2178. allowing you to add different examples for validation and serialization.
  2179. """
  2180. @overload
  2181. @deprecated('Using a dict for `examples` is deprecated since v2.9 and will be removed in v3.0. Use a list instead.')
  2182. def __init__(
  2183. self, examples: dict[str, Any], mode: Literal['validation', 'serialization'] | None = None
  2184. ) -> None: ...
  2185. @overload
  2186. def __init__(self, examples: list[Any], mode: Literal['validation', 'serialization'] | None = None) -> None: ...
  2187. def __init__(
  2188. self, examples: dict[str, Any] | list[Any], mode: Literal['validation', 'serialization'] | None = None
  2189. ) -> None:
  2190. if isinstance(examples, dict):
  2191. warnings.warn(
  2192. 'Using a dict for `examples` is deprecated, use a list instead.',
  2193. PydanticDeprecatedSince29,
  2194. stacklevel=2,
  2195. )
  2196. self.examples = examples
  2197. self.mode = mode
  2198. def __get_pydantic_json_schema__(
  2199. self, core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler
  2200. ) -> JsonSchemaValue:
  2201. mode = self.mode or handler.mode
  2202. json_schema = handler(core_schema)
  2203. if mode != handler.mode:
  2204. return json_schema
  2205. examples = json_schema.get('examples')
  2206. if examples is None:
  2207. json_schema['examples'] = to_jsonable_python(self.examples)
  2208. if isinstance(examples, dict):
  2209. if isinstance(self.examples, list):
  2210. warnings.warn(
  2211. 'Updating existing JSON Schema examples of type dict with examples of type list. '
  2212. 'Only the existing examples values will be retained. Note that dict support for '
  2213. 'examples is deprecated and will be removed in v3.0.',
  2214. UserWarning,
  2215. )
  2216. json_schema['examples'] = to_jsonable_python(
  2217. [ex for value in examples.values() for ex in value] + self.examples
  2218. )
  2219. else:
  2220. json_schema['examples'] = to_jsonable_python({**examples, **self.examples})
  2221. if isinstance(examples, list):
  2222. if isinstance(self.examples, list):
  2223. json_schema['examples'] = to_jsonable_python(examples + self.examples)
  2224. elif isinstance(self.examples, dict):
  2225. warnings.warn(
  2226. 'Updating existing JSON Schema examples of type list with examples of type dict. '
  2227. 'Only the examples values will be retained. Note that dict support for '
  2228. 'examples is deprecated and will be removed in v3.0.',
  2229. UserWarning,
  2230. )
  2231. json_schema['examples'] = to_jsonable_python(
  2232. examples + [ex for value in self.examples.values() for ex in value]
  2233. )
  2234. return json_schema
  2235. def __hash__(self) -> int:
  2236. return hash(type(self.mode))
  2237. def _get_all_json_refs(item: Any) -> set[JsonRef]:
  2238. """Get all the definitions references from a JSON schema."""
  2239. refs: set[JsonRef] = set()
  2240. stack = [item]
  2241. while stack:
  2242. current = stack.pop()
  2243. if isinstance(current, dict):
  2244. for key, value in current.items():
  2245. if key == 'examples' and isinstance(value, list):
  2246. # Skip examples that may contain arbitrary values and references
  2247. # (e.g. `{"examples": [{"$ref": "..."}]}`). Note: checking for value
  2248. # of type list is necessary to avoid skipping valid portions of the schema,
  2249. # for instance when "examples" is used as a property key. A more robust solution
  2250. # could be found, but would require more advanced JSON Schema parsing logic.
  2251. continue
  2252. if key == '$ref' and isinstance(value, str):
  2253. refs.add(JsonRef(value))
  2254. elif isinstance(value, dict):
  2255. stack.append(value)
  2256. elif isinstance(value, list):
  2257. stack.extend(value)
  2258. elif isinstance(current, list):
  2259. stack.extend(current)
  2260. return refs
  2261. AnyType = TypeVar('AnyType')
  2262. if TYPE_CHECKING:
  2263. SkipJsonSchema = Annotated[AnyType, ...]
  2264. else:
  2265. @dataclasses.dataclass(**_internal_dataclass.slots_true)
  2266. class SkipJsonSchema:
  2267. """!!! abstract "Usage Documentation"
  2268. [`SkipJsonSchema` Annotation](../concepts/json_schema.md#skipjsonschema-annotation)
  2269. Add this as an annotation on a field to skip generating a JSON schema for that field.
  2270. Example:
  2271. ```python
  2272. from pprint import pprint
  2273. from typing import Union
  2274. from pydantic import BaseModel
  2275. from pydantic.json_schema import SkipJsonSchema
  2276. class Model(BaseModel):
  2277. a: Union[int, None] = None # (1)!
  2278. b: Union[int, SkipJsonSchema[None]] = None # (2)!
  2279. c: SkipJsonSchema[Union[int, None]] = None # (3)!
  2280. pprint(Model.model_json_schema())
  2281. '''
  2282. {
  2283. 'properties': {
  2284. 'a': {
  2285. 'anyOf': [
  2286. {'type': 'integer'},
  2287. {'type': 'null'}
  2288. ],
  2289. 'default': None,
  2290. 'title': 'A'
  2291. },
  2292. 'b': {
  2293. 'default': None,
  2294. 'title': 'B',
  2295. 'type': 'integer'
  2296. }
  2297. },
  2298. 'title': 'Model',
  2299. 'type': 'object'
  2300. }
  2301. '''
  2302. ```
  2303. 1. The integer and null types are both included in the schema for `a`.
  2304. 2. The integer type is the only type included in the schema for `b`.
  2305. 3. The entirety of the `c` field is omitted from the schema.
  2306. """
  2307. def __class_getitem__(cls, item: AnyType) -> AnyType:
  2308. return Annotated[item, cls()]
  2309. def __get_pydantic_json_schema__(
  2310. self, core_schema: CoreSchema, handler: GetJsonSchemaHandler
  2311. ) -> JsonSchemaValue:
  2312. raise PydanticOmit
  2313. def __hash__(self) -> int:
  2314. return hash(type(self))
  2315. def _get_typed_dict_config(cls: type[Any] | None) -> ConfigDict:
  2316. if cls is not None:
  2317. try:
  2318. return _decorators.get_attribute_from_bases(cls, '__pydantic_config__')
  2319. except AttributeError:
  2320. pass
  2321. return {}
  2322. def _get_ser_schema_for_default_value(schema: CoreSchema) -> core_schema.PlainSerializerFunctionSerSchema | None:
  2323. """Get a `'function-plain'` serialization schema that can be used to serialize a default value.
  2324. This takes into account having the serialization schema nested under validation schema(s).
  2325. """
  2326. if (
  2327. (ser_schema := schema.get('serialization'))
  2328. and ser_schema['type'] == 'function-plain'
  2329. and not ser_schema.get('info_arg')
  2330. ):
  2331. return ser_schema
  2332. if _core_utils.is_function_with_inner_schema(schema):
  2333. return _get_ser_schema_for_default_value(schema['schema'])