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
🐛 Fix jsonable_encoder
using include
and exclude
parameters for non-Pydantic objects
#2606
🐛 Fix jsonable_encoder
using include
and exclude
parameters for non-Pydantic objects
#2606
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2606 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 533 533
Lines 13701 13725 +24
=========================================
+ Hits 13701 13725 +24
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
📝 Docs preview for commit 0e5f1cf at: https://5ff45eba9a7a280778e1db88--fastapi.netlify.app |
I haven't had a chance to check this out thoroughly, but how does this handle nested allowed/exclude keys? My understanding is that include/exclude with pydantic models is not only at the top level but can be specified even in a nested or inherited model. Or, worded another way, if you exclude a key at the top level, will it also exclude keys inside the dict at lower levels of the same name? Is that intended or desired? Because this is a recursive function, it's a bit hard for me to reason about without testing it out. |
Hi @falkben, Thank you for your reply. You are indeed correct it is a recursive function, however, when passing to children, the include and exclude is not passed (this was like this already). Not sure what is desired or not, but I am seeking consistency, so I went ahead and try how it works when passing a BaseModel object: class Model1(BaseModel):
foo: str
bar: str
blabla: str
class Model2(BaseModel):
foo: str
bar: str
model1: Model1
def test_jsonable_encoder_nested():
model1 = Model1(foo="foo1", bar="bar1", blabla="blabla1")
model2 = Model2(foo="foo1", bar="bar1", model1=model1)
jsonable_encoder(model2, include={"foo"})
# returns: {'foo': 'foo1'}
jsonable_encoder(model2, exclude={"foo"})
# {'bar': 'bar1', 'model1': {'bar': 'bar1', 'blabla': 'blabla1', 'foo': 'foo1'}} Then if we try with a dict: def test_jsonable_encoder_nested_dict():
model = {'bar': 'bar1', 'foo': 'foo1', 'model1': {'bar': 'bar1', 'blabla': 'blabla1', 'foo': 'foo1'}}
jsonable_encoder(model, include={"foo"})
# {'foo': 'foo1'}
jsonable_encoder(model, exclude={"foo"})
# {'bar': 'bar1', 'model1': {'bar': 'bar1', 'blabla': 'blabla1', 'foo': 'foo1'}} You can see that it behaves the same way as Pydantic, which is not including and excluding for children objects. I guess this will need to be added to the test suite, so we can define how the function is expected to work for future reference. Do you agree? Regards, |
Sorry I did not answer your question about nested include and exclude. This PR does not add the functionality that Pydantic has with nested includes and excludes as defined in here. In my opinion, this would be part of a new feature if we are interested in adding it, so should be treated in another PR. This PR fixes a bug that the |
Hi there, Just for the awareness, this bug impacts the code from full-stack-fastapi-postgresql, which is a widely used template project for FastAPI. The problem comes when there is a circular dependency in the ORM (e.g. SQLAlchemy), then the obj_data = jsonable_encoder(db_obj, include=set(obj_in.__fields__.keys())) Finally, after this change in my code is when I realized this bug, which made me realized that I am not worried because I checked out the jsonable_encoder function to my private repository, but I am just concerned that someone else might be facing the same problem. Regards, |
0e5f1cf
to
11876ca
Compare
📝 Docs preview for commit 11876ca at: https://625d1e4b94cca815fb062aeb--fastapi.netlify.app |
PR was rebased and fixed to be compatible with include and exclude dictionaries, which was added in this PR and merged: #2016 |
11876ca
to
939a0ef
Compare
📝 Docs preview for commit 939a0ef at: https://62718bd8565c130056849df4--fastapi.netlify.app |
939a0ef
to
9658135
Compare
📝 Docs preview for commit 9658135 at: https://627825599f18150be582ef45--fastapi.netlify.app |
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.
📝 Docs preview for commit 5dac57a at: https://628a83330c8e941472fda48e--fastapi.netlify.app |
5dac57a
to
03c5043
Compare
📝 Docs preview for commit 03c5043 at: https://628a842683a2300f24171db8--fastapi.netlify.app |
📝 Docs preview for commit af87159 at: https://62fea44bffb39a04d745aa03--fastapi.netlify.app |
jsonable_encoder
using include
and exclude
parameters for non-Pydantic objects
… non-Pydantic objects (tiangolo#2606) Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This fixes #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.
When I first added the new tests in
test_jsonable_encoder.py
(without changing the codebase), these were the errors I got:You can see that even I added the same tests to
test_encode_model_with_default
, they all passed. This is because the include and exclude done by Pydantic was done correctly. Therefore, what this PR adds is:include
andexclude
parameters from thejsonable_encoder
function