Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions fastapi_cache/coder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@

import pendulum
from fastapi.encoders import jsonable_encoder
from pydantic import BaseConfig, ValidationError, fields
from pydantic import BaseConfig, ValidationError
from starlette.responses import JSONResponse
from starlette.templating import (
_TemplateResponse as TemplateResponse, # pyright: ignore[reportPrivateUsage]
)

from pydantic.version import VERSION as PYDANTIC_VERSION

PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.")

if PYDANTIC_V2:
from fastapi._compat import ModelField
else:
from pydantic.fields import ModelField


_T = TypeVar("_T", bound=type)


Expand Down Expand Up @@ -69,7 +79,7 @@ def decode(cls, value: bytes) -> Any:
# decode_as_type method and then stores a different kind of field for a
# given type, do make sure that the subclass provides its own class
# attribute for this cache.
_type_field_cache: ClassVar[Dict[Any, fields.ModelField]] = {}
_type_field_cache: ClassVar[Dict[Any, ModelField]] = {}

@overload
@classmethod
Expand All @@ -93,7 +103,7 @@ def decode_as_type(cls, value: bytes, *, type_: Optional[_T]) -> Union[_T, Any]:
try:
field = cls._type_field_cache[type_]
except KeyError:
field = cls._type_field_cache[type_] = fields.ModelField(
field = cls._type_field_cache[type_] = ModelField(
name="body", type_=type_, class_validators=None, model_config=BaseConfig
)
result, errors = field.validate(result, {}, loc=())
Expand Down