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 Mar 4, 2022
1 parent 440d2d2 commit 2b6ed83
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions fastapi/utils.py
Expand Up @@ -3,6 +3,7 @@
from dataclasses import is_dataclass
from enum import Enum
from typing import Any, Dict, Optional, Set, Type, Union, cast
from weakref import WeakKeyDictionary

import fastapi
from fastapi.datastructures import DefaultPlaceholder, DefaultType
Expand Down Expand Up @@ -72,11 +73,13 @@ 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 = dict()
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 2b6ed83

Please sign in to comment.