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

2
fastapi/routing.py

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

4
tests/test_serialize_response_model.py

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

Loading…
Cancel
Save