Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wangxin688 opened this issue Mar 2, 2023 · 7 comments
Open

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

wangxin688 opened this issue Mar 2, 2023 · 7 comments
Labels
bug Something isn't working

Comments

@wangxin688
Copy link

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 wangxin688 added the bug Something isn't working label Mar 2, 2023
@wangxin688
Copy link
Author

@dmontagu May I ask is the project still alive?

@dmontagu
Copy link
Owner

dmontagu commented Mar 3, 2023

@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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

@dmontagu how about now?

@wangxin688
Copy link
Author

wangxin688 commented Mar 19, 2023

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

@dmontagu
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants