-
-
Notifications
You must be signed in to change notification settings - Fork 43
Open
Description
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).
To Reproduce
from sanic import Sanic, response, request
from pydantic import BaseModel
from sanic_ext import validate
class MyQueryData(BaseModel):
name: str
app = Sanic("Example")
@app.get('/test')
@validate(query=MyQueryData)
async def handler(request: request.Request, query: MyQueryData):
print(f"{query=}")
return response.empty()
# Works as expected. Return 200 OK
curl <the-url>/test?name=abcd
# Works as expected. validation error as require params not set
curl <the-url>/test
# Unexpected failure. Return 500
curl <the-url>/test?name=abcd&another=abcd
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
# Expected behviour is to return 200 OK via silently ignoring extra data
curl <the-url>/test?name=abcd&another=abcd
Environment (please complete the following information):
- OS: Ubuntu
- Browser: N/A
- Version
sanic==23.12.0
sanic-ext==23.12.0
sanic-routing==23.12.0
Panaetius and monicaluodialpad
Metadata
Metadata
Assignees
Labels
No labels