[QUESTION] How to catch misspelled/unknown parameters?
See original GitHub issueHow 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:
- Created 3 years ago
- Reactions:4
- Comments:12 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 🤓 🚀
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:
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.