sanic-org / sanic-ext

Extended Sanic functionality
https://sanic.dev/en/plugins/sanic-ext/getting-started.html
MIT License
50 stars 36 forks source link

[Bug] Sanic validation extension failed to validate aliased pydantic model field #250

Open Rileghft opened 5 months ago

Rileghft commented 5 months ago

Describe the bug Sanic validation extension failed to validate aliased field name in query parameter, and original non-aliased field name passed validation. This is not a pydantic validation failure (both aliased and non-aliased field name passed validation when using model_validate method).

Screenshots image

To Reproduce

from sanic import Sanic, response, request
from pydantic import BaseModel, ConfigDict, Field
from pydantic.alias_generators import to_camel
from sanic_ext import validate

class TestQuery(BaseModel):
    model_config = ConfigDict(
        alias_generator=to_camel,
        populate_by_name=True
    )
    request_uri: str = Field(str, title="request path", description="API path", example='/test', required=True)

app = Sanic("Example")

@app.get('/test')
@validate(query=TestQuery)
async def handler(request: request.Request, query: TestQuery):
    return response.empty()

if __name__ == '__main__':
    app.run()
# Unexpected failure. Return 500
curl <the-url>/test?requertUri=/test

# Works as expected. Return 204
curl <the-url>/test?requert_uri=/test

Expected behavior

Sanic validation extension need to check aliased field name.

# Expected behviour is to return 204
curl <the-url>/test?requertUri=/test

Environment (please complete the following information):

OS: Ubuntu Browser: N/A Version sanic==23.12.1 sanic-ext==23.12.0 sanic-routing==23.12.0

Related issue https://github.com/sanic-org/sanic-ext/issues/247