| 1103 | return self.openapi_schema |
| 1104 | |
| 1105 | def setup(self) -> None: |
| 1106 | if self.openapi_url: |
| 1107 | |
| 1108 | async def openapi(req: Request) -> JSONResponse: |
| 1109 | root_path = req.scope.get("root_path", "").rstrip("/") |
| 1110 | schema = self.openapi() |
| 1111 | if root_path and self.root_path_in_servers: |
| 1112 | server_urls = {s.get("url") for s in schema.get("servers", [])} |
| 1113 | if root_path not in server_urls: |
| 1114 | schema = dict(schema) |
| 1115 | schema["servers"] = [{"url": root_path}] + schema.get( |
| 1116 | "servers", [] |
| 1117 | ) |
| 1118 | return JSONResponse(schema) |
| 1119 | |
| 1120 | self.add_route(self.openapi_url, openapi, include_in_schema=False) |
| 1121 | if self.openapi_url and self.docs_url: |
| 1122 | |
| 1123 | async def swagger_ui_html(req: Request) -> HTMLResponse: |
| 1124 | root_path = req.scope.get("root_path", "").rstrip("/") |
| 1125 | openapi_url = root_path + self.openapi_url |
| 1126 | oauth2_redirect_url = self.swagger_ui_oauth2_redirect_url |
| 1127 | if oauth2_redirect_url: |
| 1128 | oauth2_redirect_url = root_path + oauth2_redirect_url |
| 1129 | return get_swagger_ui_html( |
| 1130 | openapi_url=openapi_url, |
| 1131 | title=f"{self.title} - Swagger UI", |
| 1132 | oauth2_redirect_url=oauth2_redirect_url, |
| 1133 | init_oauth=self.swagger_ui_init_oauth, |
| 1134 | swagger_ui_parameters=self.swagger_ui_parameters, |
| 1135 | ) |
| 1136 | |
| 1137 | self.add_route(self.docs_url, swagger_ui_html, include_in_schema=False) |
| 1138 | |
| 1139 | if self.swagger_ui_oauth2_redirect_url: |
| 1140 | |
| 1141 | async def swagger_ui_redirect(req: Request) -> HTMLResponse: |
| 1142 | return get_swagger_ui_oauth2_redirect_html() |
| 1143 | |
| 1144 | self.add_route( |
| 1145 | self.swagger_ui_oauth2_redirect_url, |
| 1146 | swagger_ui_redirect, |
| 1147 | include_in_schema=False, |
| 1148 | ) |
| 1149 | if self.openapi_url and self.redoc_url: |
| 1150 | |
| 1151 | async def redoc_html(req: Request) -> HTMLResponse: |
| 1152 | root_path = req.scope.get("root_path", "").rstrip("/") |
| 1153 | openapi_url = root_path + self.openapi_url |
| 1154 | return get_redoc_html( |
| 1155 | openapi_url=openapi_url, title=f"{self.title} - ReDoc" |
| 1156 | ) |
| 1157 | |
| 1158 | self.add_route(self.redoc_url, redoc_html, include_in_schema=False) |
| 1159 | |
| 1160 | async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: |
| 1161 | if self.root_path: |