async-lru
async-lru copied to clipboard
decorator is causing '422 Unprocessable Entity' error in FastAPI+Swagger
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
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...
Agree.