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] Sanic validation extension failed to validate aliased pydantic model field #250

Open
Rileghft opened this issue Jun 6, 2024 · 0 comments

Comments

@Rileghft
Copy link

Rileghft commented Jun 6, 2024

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
#247

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