Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

✨ Add support for extensions and updates to the OpenAPI schema in path operations#1922

Merged
tiangolo merged 11 commits intofastapi:masterfrom
edouardlp:fix/support_openapi_extensions
Jul 29, 2021
Merged

✨ Add support for extensions and updates to the OpenAPI schema in path operations#1922
tiangolo merged 11 commits intofastapi:masterfrom
edouardlp:fix/support_openapi_extensions

Conversation

@edouardlp
Copy link
Contributor

@edouardlpedouardlp commentedAug 19, 2020
edited
Loading

Description

This PR adds support for specifying extra info for the JSON schema of path operations. This is so thatOpenAPI 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) forretries andtimeouts, but there are probably many use cases for this.

Implementation

  • There is a lot of boilerplate to add support for extra arguments, but it does not look like there is a way around it. Suggestions welcome.
  • The pydantic model ofOperation 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.

mbelang, moaazsidat, hadrien, and cryptoshell reacted with thumbs up emoji
@codecov
Copy link

codecovbot commentedAug 19, 2020
edited
Loading

Codecov Report

Merging#1922 (0e3d84b) intomaster (7db3591) willnot change coverage.
The diff coverage is100.00%.

Impacted file tree graph

@@            Coverage Diff             @@##            master     #1922    +/-   ##==========================================  Coverage   100.00%   100.00%            ==========================================  Files          401       408     +7       Lines        10095     10196   +101     ==========================================+ Hits         10095     10196   +101
Impacted FilesCoverage Δ
fastapi/applications.py100.00% <ø> (ø)
...th_operation_advanced_configuration/tutorial005.py100.00% <100.00%> (ø)
...th_operation_advanced_configuration/tutorial006.py100.00% <100.00%> (ø)
...th_operation_advanced_configuration/tutorial007.py100.00% <100.00%> (ø)
fastapi/openapi/models.py100.00% <100.00%> (ø)
fastapi/openapi/utils.py100.00% <100.00%> (ø)
fastapi/routing.py100.00% <100.00%> (ø)
tests/test_openapi_route_extensions.py100.00% <100.00%> (ø)
...ration_advanced_configurations/test_tutorial005.py100.00% <100.00%> (ø)
...ration_advanced_configurations/test_tutorial006.py100.00% <100.00%> (ø)
... and8 more

Continue to review full report at Codecov.

Legend -Click here to learn more
Δ = absolute <relative> (impact),ø = not affected,? = missing data
Powered byCodecov. Last update7db3591...0e3d84b. Read thecomment docs.

@github-actions
Copy link
Contributor

📝 Docs preview for commitef11de8 at:https://5f3d3325b71f19b554d54b99--fastapi.netlify.app

@edouardlpedouardlp changed the titlefix(openapi): add support for extensions in path operationsAdd support for extensions in path operationsAug 19, 2020
@edouardlpedouardlp changed the titleAdd support for extensions in path operationsAdd support for OpenAPI extensions in path operationsAug 19, 2020
@edouardlpedouardlp changed the titleAdd support for OpenAPI extensions in path operations✨ Add support for OpenAPI extensions in path operationsAug 19, 2020
@edouardlpedouardlp marked this pull request as ready for reviewAugust 19, 2020 14:31
@edouardlp
Copy link
ContributorAuthor

Note that this is similar to#1917 in thatAPIRoute accepts extra arguments, but I believe the similarities end there. Both PRs appear compatible to me.

@github-actions
Copy link
Contributor

📝 Docs preview for commitb73f7af at:https://5f3d4104691656ae542c442a--fastapi.netlify.app

@Kludex
Copy link
Member

Note that this is similar to#1917 in that APIRoute accepts extra arguments, but I believe the similarities end there. Both PRs appear compatible to me.

Right now, the PR's are not compatible... To make them compatibleextra should be adict, not akeyworded .
But as I said on that PR, I'm not sure if it will be accepted so I don't think you should addapt this PR (or be worried about compatibility) for the sake of that one. We should wait to see what@tiangolo has to say.

ycd, edouardlp, and hadrien reacted with thumbs up emoji

app = FastAPI()


@app.get("/items/", **{"x-my-open-api-extension": "value"})

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:

Suggested change
@app.get("/items/",**{"x-my-open-api-extension":"value"})
@app.get("/items/",openapi_extensions={"x-my-open-api-extension":"value"})

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.

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 😬

Copy link
Member

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? 😮

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.

Copy link
ContributorAuthor

@edouardlpedouardlpAug 21, 2020
edited
Loading

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 mirroringPath,Field andQuery which will add any other arguments to the open api spec. I'll make the change, thanks for the suggestion!

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?

@hadrien
Copy link

Would there be a way to add extension to the router as well?

AlinMH reacted with thumbs up emoji

@mbelang
Copy link

@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

@triptec
Copy link

I tried this branch out and found a missing **route.extra in the call to add_api_route in include_router and also moved

            if route.extra:                operation.update(route.extra)

to get_openapi_path.
I've tried to make PR to edouardlp:fix/support_openapi_extensions, but couldn't for some reason. The changes can be found atFuture-Position-X@4d3f781

Is it perhaps something of interest?

@tiangolotiangolo changed the title✨ Add support for OpenAPI extensions in path operations✨ Add support for extensions and updates to the OpenAPI schema in path operationsJul 29, 2021
@github-actions
Copy link
Contributor

📝 Docs preview for commit0e3d84b at:https://61030891eb9ff5337060b81d--fastapi.netlify.app

@tiangolo
Copy link
Member

Thanks for the work@edouardlp! 🚀 🍰

And thanks for the discussion everyone.

I updated this a bit, renamed the parameter toopenapi_extra, to keep in line withPydantic'sschema_extra while making it explicit that this is about OpenAPI.

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 version0.68.0 in some minutes. 🔖🎉

hadrien reacted with hooray emoji

@tiangolotiangolo merged commit836bb97 intofastapi:masterJul 29, 2021
solomein-sv pushed a commit to solomein-sv/fastapi that referenced this pull requestJul 30, 2021
…h operations (fastapi#1922)Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@KludexKludexKludex left review comments

+2 more reviewers

@hadrienhadrienhadrien left review comments

@triptectriptectriptec left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

6 participants

@edouardlp@Kludex@hadrien@mbelang@triptec@tiangolo

Comments


[8]ページ先頭

©2009-2026 Movatter.jp