question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Custom query parameter parsing?

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

14reactions
inikolaevcommented, Jun 6, 2020

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?

6reactions
teunebooncommented, Jul 26, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to customize express.js url query parameter parsing
My problem is with "25", "0" and "100" - I want them to be numbers. So, I can either change the req.query post...
Read more >
Query Parameters - FastAPI
When you declare other function parameters that are not part of the path parameters, they are automatically interpreted as "query" parameters. from fastapi ......
Read more >
Passing Information via Query Strings - Qualtrics
Data can be passed into a survey using a query string. Once passed, the data can be saved as embedded data with the...
Read more >
Query string - Wikipedia
The query string is composed of a series of arguments by parsing the text into words at the spaces. · The series is...
Read more >
Query String and Query Parameter in Spring MVC
So in the above URL, the query string is whatever follows the question mark sign (“?”) i.e (“number1=23&number2=12”) this part.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found