Skip to content
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

Merged
merged 6 commits into from Jun 28, 2019

Conversation

Projects
None yet
4 participants
@tiangolo
Copy link
Collaborator

commented Jun 26, 2019

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

definitions[model_name] = None

By setting a definition to None it was only marking it to be updated later: https://github.com/samuelcolvin/pydantic/pull/572/files#diff-7d11a8c77c4167b2958bcdb27b18cf2aR228

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 Dep, the definition is created with dep and then it's removed with dep_l (setting it to None).

#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-using definitions 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

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • HISTORY.rst has been updated
    • if this is the first change since a release, please add a new section
    • include the issue number or this pull request number #<number>
    • include your github username @<whomever>

@tiangolo tiangolo referenced this pull request Jun 26, 2019

Closed

schema generation fix #613

4 of 4 tasks complete
@samuelcolvin

This comment has been minimized.

Copy link
Owner

commented Jun 26, 2019

LGTM from a brief look. Please update history.

@wongpat is this okay with you?

@tiangolo tiangolo force-pushed the tiangolo:circular-schema-refactor branch from dee8b21 to 75e04e3 Jun 26, 2019

@codecov

This comment has been minimized.

Copy link

commented Jun 26, 2019

Codecov Report

Merging #621 into master will not change coverage.
The diff coverage is 100%.

@@          Coverage Diff          @@
##           master   #621   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          15     15           
  Lines        2616   2628   +12     
  Branches      516    516           
=====================================
+ Hits         2616   2628   +12
@wongpat
Copy link

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 namedtuple

(Also means that the accompanying PR in FastAPI is unnecessary)

Show resolved Hide resolved pydantic/schema.py Outdated
Show resolved Hide resolved pydantic/schema.py Outdated

tiangolo and others added some commits Jun 27, 2019

Update nested_models in-place
Co-Authored-By: Patrick Wong <wongpat@users.noreply.github.com>
Update nested_models in-place
Co-Authored-By: Patrick Wong <wongpat@users.noreply.github.com>
@tiangolo

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 27, 2019

Thanks for the feedback @wongpat ! I applied your suggestions and replicated them in all the other places.

@samuelcolvin samuelcolvin merged commit 3cdbbae into samuelcolvin:master Jun 28, 2019

6 of 9 checks passed

Header rules No header rules processed
Details
Pages changed All files already uploaded
Details
Redirect rules No redirect rules processed
Details
LGTM analysis: Python No new or fixed alerts
Details
Mixed content No mixed content detected
Details
codecov/project 100% (+0%) compared to ccdf8e1
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
deploy/netlify Deploy preview ready!
Details
samuelcolvin.pydantic Build #20190627.6 succeeded
Details
@samuelcolvin

This comment has been minimized.

Copy link
Owner

commented Jun 28, 2019

great, thank you.

@tiangolo

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 28, 2019

Thanks! 🚀

@tiangolo tiangolo deleted the tiangolo:circular-schema-refactor branch Jun 28, 2019

@wongpat

This comment has been minimized.

Copy link

commented Jun 28, 2019

Thank you both!

@jorgecarleitao

This comment has been minimized.

Copy link

commented Jul 5, 2019

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).

@samuelcolvin

This comment has been minimized.

Copy link
Owner

commented Jul 7, 2019

released, sorry for the delay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.