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] validate should check passed in model type #257

Open
Pirulax opened this issue Aug 6, 2024 · 2 comments
Open

[Bug] validate should check passed in model type #257

Pirulax opened this issue Aug 6, 2024 · 2 comments

Comments

@Pirulax
Copy link

Pirulax commented Aug 6, 2024

Describe the bug
The validate function should check the type of the passed in schema.
In my case doing this:

import sanic as s
from sanic_ext import validate

class MyRESTMethodArgs:
    id : str
@bp.post('/create')
@validate(json=MyRESTMethodArgs)
async def post_create(req : s.Request, body : MyRESTMethodArgs):
    pass # Business logic

Fails miserably with:

File "<irrelevant>\.venv\Lib\site-packages\sanic_ext\extras\validation\decorator.py", line 73, in decorated_function
    retval = f(*args, **kwargs)

Inspecting it closer, it turns out schemas looks like this:

{'json': {}, 'form': None, 'query': None}

I assume the issue is with generate_schema for some reason just returning {} instead of raising an exception

To Reproduce
See above

Expected behavior
validate should fail if it encounters a schema model it can't process (As according to the docs, a simple Python class can't be used)

Environment (please complete the following information):
mode: debug, single worker
server: sanic, HTTP/1.1
python: 3.12.4
platform: Windows-11-10.0.22631-SP0
packages: sanic-routing==23.12.0, sanic-ext==23.12.0

@goodki-d
Copy link

goodki-d commented Jan 8, 2025

Yeah this behaviour checks out looking at internal make_schema function:

def make_schema(agg, item):
if type(item) in (bool, str, int, float):
return agg
if is_generic(item) and (args := get_args(item)):
for arg in args:
make_schema(agg, arg)
elif item.__name__ not in agg and (
is_dataclass(item) or is_attrs(item) or is_msgspec(item)
):
if is_dataclass(item):
fields = item.__dataclass_fields__
elif is_msgspec(item):
fields = {f.name: f.type for f in msgspec_type_info(item).fields}
else:
fields = {attr.name: attr for attr in item.__attrs_attrs__}
sig = signature(item)
hints = parse_hints(get_type_hints(item), fields)
agg[item.__name__] = {
"sig": sig,
"hints": hints,
}
for hint in hints.values():
make_schema(agg, hint.hint)
return agg

That is, passing a bare python class will not raise error but return the empty dict.

@goodki-d
Copy link

goodki-d commented Jan 8, 2025

validate should fail if it encounters a schema model it can't process (As according to the docs, a simple Python class can't be used)

I think only pydantic, msgspec, attrs and dataclasses are supported

We can add a check here

def generate_schema(param):
try:
if param is None or is_msgspec(param) or is_pydantic(param):
return param
except TypeError:
...
return make_schema({}, param) if isclass(param) else param

maybe like

+ if isclass(param) and not is_attrs(param) and not is_dataclass(param):
+       raise SchemaNotSupported

return make_schema({}, param) if isclass(param) else param

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

2 participants