fix jsonable_encoder exclude include encode model obj invalid#3078
fix jsonable_encoder exclude include encode model obj invalid#3078shan-guo wants to merge 1 commit into
Conversation
|
📝 Docs preview for commit f0adeb7 at: https://6074747ab111dc532ec5a3e7--fastapi.netlify.app |
Codecov Report
@@ Coverage Diff @@
## master #3078 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 243 243
Lines 7419 7427 +8
=========================================
+ Hits 7419 7427 +8
Continue to review full report at Codecov.
|
|
For someone reviewing this PR: this was raised in #2594, and the open PR for the fix is #2606. There is more than just including the "include" and "exclude". As of this fix @shan-guo, did you considered nested objects? With your proposed solution, the include and exclude will be recursively passed to the children and they will also be included and excluded, and I am not sure if that is the expected behaviour. For this reason in #2606, I just included "include" and "exclude" to the last "jsonable_encoder". Pydantic solves this recursivity problem by allowing a nested dictionary for "include" and "exclude", but this is not solved either in this PR and in the #2606. It is a different problem to solve. For the first "include" and "exclude" you passed (BaseModel) is not needed since it is passed to the BaseModel.dict function just above, so it is redundant. For the second one (dict), it is not needed for what I mentioned before, it will include and exclude children, which I don't think it is exptected. For the third one (default) is needed since it will be needed for the dict case. This is why this was included in #2606. Furthermore, the #2606 fixes the problem with include and exclude for dict objects to work like they work in Pydantic BaseModel. You can see the added tests in #2606 for the use cases. Regards, |
|
Sorry i ignore the Pydantic work way ,Thank you answer, I'll close the issue |
Hi, When i use the
jsonable_encoderexpect covert model to dict obj,use theexcludeorincludeoptions was invalidor
The
idalways in the dataSo i created this pr to solve this problem, Maybe there are better ways to solve this problem