33import typing
44from collections .abc import Callable
55from http import HTTPStatus
6- from typing import Any , Dict , ClassVar
6+ from typing import Any , ClassVar
77
88from pydantic import BaseModel
99
@@ -43,8 +43,9 @@ class BaseRouter:
4343 It can include sub-routers and generate an OpenAPI specification from
4444 the declared routes.
4545 """
46+
4647 # Class-level cache for model schemas to avoid redundant processing
47- _model_schema_cache : ClassVar [Dict [str , dict ]] = {}
48+ _model_schema_cache : ClassVar [dict [str , dict ]] = {}
4849
4950 def __init__ (
5051 self ,
@@ -264,31 +265,31 @@ def _serialize_response(result: Any) -> Any:
264265 @classmethod
265266 def _get_model_schema (cls , model : type [BaseModel ], definitions : dict ) -> dict :
266267 """
267- Get the OpenAPI schema for a Pydantic model, with caching for better performance.
268+ Get the OpenAPI schema for a Pydantic model, with caching for better performance
268269 """
269270 model_name = model .__name__
270271 cache_key = f"{ model .__module__ } .{ model_name } "
271-
272+
272273 # Check if the schema is already in the class-level cache
273274 if cache_key not in cls ._model_schema_cache :
274275 # Generate the schema if it's not in the cache
275276 model_schema = model .model_json_schema (
276277 ref_template = "#/components/schemas/{model}"
277278 )
278-
279+
279280 # Process and store nested definitions
280281 for key in ("definitions" , "$defs" ):
281282 if key in model_schema :
282283 definitions .update (model_schema [key ])
283284 del model_schema [key ]
284-
285+
285286 # Add schema to the cache
286287 cls ._model_schema_cache [cache_key ] = model_schema
287-
288+
288289 # Make sure the schema is in the definitions dictionary
289290 if model_name not in definitions :
290291 definitions [model_name ] = cls ._model_schema_cache [cache_key ]
291-
292+
292293 return {"$ref" : f"#/components/schemas/{ model_name } " }
293294
294295 @staticmethod
@@ -370,4 +371,4 @@ def resolve_endpoint_params(
370371 def openapi (self ) -> dict :
371372 if self ._openapi_schema is None :
372373 self ._openapi_schema = self .generate_openapi ()
373- return self ._openapi_schema
374+ return self ._openapi_schema
0 commit comments