New Query feature does not behave well with pydantic AliasGenerator #12224
Replies: 2 comments
-
|
I have the same issues described here: #12231 If it is intended, I would close my question and would move my minimal examples in this one? |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Several issues related to aliases was fixed in FastAPI 0.124.4. |
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
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
My request models have an alias generator using the default to_camel alias generator for validation. This allows me to conform to api requirements of camel case while my python models use snake case.
For the above request for example, I would want to be able to call the endpoint like
However I am encountering a number of issues. To begin with, the defaults aren't playing nice.
If I call
I will get an extra_forbidden on field order_by (and if I remove the default I will get an extra_forbidden on field other_tags)
If I add populate_by_name=True to the ConfigDict I can then run the endpoint successfully which seems to imply that fastapi is pushing a dict to the pydantic using the default names.
Worth noting that if the default value is None - I don't have this issue, so again I assume fastapi is stripping null values from the dict.
i.e.
=================================================================
For query requests there are two issues.
If I have added populate_by_name to the ConfigDict and then call
I will now get an extra_forbidden error on order_by with input "created_at" again as it looks like it's passing in the defined query parameter as orderBy correcly BUT ALSO the default value thanks to populate_by_name
Again, if I have no defaults or the defaults are None this works without issue.
The second issue is that lists do not work. If I call
I will get an error that input should be a valid list with input being 1
if I call
I will get an error that input should be a valid list with input being 2 (so appears to only include the last one)
=================================================================
It is also important to note that both these issues only affect snake cased fields.
If you renamed the fields to orderby or othertags, but kept the configdict as is, everything would work as expected.
For this reason I can get around this by naming the fields as their camelCase names, however I have run POST and other requests using the AliasGenerator. and it has worked perfectly, not to mention it also works perfectly for non list fields that have a default value of None (or are required).
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.115.0
Pydantic Version
2.9.2
Python Version
3.11.6
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions