[QUESTION] Custom query parameter parsing?
See original GitHub issueDescription
I’m currently migrating an API to FastAPI and I’m trying to prevent too many changes regarding how you call the API. To filter on the old API you’d pass a json serialized complex dict(with nested dicts and lists in it) as a query parameter and this would then be deserialized and parsed to create the filter. I can make this work in FastAPI as well, but this means I have to call json.loads(filter_)
in every route that uses it, and it kind of feels like there is a way to declare a custom type that does this for me.
On a similar note: the old API had comma separated lists. For example: ?columns=id,name,email
. If I define something as a list in FastAPI right now I have to pass it this: ?columns=id&columns=name&columns=email
which seems really verbose for my usecase(I can see it being useful if your data would potentially contain a “,” though), so I’d like to write a custom parser for that as well(although iirc OpenAPI has support for this way of defining lists already somewhere).
Could you help point me in the right direction on how to solve these issues?
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (2 by maintainers)
Top GitHub Comments
I’m not really sure how this is relevant. My question was about how to create a custom query parameters parser, similar to
Query
.Right now it’s possible to achieve with
Depends
and an extra wrapping function like this:But this way I basically have to duplicate parameter name twice: first in the formal parameter of the route function and then as an actual parameter when calling
parse_list
.My question was whether it’s possible to retrieve the name of the formal parameter in the route function, instead of passing it as string manually.
Does it make more sense now?
I love being able to answer my own questions. Apparently I missed the dependencies section of the documentation, which basically does exactly what I want: https://fastapi.tiangolo.com/tutorial/dependencies/first-steps/
Hope that helps anyone coming across this question with the same issue.