-
-
Notifications
You must be signed in to change notification settings - Fork 35
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 query validation with extra data not defined in model raise's KeyError #247
Comments
Any updates on this issue?. Urgent resolution is required. |
Should be a one liner fix. # In sanic_ext/extras/validation/clean.py
def clean_data(model: Type[object], data: Dict[str, Any]) -> Dict[str, Any]:
hints = get_type_hints(model)
# was - return {key: _coerce(hints[key], value) for key, value in data.items()}
return { key: _coerce(hints[key], value) for key, value in data.items() if key in hints } |
I do not think that the proposed one-liner fix above fully solves the problem, as it'd just ignore all extra fields. While this is the expected behavior for models like
it wouldn't be correct for
which should fail with a validation error for unknown properties. |
I took a stab at fixing this in #259 Do note that this fix only solves these issues for models inheriting from In the dataclass case, the |
And not work for pydantic v2. |
Describe the bug
Using a pydantic model for query parameter validation fails if extra query parameters are passed to the route. This is not a pydantic validation failure (as by default pydantic ignores extra data passed to the model initializer).
Screenshots
To Reproduce
Expected behavior
Pydantic has model based settings for handling extra data being passed to model initializer. Let it handle the extra data accordingly
https://docs.pydantic.dev/2.7/api/config/#pydantic.config.ConfigDict.extra
Environment (please complete the following information):
sanic==23.12.0
sanic-ext==23.12.0
sanic-routing==23.12.0
The text was updated successfully, but these errors were encountered: