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

Adding support for documenting OpenAPI callbacks through APIRoutes #722

Merged
merged 23 commits into from
Dec 11, 2019

Conversation

booooh
Copy link
Contributor

@booooh booooh commented Nov 20, 2019

Added support for automatic generation and validation (via pydantic) of OpenAPI callback objects.

This is currently done by providing a list of APIRoute objects to a new keyword argument in the path decorators (post, get, put, etc.).

This is convenient, as that list can easily be compiled by creating an APIRouter object, and defining the expected (callback) routes. This has the added benefit that all Request and Response Schemas are added to the documentation, and the router used for defining the callbacks makes a solid base for mocking/testing the callback endpoints that a client would implement.

Please let me know if you have any comments/concerns.
Right now, the tests are failing because the coverage decreased a little. I'll try to see if this can be fixed. I can also squash and rebase my changes if needed.

@codecov
Copy link

codecov bot commented Nov 20, 2019

Codecov Report

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

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #722   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         277    279    +2     
  Lines        7210   7256   +46     
=====================================
+ Hits         7210   7256   +46
Impacted Files Coverage Δ
...est_tutorial/test_extra_models/test_tutorial005.py 100% <ø> (ø) ⬆️
fastapi/applications.py 100% <ø> (ø) ⬆️
tests/test_application.py 100% <ø> (ø) ⬆️
..._tutorial/test_handling_errors/test_tutorial002.py 100% <ø> (ø) ⬆️
tests/test_starlette_exception.py 100% <ø> (ø) ⬆️
...torial/test_body_nested_models/test_tutorial009.py 100% <ø> (ø) ⬆️
tests/test_extra_routes.py 100% <ø> (ø) ⬆️
fastapi/openapi/utils.py 100% <100%> (ø) ⬆️
fastapi/routing.py 100% <100%> (ø) ⬆️
fastapi/utils.py 100% <100%> (ø) ⬆️
... and 4 more

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 b36bfff...cd34bf7. Read the comment docs.

@booooh
Copy link
Contributor Author

booooh commented Nov 20, 2019

Fixes issue #673

@tiangolo
Copy link
Member

Thanks! I'll review it soon.

@booooh
Copy link
Contributor Author

booooh commented Dec 8, 2019

@tiangolo - any update on this? Anything you'd like to discuss?

Copy link
Collaborator

@dmontagu dmontagu left a comment

Choose a reason for hiding this comment

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

We also keep 100% test coverage of the docs .py files, which it looks like you haven’t added. I would recommend importing the app declares in the docs file inside your test file, rather than duplicating the code. You should be able to find examples along these lines in some of the other test files.

docs/tutorial/openapi-callbacks.md Outdated Show resolved Hide resolved
docs/tutorial/openapi-callbacks.md Outdated Show resolved Hide resolved
docs/tutorial/openapi-callbacks.md Outdated Show resolved Hide resolved
docs/tutorial/openapi-callbacks.md Outdated Show resolved Hide resolved
fastapi/__init__.py Outdated Show resolved Hide resolved
fastapi/openapi/utils.py Outdated Show resolved Hide resolved
@booooh
Copy link
Contributor Author

booooh commented Dec 8, 2019

@dmontagu - thanks for taking the time to review the changes! I'll incorporate the changes you requested, and rebase the code against the latest release.

Before moving ahead though, I just want to confirm that you are OK with the approach of defining the callbacks in this manner, and that I'm not abusing any of the existing constructs 😉

@dmontagu
Copy link
Collaborator

dmontagu commented Dec 9, 2019

@booooh

It looks like you are properly following all conventions, as far as I can tell. 👍

I haven't closely read the openapi spec for callbacks yet, and probably would want to before merging this, but as long as the schema you are outputting matches the OpenAPI spec I would be happy to support this feature, and I think the implementation here is good.


At some point I would like to refactor the endpoint creation process so that we don't have to copy the long list of openapi keyword arguments into each of the different route decorator methods; it makes adding features like this somewhat daunting / painful. But approaches to that have been brought up in other issues, and for now I am happy to continue following that convention.

@tiangolo
Copy link
Member

tiangolo commented Dec 9, 2019

Yep, this looks good, thanks for making this! 🚀 🍰

Maybe it feels weird to have a full router to declare endpoints that will never be used, but most of the logic was already there, and it will be the most familiar thing to use for FastAPI users, so I think it's fine, it's the most sensible approach.

For the rest, agreed with @dmontagu (thanks for the review!).

I also want to check some things that could be updated later, but some I don't know if they would work or not, I'll do it afterwards on top of your changes.

@tiangolo
Copy link
Member

tiangolo commented Dec 9, 2019

I'm currently working on this, I'll send my updates on top of your branch once they are done. So you can just wait a bit.

@tiangolo
Copy link
Member

Thanks! 🎉 🚀

I updated it a bit. I also added some extras that improve how this feature works.

Some of the updates:

  • Remove unnecessary code, logs.
  • Update type declarations.
  • Update operationId generation, to ensure all of them are valid Python names.
  • Update tests according to the operationIds that changed.
  • Include a default_response_class parameter when creating an APIRouter, to simplify the creation of these callbacks.
  • Update/increase docs and tutorial example.

@tiangolo tiangolo merged commit 7326097 into fastapi:master Dec 11, 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.

None yet

3 participants