New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added option to also check for unknown query parameters #1297
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1297 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 240 241 +1
Lines 7101 7147 +46
=========================================
+ Hits 7101 7147 +46 ☔ View full report in Codecov by Sentry. |
Is there any progress on this? I see there are some merge conflicts and missing tests. Having this optional feature would be a nice to have for some work I am doing as well |
No progress from my side, I'm a bit short on time and before investing into testing and docs I thought to wait for a bit of feedback about the implementation. But I guess with more than 60 pending pull requests it is quite difficult to keep up. |
I am interested with the fix, when it will be available. |
+1 |
I'm interested in the fix too. I know it has been considered as an edge case by @tiangolo in a similar issue/PR, but I can explain my niece use case. I'm implementing the "OGC API - Features - Part 1: Core" (http://docs.opengeospatial.org/is/17-069r3/17-069r3.html) which is a standard for fetching geospatial features over HTTP. In section 7.6 we read the following:
This is not a big deal, but just wanted to let you guys know of an use case where this functionality would come in handy. |
* minimal documentation added * tests added * maybe different path to pass that option * maybe rename to something else, maybe could be thought to generally check unknowns, however for models, pydantic has to be adapted
541c0b2
to
8ea5a24
Compare
📝 Docs preview for commit 8ea5a24 at: https://5fa5acf112b34d10164c20b5--fastapi.netlify.app |
📝 Docs preview for commit 49ba6a2 at: https://5fa5b0194381b919d55b3452--fastapi.netlify.app |
📝 Docs preview for commit 5463435 at: https://5fa5b48cef870919146ebb88--fastapi.netlify.app |
so Í took some time to work on this again:
|
📝 Docs preview for commit 2c38b3b at: https://5fa5b7b1402533205fc19f18--fastapi.netlify.app |
tests/test_check_unknown.py
Outdated
app_check = FastAPI(check_unknown=True) | ||
|
||
client_no_check = TestClient(app_no_check) | ||
client_ceck = TestClient(app_check) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big deal, but should probably be client_check
and not client_ceck
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes you are right, changed that :)
📝 Docs preview for commit 3c0a556 at: https://5fa7d2e68d2a1a3e4a180f0e--fastapi.netlify.app |
@mortbauer @boyaps @diogoduartec -- Thanks -- Appreciate all the hard work you're putting into FastAPI! |
@mortbauer @hawkaa @boyaps @diogoduartec As far as I understand the proposed implementation would allow users to specify Another point I would stress is the name of the parameter. I feel the Thanks for your work! What's your opinion on my two cents? |
@gergelyhunyady I agree on the naming part! And if we can align the naming with As for the specific path keyword, I have no strong opinions. |
Really keen to see this feature make it to the main branch! I am afraid I cannot directly contribute to the source code but would certainly like to test this thoroughly. Aligning the param name with pydantic as |
Are there any progress on this? |
if received_body is not None: | ||
left_body_params = set(received_body) | ||
else: | ||
left_body_params = set() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
left_body_params = set(received_body or {})
@@ -641,6 +660,7 @@ def request_params_to_args( | |||
async def request_body_to_args( | |||
required_params: List[ModelField], | |||
received_body: Optional[Union[Dict[str, Any], FormData]], | |||
check_unknown: bool = False, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to name is_unknown
.
if check_unknown: | ||
dependant_names = {param.name for param in dependant.query_params} | ||
left_query_params.difference_update(dependant_names) | ||
if outer_level: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merge this condition to the upper one. Avoid nested conditions if you do not have to.
I am also interested in this feature. However shouldn't the default status code returned be a 404 and not a 400? Query parameters are part of the URL that identify a resource. And in the case of an unexpected query parameter the URL, by definition, would be identifying a resource that does not exist. |
Just noticed the application I'm currently working on has been querying an internal API with an old query parameter name for months, without anyone noticing, resulting in reduced quality of it's output. Will definitely make use of this feature when it is available! |
I would also greatly appreciate this feature |
Would also deeply appreciate this.
they are calling
adding a random string. I'd love to return a 400 when something like this is attempted. |
Would also appreciate this feature as users would know what was wrong if they make a typo or unknown param. |
I also definitely see a use for this. Just wanted to say that a fail on validation is not maybe needed, if there is be a way to show a warning, that would be sufficient imo. |
I'm just here to lend my support for this feature. Misspelling a param bit me in the butt as well. Fortunately, it's not production code or anything. |
For anyone interested in a solution using a subclass of Credit to https://github.com/jenden who provided the first example of a solution for this. |
Currently unknown query parameters are just ignored by fastapi, I feel that it should be at least possible to change the behaviour to return a validation error also for unknown.
It adresses #1190
check unknowns, however for models, pydantic has to be adapted