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] How to catch misspelled/unknown parameters?

See original GitHub issue

How can I catch misspelled or unknown query parameters? For instance:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def root(myparam: int = 0):
    return {'myparam': myparam}
$ curl http://127.0.0.1:8000/?param=5
{"myparam":0}

It seems bad to silently accept the unknown param parameter. The caller wouldn’t be aware that they misspelled the parameter and that it remained at its default value.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

8reactions
tiangolocommented, Apr 13, 2020

Thanks for the discussion here everyone.

FastAPI is based on OpenAPI, which is based on JSON Schema. That by default allows extra values and defines validation for the ones provided. So forbidding extra parameters wouldn’t be acceptable as a default.

There are also many systems that add additional query parameters being used or not, e.g. Google Analytics, Facebook, etc. So I don’t think it would work well for many use cases.


But on the other side, I see this could be useful in some cases (like yours), so I think we could have a path operation decorator parameter allow_extra_parameters=True that conditionally triggers your proposed change.

If you want, you could create a PR with that 🤓 🚀

6reactions
bear24rwcommented, Mar 31, 2020

I agree that there should be a way to disable this check but I would argue that fastapi should be strict about the api spec by default to catch bugs quicker.

In fact, I don’t quite understand what errors can be caused by the fact that you accept exrta parameters in the query string.

The word “extra” is maybe not the best word to use, “unknown” would be better. I’m trying to catch people misspelling the optional query parameters on accident (which actually happened and burned a lot of time). As another example:

@app.get("/members")
def members(exclude_vip: bool = False):
       ....
GET /members?excludevip=1

It might not be obvious from the response that your parameter was ignore (maybe there are thousands of members and only a handful of vip members so either way you get back a huge list)

The intention of my patch is to catch these kind of bugs by default, just like we catch value type errors by default. If someone is sending truly extra params then I would argue that is the non-default exceptional case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

web api handle misspelled fromuri parameters - Stack Overflow
Here's a way using LINQ: Fetch the query parameters as NameValuePairs, selecting just the keys and convert to a List. Get the properties...
Read more >
How to handle URIs containing parameters not used by the API?
The solution would be to check for the presence of level parameter in the URI and return a error message indicating that the...
Read more >
Enter Parameter Value dialog box appears - Office
When you try to run a query, a form, or a report, the Enter Parameter Value dialog box may appear unexpectedly.
Read more >
if / else errors - learn how to fix these - Codecademy
Copy/paste all of your code in with your question, include the exercise ... If you are getting an error about the `else` it...
Read more >
Query Parameters and String Validations - FastAPI
The query parameter q is of type Union[str, None] (or str | None in Python 3.10), ... Query app = FastAPI() @app.get("/items/") async...
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