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

KeyError in sanic_ext's clean_data function during /v1/api/user/register route handling #3029

Open
1 task done
764069075 opened this issue Jan 14, 2025 · 1 comment
Open
1 task done
Labels

Comments

@764069075
Copy link

764069075 commented Jan 14, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When processing the /v1/api/user/register route, a KeyError: 'a' occurs during the validation process of sanic_ext. Specifically, in the clean_data function located in sanic_ext/extras/validation/clean.py, the code attempts to process each key-value pair of the request data using hints[key], but the hints dictionary does not contain the key 'a'. This indicates that 'a' is an extra field, and the sanic_ext validation flow does not handle this extra field correctly. Although extra = Extra.ignore might have been set in the UserRegisterSchema, the internal validation logic of sanic_ext in the clean_data function does not adhere to this setting.

Code snippet

from pydantic import BaseModel

class UserRegisterSchema(BaseModel):
class Config:
extra = ‘ignore’

@user_bp.post("/register")
@Validate(form= UserRegisterSchema)
async def register_user_handle(request: Request, body: UserRegisterSchema) -> HTTPResponse:

Expected Behavior

The sanic_ext validation process should handle extra fields according to the extra = Extra.ignore setting in the UserRegisterSchema, and no KeyError should occur. The clean_data function should either skip extra fields or handle them gracefully without raising a KeyError.

How do you run Sanic?

Sanic CLI

Operating System

Windows

Sanic Version

24.12.0

Additional context

  1. Set up a Sanic application with sanic_ext installed.
  2. Define a UserRegisterSchema with extra = Extra.ignore in its Config class.
  3. Create a route /v1/api/user/register with validation using sanic_ext.
  4. Send a request to /v1/api/user/register that includes an extra field 'a' in the request data.
  5. Observe the error when sanic_ext attempts to validate the request data in the clean_data function.
@764069075 764069075 added the bug label Jan 14, 2025
@goodki-d
Copy link

This issue rather belongs to https://github.com/sanic-org/sanic-ext repo.
Also this seems duplicate of sanic-org/sanic-ext#247.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants