Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Admit containers of pydantic models as responses models#1017

Merged
tiangolo merged 14 commits into
fastapi:masterfrom
patrickmckenna:update-response-type-processing
Feb 29, 2020
Merged

Admit containers of pydantic models as responses models#1017
tiangolo merged 14 commits into
fastapi:masterfrom
patrickmckenna:update-response-type-processing

Conversation

@patrickmckenna

Copy link
Copy Markdown
Contributor

This PR addresses #628.

Fixing the immediate problem only required removing a brittle runtime type check of each model field in a responses arg (passed to APIRoute.__init__). Putting in a more robust version seemed to lead inexorably to making changes in a few other places.

The same type checking effectively happens in parts of the code that accept a type and returns a pydantic.ModelField. As it turns out, that logic is repeated in a number of places:

Given that, it seemed best to factor out that logic into a standalone function, for the sake of DRYness. That also makes it easier to raise fastapi-specific exceptions, with clearer error messages, when invalid types are passed. And it means that the model args in responses and model_field undergo the same type checking.

@codecov

codecov Bot commented Feb 22, 2020

Copy link
Copy Markdown

Codecov Report

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

Impacted file tree graph

@@          Coverage Diff           @@
##           master   #1017   +/-   ##
======================================
  Coverage     100%    100%           
======================================
  Files         294     297    +3     
  Lines        7732    7813   +81     
======================================
+ Hits         7732    7813   +81
Impacted Files Coverage Δ
fastapi/dependencies/utils.py 100% <100%> (ø) ⬆️
tests/test_response_model_sub_types.py 100% <100%> (ø)
tests/test_response_model_invalid.py 100% <100%> (ø)
fastapi/routing.py 100% <100%> (ø) ⬆️
fastapi/exceptions.py 100% <100%> (ø) ⬆️
fastapi/utils.py 100% <100%> (ø) ⬆️
fastapi/openapi/utils.py 100% <0%> (ø) ⬆️
tests/test_param_in_path_and_dependency.py 100% <0%> (ø)

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 9c3c9b6...c36990e. Read the comment docs.

@patrickmckenna patrickmckenna left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@tiangolo a few questions/requests for feedback.

Comment thread tests/test_apiroute.py Outdated
@@ -0,0 +1,39 @@
from typing import List

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I must confess a little confusion with the test layout. Files seem to be named after possible test scenarios, but there are often multiple files testing the same scenario (e.g. tests/test_additional_response*). This also means the unit tests for a given class are often spread over many files. Given that, I wasn't sure where to add the new tests, so settled for creating new files. I'm happy to rearrange them, please just LMK how.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, the main tests are for the examples in the docs. And then there are extra tests to cover what is not already covered in those. So, it's indeed quite mixed. But it's fine, I just updated them a bit 🙂

Comment thread tests/test_fastapi.py Outdated
Comment on lines +29 to +30
The only stated guarantee w/ additional responses is that they'll be included in the
auto-generated OpenAPI docs -- so test that guarantee.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this is true, based on the docs:

Those additional responses will be included in the OpenAPI schema, so they will also appear in the API docs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yep, that's correct. I just updated it a bit to keep the style of the tests consistent with the tests in other places.

@tiangolo tiangolo merged commit afad59d into fastapi:master Feb 29, 2020
@tiangolo

Copy link
Copy Markdown
Member

This is great! 👏 👏 🎉

Very smart implementation and refactor. 🚀 🤓

I just updated the tests to simplify them a bit and to keep the "style" consistent with the other tests.

Thanks a lot for your contribution! 🍰 👏 🙇‍♂️

@patrickmckenna patrickmckenna deleted the update-response-type-processing branch March 3, 2020 02:11
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