igorbenav / fastcrud

FastCRUD is a Python package for FastAPI, offering robust async CRUD operations and flexible endpoint creation utilities.
MIT License
643 stars 45 forks source link

Fix handling dependencies #53

Closed igorbenav closed 4 months ago

igorbenav commented 5 months ago

Describe the bug or question In the docs, we have something like

router = crud_router(
    ...
    read_deps=[get_current_user],
    update_deps=[get_current_user],
    ...
)

When actually the correct way would be:

from fastapi import Depends

router = crud_router(
    ...
    read_deps=[Depends(get_current_user)],
    update_deps=[Depends(get_current_user)],
    ...
)

I'd like some opinion if possible on whether we should fix the docs or change the way it works. The latter is more explicit, but I think the former might be simpler for the end user.

If the former is used, stuff like getting non-callable objects or dependencies that are already wrapped must be handled.

JakNowy commented 5 months ago

Both approaches are acceptable in my opinion, but I would personally prefer the former (without Depends). Let me just define what dependency is required and make the library handle it for me as needed. That's also less verbose.

JakNowy commented 5 months ago

Also note that in the newest version of tiangolo fastapi template it's not required to wrap the deps with Depends in route parameters. He leverages Dependency annotations instead. Maybe we could support both cases, by conditionally wraping deps when needed?

igorbenav commented 5 months ago

Just noticed I wrote what was here on the wrong place

igorbenav commented 5 months ago

Also note that in the newest version of tiangolo fastapi template it's not required to wrap the deps with Depends in route parameters.

Yeah, this would definitely be a more elegant approach, but it's still more work to the user.

Maybe we could support both cases, by conditionally wraping deps when needed?

I think we should pick something and stick to it, support it in the best possible way. One flexibility we could allow is:

We get a sequence of functions, each function might have a type hint, if it does, we use the dependency annotation with the custom type, if not, we do it with Callable.