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

[Bug] pydantic validation exception when validating unknown query strings #256

Closed
Panaetius opened this issue Aug 6, 2024 · 2 comments
Closed

Comments

@Panaetius
Copy link

Describe the bug
when validating query strings with @validate(query=MyModel), the code can fail with a key error before reaching actual pydantic validation if query parameters not on the model are supplied.

Specifically, https://github.com/sanic-org/sanic-ext/blob/main/sanic_ext/extras/validation/clean.py#L6 fails because data contains a key that isn't in hints, as the model doesn't have that key that was sent in data.

This will lead to a KeyError instead of a pydantic ValidationError, making it hard to deal with. In our case, we pretty format ValidationError before returning errors to the client, but KeyError will just be a generic 500 error. And we don't want to catch all KeyErrors, as there could be KeyErrors unrelated to validation as well.

Expected behavior
A pydantic ValidationError to be created (if the Model has extra = "forbid") or validation to pass (if the model has extra = "ignore")

Environment (please complete the following information):

  • OS: Arch Linux
  • Browser curl/requests
  • Version 23.12.0

Additional context
We use schemathesis to test our API, which will create many negative test cases like passing random arguments, and it expects those to fail with a 4xx error, but the issue above causes 500 errors, failing schemathesis.

@Pirulax
Copy link

Pirulax commented Aug 6, 2024

Duplicate of #247?
Also, related to validate: #257

@Panaetius
Copy link
Author

ah yes you're right. I had searched for open issues and couldn't find one for some reason

I'll close this one

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

2 participants