This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
jsonable_encoder
function not working as expected with different type of variables
#2594
Comments
The pydantic docs for include/exclude are here, for anyone curious regarding this issue. @xaviml How would you expect this to work for objects that are not pydantic models? Seems like we'd have to make a lot of assumptions about the structure of the objects. Perhaps docs could be added saying include/exclude are only valid for pydantic models? Trying to get some more information: If you really want to use include/exclude, is there anything preventing you from using a pydantic model. |
Hi @falkben, Thank you for your reply.
I would expect to be consistent, so to work the same as Pydantic models. For example, if we do: jsonable_encoder({"foo": 1, "bar": 2}, include={"foo"}) I would expect to return jsonable_encoder({"foo": 1, "bar": 2}, exclude={"foo"}) It returns
I don't think is about the assumption of the structure of the object. They are all converted to dictionaries with
You can see in this
I am using the When I created this issue I tried to understand what is the expected use of include and exclude parameters from Regards, |
Possibly related? #2016 |
Hi @falkben, I did see that issue before creating this one. The #2016 PR adds support to dictionaries for the include and exclude, but it does.not change the way include and exclude are being used. Although both of these issues (#2016 and this one) are about jsonable_encoder, they are not related. Regards, |
jsonable_encoder
function not working as expected with different type of variablesjsonable_encoder
function not working as expected with different type of variables
This fixes tiangolo#2594. I added more few cases with include and exclude parameters, which were missing. Now the parameters are consistent with the way that it works with Pydantic.
I created a PR (#2606) fixing the problem mentioned here. You can find in the PR what the problem was and which tests were affected. |
I ran in the same bug tonight. |
Hello, I've added 1 more test case in another issue for proving the change introduced in 0.67 version broke the contract of jsonable_encoder. |
the frick
…On Tue, Jan 18, 2022 at 4:59 AM Stanislav Petrov ***@***.***> wrote:
Hello,
I've added 1 more test case in another issue
<#3636 (comment)>
for proving the change introduced in 0.67 version broke the contract of
*jsonable_encoder*.
—
Reply to this email directly, view it on GitHub
<#2594 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AWZ3M2OOWJ6YJQ5MH2HKJ3LUWU2WVANCNFSM4VR56WOA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Having this issue... |
This fixes tiangolo#2594. I added more few cases with include and exclude parameters, which were missing. Now the parameters are consistent with the way that it works with Pydantic.
This fixes tiangolo#2594. I added more few cases with include and exclude parameters, which were missing. Now the parameters are consistent with the way that it works with Pydantic.
This fixes tiangolo#2594. I added more few cases with include and exclude parameters, which were missing. Now the parameters are consistent with the way that it works with Pydantic.
This fixes tiangolo#2594. I added more few cases with include and exclude parameters, which were missing. Now the parameters are consistent with the way that it works with Pydantic.
jsonable_encoder
function not working as expected with different type of variablesjsonable_encoder
function not working as expected with different type of variables
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First check
Example
Here's a self-contained, minimal, reproducible, example with my use case:
Example 1 (BaseModel)
Example 2 (Python dictionary)
Example 3 (Python object with dataclass)
Description
I was trying to understand how the
include
andexclude
parameters worked. I looked in the documentation (here) and the the test suite (here, latest commit of this file in master branch) and I did not find anything related toinclude
andexclude
in both places.I did see that these 2 parameters came from Pydantic (from the dict function) and it is being used in the _calculate_keys method.
The first thing I did was to check the
jsonable_encoder
using a BaseModel object, which uses the dict function from Pydantic (Example 1), so I created a set of test with a BaseModel instance. All those tests are passing. Then I created the same tests with a Python dictionary (Example 2) and a Python object (Example 3).I expect that the same tests with a different type of variable would work the same. In the case of Example 3, it is failing the most because it is missing the
include
andexclude
parameters at the end of the function (here).Environment
To know the FastAPI version use:
python -c "import fastapi; print(fastapi.__version__)"
To know the Python version use:
Additional context
To reproduce the Example snippets, you will also need
pytest
installed (6.2.1 in my case)I am happy to help by creating a PR for this bug once it is confirmed.
All examples together
The text was updated successfully, but these errors were encountered: