Skip to content

Commit

Permalink
Cache calculated field_aliases for a schema
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-dyfis-net committed Oct 1, 2024
1 parent f26087d commit e377a84
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion litestar/openapi/spec/schema.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from dataclasses import dataclass, field, fields, is_dataclass
from typing import TYPE_CHECKING, Any, Hashable, Mapping, Sequence
from typing import TYPE_CHECKING, Any, Hashable, Mapping, Sequence, cast

from litestar.openapi.spec.base import BaseSchemaObject
from litestar.utils.predicates import is_non_string_sequence
Expand Down Expand Up @@ -649,10 +649,13 @@ def __hash__(self) -> int:

@classmethod
def field_aliases(cls) -> dict[str, str]:
if hasattr(cls, "_field_aliases"):
return cast("dict[str, str]", cls._field_aliases)
retval = {}
for field_def in fields(cls):
if field_def.metadata is not None and (field_alias := field_def.metadata.get("alias")):
retval[field_alias] = field_def.name
cls._field_aliases = retval
return retval


Expand Down

0 comments on commit e377a84

Please sign in to comment.