Skip to content

Commit

Permalink
Compare objects MRO with encoders at runtime
Browse files Browse the repository at this point in the history
The previous implementation doesn't handle subclass instances when
pydantic.json.ENCODERS_BY_TYPE is modified after fastapi.encoders import.

This diff makes it easier for developers to add custom encoders that also work
with subclass instances (and it simplifies the code, as well).
  • Loading branch information
ramnes committed Jan 7, 2023
1 parent 69bd7d8 commit bdc4ac5
Showing 1 changed file with 4 additions and 21 deletions.
25 changes: 4 additions & 21 deletions fastapi/encoders.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import dataclasses
from collections import defaultdict
from enum import Enum
from pathlib import PurePath
from types import GeneratorType
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union
from typing import Any, Callable, Dict, List, Optional, Set, Union

from pydantic import BaseModel
from pydantic.json import ENCODERS_BY_TYPE
Expand All @@ -12,20 +11,6 @@
DictIntStrAny = Dict[Union[int, str], Any]


def generate_encoders_by_class_tuples(
type_encoder_map: Dict[Any, Callable[[Any], Any]]
) -> Dict[Callable[[Any], Any], Tuple[Any, ...]]:
encoders_by_class_tuples: Dict[Callable[[Any], Any], Tuple[Any, ...]] = defaultdict(
tuple
)
for type_, encoder in type_encoder_map.items():
encoders_by_class_tuples[encoder] += (type_,)
return encoders_by_class_tuples


encoders_by_class_tuples = generate_encoders_by_class_tuples(ENCODERS_BY_TYPE)


def jsonable_encoder(
obj: Any,
include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
Expand Down Expand Up @@ -142,11 +127,9 @@ def jsonable_encoder(
)
return encoded_list

if type(obj) in ENCODERS_BY_TYPE:
return ENCODERS_BY_TYPE[type(obj)](obj)
for encoder, classes_tuple in encoders_by_class_tuples.items():
if isinstance(obj, classes_tuple):
return encoder(obj)
for base in obj.__class__.__mro__[:-1]:
if base in ENCODERS_BY_TYPE:
return ENCODERS_BY_TYPE[base](obj)

try:
data = dict(obj)
Expand Down

0 comments on commit bdc4ac5

Please sign in to comment.