Fix circular schema generation, remove None checking hack #621
The original PR to support circular references (by me) used a "hack" here: https://github.com/samuelcolvin/pydantic/pull/572/files#diff-7d11a8c77c4167b2958bcdb27b18cf2aR754
definitions[model_name] = None
By setting a definition to
But it has a bug, it makes it remove other references to the same sub-model when declared in the same parent model, e.g.
class Dep(BaseModel): number: int class Model(BaseModel): dep: Dep dep_l: List[Dep]
Here, by having more than one field referencing
#613 fixes the original hack, updating it. Instead of marking a definition as declared in the future with
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
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
@@ Coverage Diff @@ ## master #621 +/- ## ===================================== Coverage 100% 100% ===================================== Files 15 15 Lines 2616 2628 +12 Branches 516 516 ===================================== + Hits 2616 2628 +12
wongpat left a comment •
@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
(Also means that the accompanying PR in FastAPI is unnecessary)