async-lru icon indicating copy to clipboard operation
async-lru copied to clipboard

decorator is causing '422 Unprocessable Entity' error in FastAPI+Swagger

Open sinancetinkaya opened this issue 5 years ago • 1 comments

I'm learning FastAPI. One of my API endpoints is using AsyncLRU for caching some results. However if I use AsyncLRU decorator on API endpoint, FastAPI can't detect query parameters. Swagger is showing *args, **kwards as query parameters. using functools.wrap solves this problem.

class MyAsyncLRU(AsyncLRU):
    def __call__(self, func):
        @wraps(func)
        async def wrapper(*args, **kwargs):
            key = self._Key(args, kwargs)
            if key in self.lru:
                return self.lru[key]
            else:
                self.lru[key] = await func(*args, **kwargs)
                return self.lru[key]

        wrapper.__name__ += func.__name__

        return wrapper



sinancetinkaya avatar Feb 17 '21 23:02 sinancetinkaya

fastapi uses inspect to get the parameters of the endpoint function, in order to not influence fastapi, this cache decorator should be used inside the crud model of your app, I think...

synodriver avatar Jul 30 '21 07:07 synodriver

Agree.

asvetlov avatar Feb 18 '23 23:02 asvetlov