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 circular schema generation, remove None checking hack #621
Fix circular schema generation, remove None checking hack #621
Conversation
LGTM from a brief look. Please update history. @wongpat is this okay with you? |
dee8b21
to
75e04e3
Compare
Codecov Report
@@ Coverage Diff @@
## master #621 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 15 15
Lines 2616 2628 +12
Branches 516 516
=====================================
+ Hits 2616 2628 +12 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tiangolo Looks good to me a couple lines that could be a bit optimized. Definitely a better solution than using a magical key in an existing object.
I'd say if we need any more metadata than this as we process the schemas then it could be worth using a namedtuple
(Also means that the accompanying PR in FastAPI is unnecessary)
Co-Authored-By: Patrick Wong <wongpat@users.noreply.github.com>
Co-Authored-By: Patrick Wong <wongpat@users.noreply.github.com>
Thanks for the feedback @wongpat ! I applied your suggestions and replicated them in all the other places. |
great, thank you. |
Thanks! 🚀 |
Thank you both! |
Any chance to release this as a patch? Got hit by this, and seems a bug on the latest version (0.29 as of writing). |
released, sorry for the delay. |
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
Change Summary
This is a continuation/refactor of #613 by @wongpat. It should solve #601
The original PR to support circular references (by me) used a "hack" here: https://github.com/samuelcolvin/pydantic/pull/572/files#diff-7d11a8c77c4167b2958bcdb27b18cf2aR754
By setting a definition to
None
it was only marking it to be updated later: https://github.com/samuelcolvin/pydantic/pull/572/files#diff-7d11a8c77c4167b2958bcdb27b18cf2aR228But it has a bug, it makes it remove other references to the same sub-model when declared in the same parent model, e.g.
Here, by having more than one field referencing
Dep
, the definition is created withdep
and then it's removed withdep_l
(setting it toNone
).#613 fixes the original hack, updating it. Instead of marking a definition as declared in the future with
None
in the definition itself, it creates another key (improved hack)_nested
, then it is removed from the definitions and used.This PR re-implements the same idea from #613 , but being explicit about what's it's doing. It's more verbose (with an extra variable
nested_models
), but presumably less "hacky", by not re-usingdefinitions
for something it is not intended to be used for, as was done in my original implementation and in the fix at #613 .As the changes are considerable, I ended up writing this new PR. But I don't know how to include the commit/attribution to @wongpat.
I think we can start by discussing the implementation. @samuelcolvin what do you think?
Related issue number
#601
Checklist
HISTORY.rst
has been updated#<number>
@<whomever>