From 7b93dd81d4f2080222a5be1190a864cc2d57a60b Mon Sep 17 00:00:00 2001 From: Tim Wedde Date: Fri, 22 Jul 2022 19:53:48 +0200 Subject: [PATCH 1/2] Support for SwaggerUI As per #300 --- starlite/openapi/controller.py | 55 ++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/starlite/openapi/controller.py b/starlite/openapi/controller.py index c139386a9a..c06903c629 100644 --- a/starlite/openapi/controller.py +++ b/starlite/openapi/controller.py @@ -1,3 +1,4 @@ +import copy from typing import TYPE_CHECKING from orjson import OPT_INDENT_2, dumps @@ -37,6 +38,60 @@ def retrieve_schema_json(self, request: Request) -> "OpenAPI": """Returns the openapi schema""" return self.schema_from_request(request) + @get(path="/swagger", media_type=MediaType.HTML, include_in_schema=False) + def swagger_ui(self, request: Request) -> str: + """Endpoint that serves SwaggerUI""" + schema = self.schema_from_request(request) + if self.dumped_schema == "": + # Note: Fix for Swagger rejection OpenAPI >=3.1 + # We force the version to be lower to get the default JS bundle to accept it + # This works flawlessly as the main blocker for Swagger support for OpenAPI 3.1 is JSON schema support + # Since we use the YAML format this is not an issue for us and we can do this trick to get support right now + # We use deepcopy to avoid changing the actual schema on the request. Since this is a cached call the effect is minimal + schema = copy.deepcopy(schema) + schema.openapi = "3.0.3" + self.dumped_schema = dumps(schema.json(by_alias=True, exclude_none=True), option=OPT_INDENT_2).decode( + "utf-8" + ) + head = f""" + + {schema.info.title} + + + + + + + """ + body = f""" + +
+ + + """ + return f""" + + + {head} + {body} + + """ + @get(media_type=MediaType.HTML, include_in_schema=False) def redoc(self, request: Request) -> str: # pragma: no cover """Endpoint that serves Redoc""" From 860b7afd06293a106f90f47b6fcce20782bf7865 Mon Sep 17 00:00:00 2001 From: Tim Wedde Date: Fri, 22 Jul 2022 20:11:06 +0200 Subject: [PATCH 2/2] Use built-in copy method --- starlite/openapi/controller.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/starlite/openapi/controller.py b/starlite/openapi/controller.py index c06903c629..fbf06dc0b8 100644 --- a/starlite/openapi/controller.py +++ b/starlite/openapi/controller.py @@ -1,4 +1,3 @@ -import copy from typing import TYPE_CHECKING from orjson import OPT_INDENT_2, dumps @@ -48,9 +47,9 @@ def swagger_ui(self, request: Request) -> str: # This works flawlessly as the main blocker for Swagger support for OpenAPI 3.1 is JSON schema support # Since we use the YAML format this is not an issue for us and we can do this trick to get support right now # We use deepcopy to avoid changing the actual schema on the request. Since this is a cached call the effect is minimal - schema = copy.deepcopy(schema) - schema.openapi = "3.0.3" - self.dumped_schema = dumps(schema.json(by_alias=True, exclude_none=True), option=OPT_INDENT_2).decode( + schema_copy = schema.copy() + schema_copy.openapi = "3.0.3" + self.dumped_schema = dumps(schema_copy.json(by_alias=True, exclude_none=True), option=OPT_INDENT_2).decode( "utf-8" ) head = f"""