Skip to content

Pydantic Warning Fix - CacheManager Default Values

Issue

Some older API routes used CacheManager objects as default dependency values. That caused Pydantic to warn during OpenAPI schema generation because the default object was not JSON serializable.

Fix

The current pattern uses a factory function in app/api/deps.py:

def get_cache_manager_factory(model: Type[T]):
    def _get_cache_manager() -> CacheManager[T]:
        return CacheManager(redis_client, model)
    return _get_cache_manager

Replace:

_cache_manager = Depends(lambda: deps.get_cache_manager(SomeModel))

With:

_cache_manager = Depends(deps.get_cache_manager_factory(SomeModel))

Affected Routes

The factory pattern was applied to the following route files:

  • app/api/routes/brands.py
  • app/api/routes/locations.py
  • app/api/routes/delivery_locations.py
  • app/api/routes/trackers/listing.py
  • app/api/routes/trackers/crud.py
  • app/api/routes/trackers/status.py
  • app/api/routes/trackers/csv_import.py

Result

  • The warning no longer applies to the updated routes
  • Health API routes are unaffected because they do not use CacheManager
  • OpenAPI schema generation is cleaner and more predictable