Replies: 2 comments 4 replies
-
Still happens on version 0.109.2, can be replicated with as simple example as @router.get("/test/")
async def test(tags: list[str] = Query(...)):
print(tags) RPC ends up with:
Basically any query list parameter that is not supplied with value (required or not) ends up throwing 500 while serving the request rather than expected 422. @tiangolo Can we make this into an issue? |
Beta Was this translation helpful? Give feedback.
4 replies
-
I can confirm this error is still present |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First Check
Commit to Help
Example Code
Description
There are several rules for arg
q
:q
should be a listq
should be greater than1
q
should contain only unique itemsq
should be positive integersq
is an optional parameter, if it is not provided we setNone
as defaultv2 example has 3 query parameters because I tried to make q parameter as Optional, and when I try it I get wrong validation errors and wrong swagger interface (You can see it on attached screenshots)
How v1 code works:
{"status":"ok","q":null}
{"status":"ok","q":[1,2]}
{"detail":[{"loc":["query","q"],"msg":"ensure this value has at least 2 items","type":"value_error.list.min_items","ctx":{"limit_value":2}}]}
{"detail":[{"loc":["query","q",1],"msg":"ensure this value is greater than 0","type":"value_error.number.not_gt","ctx":{"limit_value":0}}]}
{"detail":[{"loc":["query","q"],"msg":"the list has duplicated items","type":"value_error.list.unique_items"}]}
How v2 code works:
For
q
arg:{"detail":{"msg":"Internal Server Error"}}
ValueError: [TypeError("'pydantic_core._pydantic_core.PydanticUndefinedType' object is not iterable"), TypeError('vars() argument must have __dict__ attribute')]
{"status":"ok","q":[1,2],"q1":null,"q2":null}
{"status":"ok","q":[1],"q1":null,"q2":null}
No check for list length{"detail":[{"type":"greater_than","loc":["query","q",1],"msg":"Input should be greater than 0","input":"0","ctx":{"gt":0},"url":"https://errors.pydantic.dev/2.4/v/greater_than"}]}
{"status":"ok","q":[1],"q1":null,"q2":null}
Instead of validation error we have implicit casting to setFor
q1
arg:{"status":"ok","q":[1],"q1":null,"q2":null}
{"detail":[{"type":"set_type","loc":["query","q1"],"msg":"Input should be a valid set","input":"2","url":"https://errors.pydantic.dev/2.4/v/set_type"}]}
{"detail":[{"type":"set_type","loc":["query","q1"],"msg":"Input should be a valid set","input":"1","url":"https://errors.pydantic.dev/2.4/v/set_type"}]}
{"detail":[{"type":"set_type","loc":["query","q1"],"msg":"Input should be a valid set","input":"0","url":"https://errors.pydantic.dev/2.4/v/set_type"}]}
{"detail":[{"type":"set_type","loc":["query","q1"],"msg":"Input should be a valid set","input":"1","url":"https://errors.pydantic.dev/2.4/v/set_type"}]}
For
q2
arg: Same errors as forq1
argSo, as I see it is impossible to define custom list/set type and use it as annotation for query parameter
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.103.1
Pydantic Version
2.4.0 and 1.10.12
Python Version
3.11.5
Additional Context
Beta Was this translation helpful? Give feedback.
All reactions