Description
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?
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.
Thanks for reporting back and saying how you solved it 馃榾馃殌
trying to solve the same, but the link seems to be broken :)
@inikolaev https://fastapi.tiangolo.com/tutorial/dependencies/ this link should work!
@inikolaev https://fastapi.tiangolo.com/tutorial/dependencies/ this link should work!
Thank you! I have found it myself, but now I have a question: is it possible to write a generic function which will parse whatever query parameter it's applied to?
Something like:
async def parse_list(request: Request):
# extract all query parameters with name specified in `name`
# parse and return array
async def route(param: List[str] = Depends(parse_list)):
pass
Is it possible to somehow get the name of the parameter dependency is being applied to?
@inikolaev You can probably define it in the additional responses: https://fastapi.tiangolo.com/advanced/additional-responses/
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:
async def parse_list(param_name: str):
def parse(request: Request):
return request.query_params[param_name].split(',')
return parse
async def route(param: List[str] = Depends(parse_list('param'))):
^^^^^ ^^^^^
pass
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?
Most helpful comment
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
Dependsand 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?