-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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 extensions and updates to the OpenAPI schema in path operations #1922
✨ Add support for extensions and updates to the OpenAPI schema in path operations #1922
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1922 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 401 408 +7
Lines 10095 10196 +101
==========================================
+ Hits 10095 10196 +101
Continue to review full report at Codecov.
|
📝 Docs preview for commit ef11de8 at: https://5f3d3325b71f19b554d54b99--fastapi.netlify.app |
Note that this is similar to #1917 in that |
📝 Docs preview for commit b73f7af at: https://5f3d4104691656ae542c442a--fastapi.netlify.app |
Right now, the PR's are not compatible... To make them compatible |
app = FastAPI() | ||
|
||
|
||
@app.get("/items/", **{"x-my-open-api-extension": "value"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather write something like:
@app.get("/items/", **{"x-my-open-api-extension": "value"}) | |
@app.get("/items/", openapi_extensions={"x-my-open-api-extension": "value"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explicit is better than implicit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Realized @Kludex commented the exact same thing 😬
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't said anything, have I? 😮
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make them compatible extra should be a dict, not a keyworded.
I mis-read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do prefer it explicit as well, but I was mirroring Path
, Field
and Query
which will add any other arguments to the open api spec. I'll make the change, thanks for the suggestion!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hadrien @edouardlp I think it should be openapi_extensions={..}, or if we'd use ** we should update the operation late in get_openapi_path instead of in get_openapi_operation_metadata as that would give us a "openapi override" so we could set requestBody or whatever and not having it overwritten somewhere in get_openapi_path.
What do you guys think?
Would there be a way to add extension to the router as well? |
@tiangolo Any chance this could be looked at please? We think it is a nice enhancement to the openapi support and that would solve a big part of automation for us. Thx |
I tried this branch out and found a missing **route.extra in the call to add_api_route in include_router and also moved
to get_openapi_path. Is it perhaps something of interest? |
to keep it comparable/consistent with Pydantic's schema_extra: https://pydantic-docs.helpmanual.io/usage/schema/#schema-customization Remove wildcard parameters from path operation decorators, to be re-added as a single parameter after merging master
📝 Docs preview for commit 0e3d84b at: https://61030891eb9ff5337060b81d--fastapi.netlify.app |
Thanks for the work @edouardlp! 🚀 🍰 And thanks for the discussion everyone. I updated this a bit, renamed the parameter to I also refactored the implementation to make sure that it does a deep merge of the schema and moved it to do it after the other stuff is added. This way this can be used not only for extensions but also to modify the automatically generated schema. I also added some other examples, docs, and tests. This will be available in FastAPI version |
…h operations (tiangolo#1922) Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
Description
This PR adds support for specifying extra info for the JSON schema of path operations. This is so that OpenAPI extensions can be used when declaring routes.
Use case
We need this because we rely on open api extensions to configure our service mesh (linkerd) for retries and timeouts, but there are probably many use cases for this.
Implementation
Operation
accepts any extra info, which matches the behavior ofPath
,Field
,Query
, etc. There might be an argument to restrict it tox-
prefixed fields, but this will be more complex. I'm not sure it is worth it.