bauerji / flask-pydantic

flask extension for integration with the awesome pydantic package
MIT License
344 stars 56 forks source link

Using flask-pydantic with Python Dependency Injection Framework #49

Open akhundMurad opened 2 years ago

akhundMurad commented 2 years ago

Hi! I am using this module with Python Dependency Injection Framework and when i try to use Provider in kwargs of the view i am getting: RuntimeError: no validator found for <class '...'>, see arbitrary_types_allowed in Config Reason of this error is the fact that @validate decorator ignores only kwargs named as "query", "body", "return". It would be better if @validate decorator ignored kwargs which default is Provide instance

Sample code:

@bp.route('/api/posts', methods=['GET'])
@inject
@validate(response_many=True)
def get_posts(
        service: PostService = Provide[Container.services.post_service]
) -> List[ReadPost]:
    ...
guywilsonjr commented 2 years ago

So it's saying PostService has no methods with @validate. Can you show the PostService class?

akhundMurad commented 2 years ago

İt is because of PostService class is not Pydantic model. PostService class is just simple object with crud

davidhariri commented 2 years ago

I recently started getting this erorr RuntimeError: no validator found for <class 'models.client.Client'>, see arbitrary_types_allowed in Config as well. Did something in pydantic or flask_pydantic change? The following code used to work just fine:

from flask_pydantic import validate

@blueprint.route("/clone", subdomain="<subdomain>", methods=["POST"])
@inject_client()
@validate()
def clone_client_endpoint(
    body: ClientCloneReq, # A Pydantic model
    client: Client, # not a Pydantic model, comes from @inject_client()
    **kwargs
):
akhundMurad commented 2 years ago

I think this issue not only about Python Dependency Injection Framework. This error appears when we use not Pydantic models in annotations

davidhariri commented 2 years ago

I think this issue not only about Python Dependency Injection Framework. This error appears when we use not Pydantic models in annotations

Yeah, just weird that this is broken now. Definitely worked before we upgraded from 0.4 to 0.9.

piotrcybulski-profl commented 1 year ago

I had the same issue and did a little investigation. The problem seems to be with validate_path_params method in validate decorator. That was introduced in 0.7.0 version, so it tell why David didn't have the issue with version 0.4.0. The issue is, it takes path parameters from the method arguments instead of route as it should.

maldoinc commented 4 months ago

Encountering the same issue. We use Wireup for DI and flask-pydantic as well. Sadly can't use both on the same view as flask-pydantic will throw.