encoders.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import dataclasses
  2. import datetime
  3. from collections import defaultdict, deque
  4. from collections.abc import Callable
  5. from decimal import Decimal
  6. from enum import Enum
  7. from ipaddress import (
  8. IPv4Address,
  9. IPv4Interface,
  10. IPv4Network,
  11. IPv6Address,
  12. IPv6Interface,
  13. IPv6Network,
  14. )
  15. from pathlib import Path, PurePath
  16. from re import Pattern
  17. from types import GeneratorType
  18. from typing import Annotated, Any
  19. from uuid import UUID
  20. from annotated_doc import Doc
  21. from fastapi.exceptions import PydanticV1NotSupportedError
  22. from fastapi.types import IncEx
  23. from pydantic import BaseModel
  24. from pydantic.networks import AnyUrl, NameEmail
  25. from pydantic.types import SecretBytes, SecretStr
  26. from pydantic_core import PydanticUndefinedType
  27. from ._compat import (
  28. Url,
  29. is_pydantic_v1_model_instance,
  30. )
  31. try:
  32. # pydantic.color.Color is deprecated since v2.0b3, but supporting for bwd-compat
  33. from pydantic.color import Color # ty: ignore[deprecated]
  34. except ImportError: # pragma: no cover
  35. class Color: # type: ignore[no-redef]
  36. pass
  37. try:
  38. # Supporting the new Color format for newer versions of Pydantic
  39. from pydantic_extra_types.color import Color as PyExtraColor
  40. except ImportError: # pragma: no cover
  41. class PyExtraColor: # type: ignore[no-redef]
  42. pass
  43. # Taken from Pydantic v1 as is
  44. def isoformat(o: datetime.date | datetime.time) -> str:
  45. return o.isoformat()
  46. # Adapted from Pydantic v1
  47. # TODO: pv2 should this return strings instead?
  48. def decimal_encoder(dec_value: Decimal) -> int | float:
  49. """
  50. Encodes a Decimal as int if there's no exponent, otherwise float
  51. This is useful when we use ConstrainedDecimal to represent Numeric(x,0)
  52. where an integer (but not int typed) is used. Encoding this as a float
  53. results in failed round-tripping between encode and parse.
  54. Our Id type is a prime example of this.
  55. >>> decimal_encoder(Decimal("1.0"))
  56. 1.0
  57. >>> decimal_encoder(Decimal("1"))
  58. 1
  59. >>> decimal_encoder(Decimal("NaN"))
  60. nan
  61. """
  62. exponent = dec_value.as_tuple().exponent
  63. if isinstance(exponent, int) and exponent >= 0:
  64. return int(dec_value)
  65. else:
  66. return float(dec_value)
  67. ENCODERS_BY_TYPE: dict[type[Any], Callable[[Any], Any]] = {
  68. bytes: lambda o: o.decode(),
  69. Color: str,
  70. PyExtraColor: str,
  71. datetime.date: isoformat,
  72. datetime.datetime: isoformat,
  73. datetime.time: isoformat,
  74. datetime.timedelta: lambda td: td.total_seconds(),
  75. Decimal: decimal_encoder,
  76. Enum: lambda o: o.value,
  77. frozenset: list,
  78. deque: list,
  79. GeneratorType: list,
  80. IPv4Address: str,
  81. IPv4Interface: str,
  82. IPv4Network: str,
  83. IPv6Address: str,
  84. IPv6Interface: str,
  85. IPv6Network: str,
  86. NameEmail: str,
  87. Path: str,
  88. Pattern: lambda o: o.pattern,
  89. SecretBytes: str,
  90. SecretStr: str,
  91. set: list,
  92. UUID: str,
  93. Url: str,
  94. AnyUrl: str,
  95. }
  96. def generate_encoders_by_class_tuples(
  97. type_encoder_map: dict[Any, Callable[[Any], Any]],
  98. ) -> dict[Callable[[Any], Any], tuple[Any, ...]]:
  99. encoders_by_class_tuples: dict[Callable[[Any], Any], tuple[Any, ...]] = defaultdict(
  100. tuple
  101. )
  102. for type_, encoder in type_encoder_map.items():
  103. encoders_by_class_tuples[encoder] += (type_,)
  104. return encoders_by_class_tuples
  105. encoders_by_class_tuples = generate_encoders_by_class_tuples(ENCODERS_BY_TYPE)
  106. def jsonable_encoder(
  107. obj: Annotated[
  108. Any,
  109. Doc(
  110. """
  111. The input object to convert to JSON.
  112. """
  113. ),
  114. ],
  115. include: Annotated[
  116. IncEx | None,
  117. Doc(
  118. """
  119. Pydantic's `include` parameter, passed to Pydantic models to set the
  120. fields to include.
  121. """
  122. ),
  123. ] = None,
  124. exclude: Annotated[
  125. IncEx | None,
  126. Doc(
  127. """
  128. Pydantic's `exclude` parameter, passed to Pydantic models to set the
  129. fields to exclude.
  130. """
  131. ),
  132. ] = None,
  133. by_alias: Annotated[
  134. bool,
  135. Doc(
  136. """
  137. Pydantic's `by_alias` parameter, passed to Pydantic models to define if
  138. the output should use the alias names (when provided) or the Python
  139. attribute names. In an API, if you set an alias, it's probably because you
  140. want to use it in the result, so you probably want to leave this set to
  141. `True`.
  142. """
  143. ),
  144. ] = True,
  145. exclude_unset: Annotated[
  146. bool,
  147. Doc(
  148. """
  149. Pydantic's `exclude_unset` parameter, passed to Pydantic models to define
  150. if it should exclude from the output the fields that were not explicitly
  151. set (and that only had their default values).
  152. """
  153. ),
  154. ] = False,
  155. exclude_defaults: Annotated[
  156. bool,
  157. Doc(
  158. """
  159. Pydantic's `exclude_defaults` parameter, passed to Pydantic models to define
  160. if it should exclude from the output the fields that had the same default
  161. value, even when they were explicitly set.
  162. """
  163. ),
  164. ] = False,
  165. exclude_none: Annotated[
  166. bool,
  167. Doc(
  168. """
  169. Pydantic's `exclude_none` parameter, passed to Pydantic models to define
  170. if it should exclude from the output any fields that have a `None` value.
  171. """
  172. ),
  173. ] = False,
  174. custom_encoder: Annotated[
  175. dict[Any, Callable[[Any], Any]] | None,
  176. Doc(
  177. """
  178. Pydantic's `custom_encoder` parameter, passed to Pydantic models to define
  179. a custom encoder.
  180. """
  181. ),
  182. ] = None,
  183. sqlalchemy_safe: Annotated[
  184. bool,
  185. Doc(
  186. """
  187. Exclude from the output any fields that start with the name `_sa`.
  188. This is mainly a hack for compatibility with SQLAlchemy objects, they
  189. store internal SQLAlchemy-specific state in attributes named with `_sa`,
  190. and those objects can't (and shouldn't be) serialized to JSON.
  191. """
  192. ),
  193. ] = True,
  194. ) -> Any:
  195. """
  196. Convert any object to something that can be encoded in JSON.
  197. This is used internally by FastAPI to make sure anything you return can be
  198. encoded as JSON before it is sent to the client.
  199. You can also use it yourself, for example to convert objects before saving them
  200. in a database that supports only JSON.
  201. Read more about it in the
  202. [FastAPI docs for JSON Compatible Encoder](https://fastapi.tiangolo.com/tutorial/encoder/).
  203. """
  204. custom_encoder = custom_encoder or {}
  205. if custom_encoder:
  206. if type(obj) in custom_encoder:
  207. return custom_encoder[type(obj)](obj)
  208. else:
  209. for encoder_type, encoder_instance in custom_encoder.items():
  210. if isinstance(obj, encoder_type):
  211. return encoder_instance(obj)
  212. if include is not None and not isinstance(include, (set, dict)):
  213. include = set(include) # type: ignore[assignment] # ty: ignore[invalid-assignment]
  214. if exclude is not None and not isinstance(exclude, (set, dict)):
  215. exclude = set(exclude) # type: ignore[assignment] # ty: ignore[invalid-assignment]
  216. if isinstance(obj, BaseModel):
  217. obj_dict = obj.model_dump(
  218. mode="json",
  219. include=include,
  220. exclude=exclude,
  221. by_alias=by_alias,
  222. exclude_unset=exclude_unset,
  223. exclude_none=exclude_none,
  224. exclude_defaults=exclude_defaults,
  225. )
  226. return jsonable_encoder(
  227. obj_dict,
  228. exclude_none=exclude_none,
  229. exclude_defaults=exclude_defaults,
  230. sqlalchemy_safe=sqlalchemy_safe,
  231. )
  232. if dataclasses.is_dataclass(obj):
  233. assert not isinstance(obj, type)
  234. obj_dict = dataclasses.asdict(obj)
  235. return jsonable_encoder(
  236. obj_dict,
  237. include=include,
  238. exclude=exclude,
  239. by_alias=by_alias,
  240. exclude_unset=exclude_unset,
  241. exclude_defaults=exclude_defaults,
  242. exclude_none=exclude_none,
  243. custom_encoder=custom_encoder,
  244. sqlalchemy_safe=sqlalchemy_safe,
  245. )
  246. if isinstance(obj, Enum):
  247. return obj.value
  248. if isinstance(obj, PurePath):
  249. return str(obj)
  250. if isinstance(obj, (str, int, float, type(None))):
  251. return obj
  252. if isinstance(obj, PydanticUndefinedType):
  253. return None
  254. if isinstance(obj, dict):
  255. encoded_dict = {}
  256. allowed_keys = set(obj.keys())
  257. if include is not None:
  258. allowed_keys &= set(include)
  259. if exclude is not None:
  260. allowed_keys -= set(exclude)
  261. for key, value in obj.items():
  262. if (
  263. (
  264. not sqlalchemy_safe
  265. or (not isinstance(key, str))
  266. or (not key.startswith("_sa"))
  267. )
  268. and (value is not None or not exclude_none)
  269. and key in allowed_keys
  270. ):
  271. encoded_key = jsonable_encoder(
  272. key,
  273. by_alias=by_alias,
  274. exclude_unset=exclude_unset,
  275. exclude_defaults=exclude_defaults,
  276. exclude_none=exclude_none,
  277. custom_encoder=custom_encoder,
  278. sqlalchemy_safe=sqlalchemy_safe,
  279. )
  280. encoded_value = jsonable_encoder(
  281. value,
  282. by_alias=by_alias,
  283. exclude_unset=exclude_unset,
  284. exclude_defaults=exclude_defaults,
  285. exclude_none=exclude_none,
  286. custom_encoder=custom_encoder,
  287. sqlalchemy_safe=sqlalchemy_safe,
  288. )
  289. encoded_dict[encoded_key] = encoded_value
  290. return encoded_dict
  291. if isinstance(obj, (list, set, frozenset, GeneratorType, tuple, deque)):
  292. encoded_list = []
  293. for item in obj:
  294. encoded_list.append(
  295. jsonable_encoder(
  296. item,
  297. include=include,
  298. exclude=exclude,
  299. by_alias=by_alias,
  300. exclude_unset=exclude_unset,
  301. exclude_defaults=exclude_defaults,
  302. exclude_none=exclude_none,
  303. custom_encoder=custom_encoder,
  304. sqlalchemy_safe=sqlalchemy_safe,
  305. )
  306. )
  307. return encoded_list
  308. if type(obj) in ENCODERS_BY_TYPE:
  309. return ENCODERS_BY_TYPE[type(obj)](obj)
  310. for encoder, classes_tuple in encoders_by_class_tuples.items():
  311. if isinstance(obj, classes_tuple):
  312. return encoder(obj)
  313. if is_pydantic_v1_model_instance(obj):
  314. raise PydanticV1NotSupportedError(
  315. "pydantic.v1 models are no longer supported by FastAPI."
  316. f" Please update the model {obj!r}."
  317. )
  318. try:
  319. data = dict(obj)
  320. except Exception as e:
  321. errors: list[Exception] = []
  322. errors.append(e)
  323. try:
  324. data = vars(obj)
  325. except Exception as e:
  326. errors.append(e)
  327. raise ValueError(errors) from e
  328. return jsonable_encoder(
  329. data,
  330. include=include,
  331. exclude=exclude,
  332. by_alias=by_alias,
  333. exclude_unset=exclude_unset,
  334. exclude_defaults=exclude_defaults,
  335. exclude_none=exclude_none,
  336. custom_encoder=custom_encoder,
  337. sqlalchemy_safe=sqlalchemy_safe,
  338. )