Skip to content

Commit

Permalink
Update create_cloned_field to use a global cache
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Nov 8, 2022
1 parent 5f0e095 commit 4d7d18e
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions fastapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dataclasses import is_dataclass
from enum import Enum
from typing import TYPE_CHECKING, Any, Dict, Optional, Set, Type, Union, cast
from weakref import WeakKeyDictionary

import fastapi
from fastapi.datastructures import DefaultPlaceholder, DefaultType
Expand Down Expand Up @@ -95,11 +96,15 @@ def create_response_field(
def create_cloned_field(
field: ModelField,
*,
cloned_types: Optional[Dict[Type[BaseModel], Type[BaseModel]]] = None,
cloned_types: Optional[
Dict[Type[BaseModel], Type[BaseModel]]
] = WeakKeyDictionary(),
) -> ModelField:
# _cloned_types has already cloned types, to support recursive models
# cloned_types caches already cloned types to support recursive models and improve
# performance by avoiding unecessary cloning
if cloned_types is None:
cloned_types = {}
cloned_types = WeakKeyDictionary()

original_type = field.type_
if is_dataclass(original_type) and hasattr(original_type, "__pydantic_model__"):
original_type = original_type.__pydantic_model__
Expand Down

0 comments on commit 4d7d18e

Please sign in to comment.