8000 Fix schema generation for nested/repeated models by dmontagu · Pull Request #333 · fastapi/fastapi · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@dmontagu
Copy link
Collaborator
@dmontagu dmontagu commented Jun 25, 2019

Remove definitions update

Addresses #332

Remove definitions update
m_schema, _ = model_process_schema(
model, model_name_map=model_name_map, ref_prefix=REF_PREFIX
)
definitions.update(m_definitions)
Copy link
Collaborator Author
@dmontagu dmontagu Jun 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This update is not necessary given the function is already iterating over flat_models. Moreover, under the right circumstances, it will overwrite valid definitions with None. I've added a test for this below.

definitions: Dict[str, Dict] = {}
for model in flat_models:
m_schema, m_definitions = model_process_schema(
for model in sorted(flat_models, key=lambda x: x.__name__):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the sorting (by name) here to ensure reproducibility of the test I added. I assume the cost of this sort is negligible for practical purposes (given this is just used to generate the schema).

@codecov
Copy link
codecov bot commented Jun 25, 2019

Codecov Report

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

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #333   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         232    233    +1     
  Lines        5492   5505   +13     
=====================================
+ Hits         5492   5505   +13
Impacted Files Coverage Δ
tests/test_get_openapi.py 100% <100%> (ø)
fastapi/utils.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 76b6fd5...b024342. Read the comment docs.

def f():
pass

openapi = app.openapi()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test will fail at this line if the definitions.update(m_definitions) is not removed above.

@dmontagu dmontagu changed the title Update utils.py Fix schema generation for complex model collections Jun 25, 2019
@dmontagu dmontagu changed the title Fix schema generation for complex model collections Fix schema generation for nested/repeated models Jun 25, 2019
@dmontagu
Copy link
Collaborator Author
dmontagu commented Jun 26, 2019

@tiangolo it looks like this is addressed by pydantic/pydantic#621 -- if that's right I'll close this pull request, but I think it could still make sense to add the schema-generation test I wrote here. (I don't care about the attribution here so feel free to just copy the test into another PR if you want it.)

Note that the schema generation is currently not deterministic due to iteration over the set of models; it might make sense to change this for the sake of testability (given the minimal performance implications).

@tiangolo
Copy link
Member

Thanks @dmontagu ! I'll come back to this once we have a new Pydantic release with pydantic/pydantic#621.

We can leave this open, that will remind me to include your test.

@tiangolo
Copy link
Member

Thanks @dmontagu ! I added your test in #385. So we can close this one. Thanks! 🍰

@tiangolo tiangolo closed this Jul 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0