-
Notifications
You must be signed in to change notification settings - Fork 1
[DOP-19924] Add custom openapi middleware #122
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cb8ba37
[DOP-19924] Add custom openapi middleware
aecc4e3
[DOP-19924] Add custom openapi middleware
66ff59c
[DOP-19924] Add custom openapi middleware
79c29d0
[DOP-19924] Add custom openapi middleware
eaaf0c4
[DOP-19924] Add custom openapi middleware
3a9bfc3
[DOP-19924] Add custom openapi middleware
4526be1
[DOP-19924] Add a comment
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| .. _configuration-server-openapi: | ||
|
|
||
| OpenAPI settings | ||
| ================ | ||
|
|
||
| These settings used to control exposing OpenAPI.json and SwaggerUI/ReDoc endpoints. | ||
|
|
||
| .. autopydantic_model:: syncmaster.settings.server.openapi.OpenAPISettings | ||
| .. autopydantic_model:: syncmaster.settings.server.openapi.SwaggerSettings | ||
| .. autopydantic_model:: syncmaster.settings.server.openapi.RedocSettings | ||
| .. autopydantic_model:: syncmaster.settings.server.openapi.LogoSettings | ||
| .. autopydantic_model:: syncmaster.settings.server.openapi.FaviconSettings |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| .. _configuration-server-static-files: | ||
|
|
||
| Serving static files | ||
| ==================== | ||
|
|
||
| These settings used to control serving static files by a server. | ||
|
|
||
| .. autopydantic_model:: syncmaster.settings.server.static_files.StaticFilesSettings |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| # SPDX-FileCopyrightText: 2023-2024 MTS PJSC | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from functools import partial | ||
|
|
||
| from fastapi import FastAPI | ||
| from fastapi.openapi.docs import ( | ||
| get_redoc_html, | ||
| get_swagger_ui_html, | ||
| get_swagger_ui_oauth2_redirect_html, | ||
| ) | ||
| from fastapi.openapi.utils import get_openapi | ||
| from starlette.requests import Request | ||
| from starlette.responses import JSONResponse | ||
|
|
||
| from syncmaster.settings.server.openapi import OpenAPISettings | ||
|
|
||
|
|
||
| async def custom_openapi(request: Request) -> JSONResponse: | ||
| app: FastAPI = request.app | ||
| root_path = request.scope.get("root_path", "").rstrip("/") | ||
| server_urls = set(filter(None, (server_data.get("url") for server_data in app.servers))) | ||
|
|
||
| if root_path not in server_urls: | ||
| if root_path and app.root_path_in_servers: | ||
| app.servers.insert(0, {"url": root_path}) | ||
| server_urls.add(root_path) | ||
|
|
||
| return JSONResponse(app.openapi()) | ||
|
|
||
|
|
||
| def custom_openapi_schema(app: FastAPI, settings: OpenAPISettings) -> dict: | ||
| if app.openapi_schema: | ||
| return app.openapi_schema | ||
|
|
||
| openapi_schema = get_openapi( | ||
| title=app.title, | ||
| version=app.version, | ||
| openapi_version=app.openapi_version, | ||
| summary=app.summary, | ||
| description=app.description, | ||
| terms_of_service=app.terms_of_service, | ||
| contact=app.contact, | ||
| license_info=app.license_info, | ||
| routes=app.routes, | ||
| webhooks=app.webhooks.routes, | ||
| tags=app.openapi_tags, | ||
| servers=app.servers, | ||
| separate_input_output_schemas=app.separate_input_output_schemas, | ||
| ) | ||
| # https://redocly.com/docs/api-reference-docs/specification-extensions/x-logo/ | ||
| openapi_schema["info"]["x-logo"] = { | ||
| "url": str(settings.logo.url), | ||
| "altText": str(settings.logo.alt_text), | ||
| "backgroundColor": f"#{settings.logo.background_color}", # noqa: WPS237 | ||
| "href": str(settings.logo.href), | ||
| } | ||
| app.openapi_schema = openapi_schema | ||
| return app.openapi_schema | ||
|
|
||
|
|
||
| async def custom_swagger_ui_html(request: Request): | ||
| app: FastAPI = request.app | ||
| settings: OpenAPISettings = app.state.settings.server.openapi | ||
| root_path = request.scope.get("root_path", "").rstrip("/") | ||
| openapi_url = root_path + request.app.openapi_url # type: ignore[arg-type] | ||
| return get_swagger_ui_html( | ||
| openapi_url=openapi_url, | ||
| title=f"{app.title} - Swagger UI", | ||
| oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url, | ||
| swagger_js_url=str(settings.swagger.js_url), | ||
| swagger_css_url=str(settings.swagger.css_url), | ||
| swagger_favicon_url=str(settings.favicon.url), | ||
| ) | ||
|
|
||
|
|
||
| async def custom_swagger_ui_redirect(request: Request): | ||
| return get_swagger_ui_oauth2_redirect_html() | ||
|
|
||
|
|
||
| async def custom_redoc_html(request: Request): | ||
| app: FastAPI = request.app | ||
| settings: OpenAPISettings = app.state.settings.server.openapi | ||
| root_path = request.scope.get("root_path", "").rstrip("/") | ||
| openapi_url = root_path + request.app.openapi_url # type: ignore[arg-type] | ||
| return get_redoc_html( | ||
| openapi_url=openapi_url, | ||
| title=f"{app.title} - ReDoc", | ||
| redoc_js_url=settings.redoc.js_url, | ||
| redoc_favicon_url=settings.favicon.url, | ||
| with_google_fonts=False, | ||
| ) | ||
|
|
||
|
|
||
| def apply_openapi_middleware(app: FastAPI, settings: OpenAPISettings) -> FastAPI: | ||
| """Add OpenAPI middleware to the application.""" | ||
| # https://fastapi.tiangolo.com/how-to/custom-docs-ui-assets/#include-the-custom-docs | ||
| if settings.enabled: | ||
| app.openapi_url = "/openapi.json" | ||
| app.add_route(app.openapi_url, custom_openapi, include_in_schema=False) | ||
|
|
||
| if settings.swagger.enabled: | ||
| app.docs_url = "/docs" | ||
| app.swagger_ui_oauth2_redirect_url = "/docs/oauth2-redirect" | ||
| app.add_route(app.docs_url, custom_swagger_ui_html, include_in_schema=False) | ||
| app.add_route(app.swagger_ui_oauth2_redirect_url, custom_swagger_ui_redirect, include_in_schema=False) | ||
|
|
||
| if settings.redoc.enabled: | ||
| app.redoc_url = "/redoc" | ||
| app.add_route(app.redoc_url, custom_redoc_html, include_in_schema=False) | ||
|
|
||
| app.openapi = partial(custom_openapi_schema, app=app, settings=settings) # type: ignore[method-assign] | ||
| return app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # SPDX-FileCopyrightText: 2023-2024 MTS PJSC | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from fastapi import FastAPI | ||
| from fastapi.staticfiles import StaticFiles | ||
|
|
||
| from syncmaster.settings.server.static_files import StaticFilesSettings | ||
|
|
||
|
|
||
| def apply_static_files(app: FastAPI, settings: StaticFilesSettings) -> FastAPI: | ||
| """Add static files serving middleware to the application.""" | ||
| if not settings.enabled: | ||
| return app | ||
|
|
||
| # https://fastapi.tiangolo.com/how-to/custom-docs-ui-assets/#serve-the-static-files | ||
| app.mount("/static", StaticFiles(directory=settings.directory), name="static") | ||
| return app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.