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

[FastAPI] FromDishka is being refused by Pydantic #265

Open
wadinj opened this issue Oct 11, 2024 · 3 comments
Open

[FastAPI] FromDishka is being refused by Pydantic #265

wadinj opened this issue Oct 11, 2024 · 3 comments
Labels
enhancement New feature or request integrations

Comments

@wadinj
Copy link

wadinj commented Oct 11, 2024

Hi,

I'm facing an issue when using Pydantic with FastAPI route decorated with pydantic + Dishka

FastAPIError: Invalid args for response field! Hint: check that typing.Annotated[api.repository.UserRepository, FromComponent(component='')] is a valid Pydantic field type. 
If you are using a return type annotation
that is not a valid Pydantic field (e.g. Union[Response, dict, None]) 
you can disable generating the response model from 
the type annotation with the path operation decorator parameter response_model=None. 

The error seems to be only when using FastAPI route decorator like:
@app.get("/users/{user_id}", response_model=UserDTO)

If registration is done through add_api_route, Pydantic does not raise exception anymore:
app.add_api_route("/users/{user_id}", get_user_by_id, methods=["GET"], response_model=UserDTO)

Moreover, Dishka seems to not support FastAPI in sync mode, when using an injection on a sync endpoint, it raises:
TypeError: object list can't be used in 'await' expression from dishka/integrations/base.py:192 (as my endpoint return a list of object)
The method setup_dishka seems to take only AsyncContainer as argument

@Tishka17
Copy link
Member

Did you forget @inject decorator? It is requried unless you explicitly enable autoinject which is not so trivial in fastapi.

We do not support sync mode yet. Actually, I do not think that using fastapi with sync view is a good idea - you loose all asyncio features and get its problems + additional. If you really need sync views I'd recommend using some WSGI-framerwork (not ASGI)

The problem with sync/async fastapi is mostly in mixing the both approaches. We can try to support sync-only mode, but anyway it looks like some guys will try to use async containers in sync code and vice versa.

@wadinj
Copy link
Author

wadinj commented Oct 12, 2024

I did use the @inject decorator, I think all of this come from the fact I'm using sync mode. Agree that sync mode is not ideal in fastapi, but I've a large codebase already written in sync mode with some underlying libs without asyncio support yet.

Is there any plan to support sync mode? I would be interested to bring such contrib.

@Tishka17
Copy link
Member

Tishka17 commented Oct 12, 2024

Try this code instead of builin integration. It should work with sync fastapi and sync container
https://github.com/Dark04072006/Simbir.Health/blob/main/services/account/src/account/presentation/dishka.py

@Tishka17 Tishka17 added enhancement New feature or request integrations labels Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request integrations
Projects
None yet
Development

No branches or pull requests

2 participants