Closed 0xlakshay closed 9 months ago
Other than this maybe we can modify the behaviour of sanic.request.parameters.RequestParameters
but I am not completely sure about this.
I think sanic.request.parameters.RequestParameters
already tries to achieve something similar but it doesn't work here while validation the query dict with models. Also should we modify its behaviour validation usecase also needs discussion.
Can I raise a PR with FIx 1 or 2? For now we have to resort to a custom query validator decorator which is not fun.
Oops! found this https://github.com/sanic-org/sanic-ext/pull/157, much cleaner solution Seems to be working fine in latest version! closing this
Query params validation is completely broken. I'm surprised this hasn't been reported yet.
Sanic uses
urllib.parse.parse_qs
to parse query params into a dictionary. The parsed params can be accessed usingRequest.args
This works fine for everything but validation. But why? Becauseurllib.parse.parse_qs
has a very convenient behaviour where it converts value of a key into a list even if only one value is present.Now it is very easy to see why
pydantic
ormsgpec
validation would fail!To Reproduce
Expected behavior The request should be validated just fine. But it will throw a validation error.
Proposed FIx Without breaking old behaviour of how sanic parses query params, we can either
urllib.parse.parse_qs
, we can manually loop and convert one value lists back to single value.parse_qs
) that gives the desired dictionary.Fix 1 can use
Request.query_args
which is essentially result ofurllib.parse.parse_qsl
which is also used inurllib.parse.parse_qs
.Fix 2,
parse_qs
only needs a slight modificationAs you can see, Fix 1 & 2 achieve expected behaviour in similar manner.
Environment (please complete the following information):