Skip to content

Commit

Permalink
Merge pull request #120 from starlite-api/memoize-openapi-schema
Browse files Browse the repository at this point in the history
Add OpenAPI Schema Memoization
  • Loading branch information
Goldziher authored May 29, 2022
2 parents 9322c5f + beb7c39 commit 8f96f14
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[flake8]
max-line-length = 120
max-complexity = 12
ignore = E501, C408, B008, B009, W503, SIM119
ignore = E501, C408, B008, B009, W503, SIM119, C417
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ repos:
sqlalchemy,
]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v0.950"
rev: "v0.960"
hooks:
- id: mypy
additional_dependencies:
Expand Down
2 changes: 1 addition & 1 deletion docs/usage/16-caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def my_cached_handler() -> str:
```

By setting `cache=True` in the route handler, caching for the route handler will be enabled for the default duration,
which 60 seconds unless modified.
which is 60 seconds unless modified.

Alternatively you can specify the number of seconds to cache the responses from the given handler like so:

Expand Down
78 changes: 39 additions & 39 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions starlite/openapi/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class OpenAPIController(Controller):
body { margin: 0; padding: 0 }
"""
redoc_version = "next"
dumped_schema = ""

@staticmethod
def schema_from_request(request: Request) -> OpenAPI:
Expand All @@ -35,9 +36,11 @@ def retrieve_schema_json(self, request: Request) -> OpenAPI:
@get(media_type=MediaType.HTML, include_in_schema=False)
def redoc(self, request: Request) -> str: # pragma: no cover
"""Endpoint that serves Redoc"""
dumped_schema = dumps(
request.app.openapi_schema.json(by_alias=True, exclude_none=True), option=OPT_INDENT_2
).decode("utf-8")
if self.dumped_schema == "":
schema = self.schema_from_request(request)
self.dumped_schema = dumps(schema.json(by_alias=True, exclude_none=True), option=OPT_INDENT_2).decode(
"utf-8"
)
head = f"""
<head>
<title>{request.app.openapi_schema.info.title}</title>
Expand All @@ -55,7 +58,7 @@ def redoc(self, request: Request) -> str: # pragma: no cover
<div id='redoc-container'/>
<script type="text/javascript">
Redoc.init(
JSON.parse({dumped_schema}),
JSON.parse({self.dumped_schema}),
undefined,
document.getElementById('redoc-container')
)
Expand Down

0 comments on commit 8f96f14

Please sign in to comment.