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.

[BUG] Allow json/BaseModels in query parameters

See original GitHub issue

Is your feature request related to a problem

I have to support getting a structured json string as parameter: Screenshot 2020-01-17 at 22 08 55

https://localhost:8080/test?moop={"name": "something %26 something else","description": "str" ,"price": 0.2,"tax": 0.2}&foo=bar

I would like to have a easy validation for those fields as well, as this could be automatic

The solution you would like

@routes.api_route('/test', methods=['GET', 'POST'], tags=['official', 'message', 'send'])
async def test(
    moop: Json[Item] = Query(..., description='Something something'),
    foo: str = Query(...),
):
    return {'woop': moop, 'lol': foo}
# end def
Initial version:

This was the initial thought, before I discovered the Json type.

@routes.api_route('/test', methods=['GET', 'POST'], tags=['official', 'message', 'send'])
async def test(
    moop: Item = Query(..., description='Something something'),
    foo: str = Query(...),
):
    return {'woop': moop, 'lol': foo}
# end def

Bug you face

AssertionError: Param: moop can only be a request body, using Body(...)

Describe alternatives you’ve considered

    moop: str

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:24 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
w4tsncommented, Apr 26, 2021

I’d like to ask again if this issue can be re-opened. I’m currently implementing a analytics backend which requires some rather complex query parameters in GET requests. I really enjoy writing models and schemas once and use them all out the code to just relax and watch fastapi to build the API documentation for me. It’s a real bummer that dirty hacks and dependency-work arounds (including me writing OpenAPI spec manually) are necessary to get this to work for every new function I implement.

9reactions
imankulovcommented, Jun 8, 2022

I think I found a good enough generic solution for parsing models in query strings.

Usage example:

class User(BaseModel):
    name: str

@app.get("/")
def root(user: User = json_param("user", User, description="User object")):
    return {"message": f"Hello, {user!r}"}

The code of json_param() is available in the gist snippet by the link.

https://gist.github.com/imankulov/cef71dd5a01f9a27caeb66f7bedaf241

Read more comments on GitHub >

github_iconTop Results From Across the Web

Plus sign in query string - Stack Overflow
In the above examples, Google's server script is URL-decoding the query parameters and then using them to do the search. URL-encoding is nothing...
Read more >
Using Query Parameters (AWS Signature Version 4)
Using query parameters to authenticate requests is useful when you want to express a request entirely in a URL. This method is also...
Read more >
Use parameters to ask for input when running a query
To specify the data type for parameters in a query: With the query open in Design view, on the Design tab, in the...
Read more >
Running parameterized queries | BigQuery - Google Cloud
the bq command-line tool's bq query command; the API; the client libraries. The following example shows how to pass parameter values to a...
Read more >
Query Parameters and String Validations - FastAPI
FastAPI allows you to declare additional information and validation for your parameters. Let's take this application as example: Python 3.6 and above Python ......
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