Browse Source

Support older versions

pull/11634/head
Alex Couper 1 year ago
parent
commit
45b3e76e53
  1. 3
      fastapi/_compat.py
  2. 2
      fastapi/routing.py
  3. 4
      tests/test_serialize_response_model.py

3
fastapi/_compat.py

@ -10,6 +10,7 @@ from typing import (
FrozenSet,
List,
Mapping,
Optional,
Sequence,
Set,
Tuple,
@ -143,7 +144,7 @@ if PYDANTIC_V2:
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
context: dict[str, Any] | None = None,
context: Optional[Dict[str, Any]]= None,
) -> Any:
# What calls this code passes a value that already called
# self._type_adapter.validate_python(value)

2
fastapi/routing.py

@ -130,7 +130,7 @@ async def serialize_response(
exclude_defaults: bool = False,
exclude_none: bool = False,
is_coroutine: bool = True,
context: dict[str, Any] | None = None,
context: Optional[Dict[str, Any]]= None,
) -> Any:
if field:
errors = []

4
tests/test_serialize_response_model.py

@ -3,7 +3,7 @@ from typing import Dict, List, Optional
import pytest
from fastapi import FastAPI
from fastapi._compat import PYDANTIC_VERSION
from pydantic import BaseModel, Field, SerializationInfo, model_serializer
from pydantic import BaseModel, Field, model_serializer
from starlette.testclient import TestClient
app = FastAPI()
@ -15,7 +15,7 @@ class Item(BaseModel):
owner_ids: Optional[List[int]] = None
@model_serializer(mode="wrap")
def _serialize(self, handler, info: SerializationInfo | None = None):
def _serialize(self, handler, info):
data = handler(self)
if info.context and info.context.get("mode") == "FASTAPI":
if "price" in data:

Loading…
Cancel
Save