Admit containers of pydantic models as responses models#1017
Conversation
This should allow for smarter error handling, and clearer testing.
This should also enable DRYing up APIRoute.__init__
Enable this fcn to be used in fastapi.dependencies.utils, too.
The type check effectively happens now in the call to create_response_field.
Codecov Report
@@ Coverage Diff @@
## master #1017 +/- ##
======================================
Coverage 100% 100%
======================================
Files 294 297 +3
Lines 7732 7813 +81
======================================
+ Hits 7732 7813 +81
Continue to review full report at Codecov.
|
patrickmckenna
left a comment
There was a problem hiding this comment.
@tiangolo a few questions/requests for feedback.
| @@ -0,0 +1,39 @@ | |||
| from typing import List | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 🙂
| The only stated guarantee w/ additional responses is that they'll be included in the | ||
| auto-generated OpenAPI docs -- so test that guarantee. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Yep, that's correct. I just updated it a bit to keep the style of the tests consistent with the tests in other places.
|
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! 🍰 👏 🙇♂️ |
This PR addresses #628.
Fixing the immediate problem only required removing a brittle runtime type check of each
modelfield in aresponsesarg (passed toAPIRoute.__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
typeand returns apydantic.ModelField. As it turns out, that logic is repeated in a number of places:modelfields in theresponsesarg toAPIRoute.__init__response_modelarg toAPIRoute.__init__fastapi.dependencies.utils: here, here, hereGiven 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 themodelargs inresponsesandmodel_fieldundergo the same type checking.