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

Dependency overriding: validation error #150

Open
jonathanmach opened this issue Nov 24, 2024 · 0 comments
Open

Dependency overriding: validation error #150

jonathanmach opened this issue Nov 24, 2024 · 0 comments

Comments

@jonathanmach
Copy link

jonathanmach commented Nov 24, 2024

Hi!

I have the following experiment, which works exactly as expected 🎉 :
Both webhooks_service and body get injected 👍

# main.py

@webhooks_api.route("/exp2", methods=["POST"])
@inject
def my_webhook2(
    webhooks_service: WebhookService = Depends(get_webhook_service),
    body: WebhookRequest = Validate(),
):
    webhooks_service.process_webhook(body.payload)
    return "Webhook processed successfully"

However, during tests, when trying to override the get_webhook_service dependency:

class FakeWebhookService(WebhookService):
    def process_webhook(self, payload):
        print("Fake processing webhook:", payload)


def get_fake_webhook_service():
    return FakeWebhookService()


@pytest.fixture
def test_app():
    flask_app = create_app()

    # Override the dependency
    dependency_provider.override(get_webhook_service, get_fake_webhook_service)
    # or
    # dependency_provider.dependency_overrides[original_dependency] = override_dependency

    with flask_app.test_client() as test_client:
        yield test_client


def test_dependency_overriding_exp2(test_app):
    # Exp2: Uses Annotated
    res = test_app.post("/exp2", json={"payload": {"foo": "bar"}})
    assert res.status_code == 200, res.data

I get the following error:

      Error: pydantic_core._pydantic_core.ValidationError: 1 validation error for my_webhook2
      webhooks_service
      Input should be an instance of WebhookService [type=is_instance_of, input_value=<test_app_3_fastdepends.g...bject at 0xffff9bc4eaa0>, input_type=get_fake_webhook_service.<locals>.FakeWebhookService]
      For further information visit https://errors.pydantic.dev/2.9/v/is_instance_of

NB: I noticed that the same issue doesn't happen on FastAPI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant