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

Add support for JSON Schema with circular references in Python 3.7 #572

Merged

Conversation

Projects
None yet
2 participants
@tiangolo
Copy link
Collaborator

commented Jun 3, 2019

Change Summary

Add JSON Schema generation support for models with circular references, including self-referencing.

Related issue number

This fixes #531, the comment in #524 (comment).

I think parts of it might move in the direction of #478 too.

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>
@codecov

This comment has been minimized.

Copy link

commented Jun 3, 2019

Codecov Report

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

@@          Coverage Diff          @@
##           master   #572   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          15     15           
  Lines        2498   2508   +10     
  Branches      499    501    +2     
=====================================
+ Hits         2498   2508   +10
@tiangolo

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 3, 2019

There's something strange happening to GitHub.

The tests finished a while ago, but they still show up as "running".

The same for Travis and for the tests in Netlify. And the coverage was raise again, removing the partial, but the comment still hasn't been updated.

Show resolved Hide resolved pydantic/schema.py Outdated
Show resolved Hide resolved pydantic/schema.py Outdated
Show resolved Hide resolved pydantic/schema.py Outdated
Show resolved Hide resolved pydantic/schema.py Outdated
Show resolved Hide resolved pydantic/schema.py Outdated
@@ -662,6 +691,7 @@ def field_singleton_schema( # noqa: C901 (ignore complexity)
model_name_map: Dict[Type['BaseModel'], str],
schema_overrides: bool = False,
ref_prefix: Optional[str] = None,
known_models: Set[Type['BaseModel']],

This comment has been minimized.

Copy link
@samuelcolvin

samuelcolvin Jun 4, 2019

Owner

this function is MASSIVE is there anything we can do to simplify it?

This comment has been minimized.

Copy link
@tiangolo

tiangolo Jun 4, 2019

Author Collaborator

Let me check what I can do...

This comment has been minimized.

Copy link
@samuelcolvin

samuelcolvin Jun 4, 2019

Owner

if it's not possible or too difficult, don't worry. Maybe better to do in a separate PR anyway.

This comment has been minimized.

Copy link
@tiangolo

tiangolo Jun 4, 2019

Author Collaborator

Hmm, I agree it's big. But I don't really know what to put outside, I don't see much code repetition as it handles several different cases.

Maybe the last section that checks if it's a model, under if issubclass(field_type, pydantic.BaseModel): could be in a function...

But it would require several parameters, would have to modify some of them (which is a bit difficult to debug/refactor later) and would require an extra if to check if it returned a value (to return it) or not, so I'm not sure.

Do you want me to put it outside? Or later?

This comment has been minimized.

Copy link
@samuelcolvin

samuelcolvin Jun 4, 2019

Owner

let's do it (or not do it) later.

Show resolved Hide resolved tests/test_py37.py Outdated
Show resolved Hide resolved tests/test_py37.py Outdated
Show resolved Hide resolved tests/test_schema.py Outdated

tiangolo and others added some commits Jun 4, 2019

Use str concat expression instead of double expression in schema
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Update pydantic/schema.py
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

@samuelcolvin samuelcolvin assigned samuelcolvin and unassigned tiangolo Jun 4, 2019

@samuelcolvin samuelcolvin merged commit d73aa1b into samuelcolvin:master Jun 4, 2019

5 of 8 checks passed

Header rules No header rules processed
Details
Pages changed 2 new files uploaded
Details
Redirect rules No redirect rules processed
Details
Mixed content No mixed content detected
Details
codecov/project 100% (+0%) compared to 9ffa311
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
deploy/netlify Deploy preview ready!
Details
samuelcolvin.pydantic Build #20190604.19 succeeded
Details
@samuelcolvin

This comment has been minimized.

Copy link
Owner

commented Jun 4, 2019

awesome. Thank you.

@tiangolo

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 4, 2019

Great! Thanks 🎉

@tiangolo tiangolo deleted the tiangolo:json-schema-circular-references branch Jun 4, 2019

@samuelcolvin samuelcolvin referenced this pull request Jun 6, 2019

Merged

StrictBool #580

4 of 4 tasks complete
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.