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
Enhancement: Support all kwargs for model_dump
in PydanticPlugin
#3147
Labels
Comments
I think it's reasonable to include not just the ones mentioned, but all the arguments supported by |
guacs
changed the title
Enhancement: Support exclude_none and exlude_defaults in PydanticPlugin
Enhancement: Support all kwags for Mar 4, 2024
model_dump
in PydanticPlugin
JacobCoffee
changed the title
Enhancement: Support all kwags for
Enhancement: Support all kwargs for Mar 4, 2024
model_dump
in PydanticPluginmodel_dump
in PydanticPlugin
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Summary
litestar.contrib.pydantic.PydanticPlugin
has the argumentprefer_alias
which sets theby_alias
argument of the created Pydantic type encoders. The type encoders are based onpydantic.BaseModel.model_dump
which also has the argumentsexclude_none
andexclude_defaults
. This enhancement suggests to add similar arguments toPydanticPlugin
to configure the type encoders.Basic Example
A GET request to
/
would return{"someNumber":42}
because of the plugin configuration. Without it, it would return{"some_number":42,"title":"a number","comment":null}
.Drawbacks and Impact
You can currently achieve the same result without this feature by adding a custom type encoder for
pydantic.BaseModel
. However, it must be combined withPydanticPlugin
for the generated schema to use aliases.Therefore, the only purpose of this enhancement is to make this use case (which I believe to be a common one) simpler to express in code by making the plugin able to do more, reducing the need for type encoders. The community must decide if this is desirable or not.
As long as the default values of the new arguments match the old behavior, this should not cause any issues with backwards compatibility.
Unresolved questions
I can see that there may be concerns about having to support a growing list of
pydantic.BaseModel.model_dump
arguments (for exampleexlude_unset
). I believe that concern to be valid, but you could always draw a line and say "the plugin supports these extremely common arguments, if you want more, use a custom type encoder". Still, the concern is valid and I believe it should be part of the discussion.Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh dashboard
The text was updated successfully, but these errors were encountered: