Closed wangxin688 closed 1 year ago
Can you share the fastapi and pydantic versions?
Hello,
after a quick check it seems this code block can works you simply forgot type hint which is required for dependencies to work.
also: for your comfort you can use the Headrs object instead of request.headers so this is also added here.
from fastapi import Request, FastAPI, Depends, APIRouter, Header
from fastapi_restful.cbv import cbv
def get_locale(locale: str = Header(default=None)):
return locale
router = APIRouter()
@router.get("/test")
def show_locale(locale=Depends(get_locale)):
return {"locale": locale}
@cbv(router)
class TestRouter:
locale: str = Depends(get_locale)
@router.get("/test1")
def get_test_cbv(self):
return {"locale": self.locale}
app = FastAPI()
app.include_router(router)
It can also work with Annotated but adding a type hint is also an option
Seems about right, closing thanks to @DawnOfHell
Test Code:
result of
test
is{"locale": "en_US"}
result oftest1
is{ "locale": { "dependency": {}, "use_cache": true } }
Depends was not execute as print info: