dmontagu / fastapi-utils

Reusable utilities for FastAPI
MIT License
1.89k stars 165 forks source link

[BUG] Depends not execute when use Request directly #268

Open wangxin688 opened 1 year ago

wangxin688 commented 1 year ago

Test Code:

from fastapi import Request, FastAPI, Depends, APIRouter
from fastapi_utils.cbv import cbv

def get_locale(request: Request):
    return request.headers.get("locale", "en_US")

router = APIRouter()

@router.get("/test")
def get_test(locale=Depends(get_locale)):
    return {"locale": locale}

@cbv(router)
class TestRouter:
    locale = Depends(get_locale)

    @router.get("/test1")
    def get_test_cbv(self):
        print(self.locale)
        return {"locale": self.locale}

app =FastAPI()

app.include_router(router, prefix="")

result of test is {"locale": "en_US"} result of test1 is { "locale": { "dependency": {}, "use_cache": true } }

Depends was not execute as print info:

INFO:     Uvicorn running on http://127.0.0.1:8080 (Press CTRL+C to quit)
Depends(get_locale)
INFO:     127.0.0.1:56462 - "GET /test1 HTTP/1.1" 200 OK
Depends(get_locale)
INFO:     127.0.0.1:56462 - "GET /test1 HTTP/1.1" 200 OK
wangxin688 commented 1 year ago

@dmontagu May I ask is the project still alive?

dmontagu commented 1 year ago

@wangxin688 I haven't looked at this in a while, but I will try to find some time in the near future (hopefully the next week or two, feel free to keep pinging me) to try to bring this up-to-date with more recent versions of fastapi, pydantic, etc., and address bugs.

wangxin688 commented 1 year ago

Thanks. That will be great! Also sqlalchemy need to bump to 2.x version since a lot of excellent features bring out to us

wangxin688 commented 1 year ago

Test Code:

from fastapi import Request, FastAPI, Depends, APIRouter
from fastapi_utils.cbv import cbv

def get_locale(request: Request):
    return request.headers.get("locale", "en_US")

router = APIRouter()

@router.get("/test")
def get_test(locale=Depends(get_locale)):
    return {"locale": locale}

@cbv(router)
class TestRouter:
    locale = Depends(get_locale)

    @router.get("/test1")
    def get_test_cbv(self):
        print(self.locale)
        return {"locale": self.locale}

app =FastAPI()

app.include_router(router, prefix="")

result of test is {"locale": "en_US"} result of test1 is { "locale": { "dependency": {}, "use_cache": true } }

Depends was not execute as print info:

INFO:     Uvicorn running on http://127.0.0.1:8080 (Press CTRL+C to quit)
Depends(get_locale)
INFO:     127.0.0.1:56462 - "GET /test1 HTTP/1.1" 200 OK
Depends(get_locale)
INFO:     127.0.0.1:56462 - "GET /test1 HTTP/1.1" 200 OK

I just figured it out with implementing a locale context middleware. But I don't know it's a bug or something else. The source of cbv is really hard to understand for me, sadlly.

wangxin688 commented 1 year ago

@dmontagu how about now?

wangxin688 commented 1 year ago

one more things. Fastapi 0.95.0 is supporting Annoted now, which is not working in cbv router.

dmontagu commented 1 year ago

Working on updating fastapi-utils to work with latest versions of everything and get a new release out today. Will then look at the open PRs and issues and try to get some things fixed. Hoping to get to that this evening, if not, then during this upcoming week.