From 69e891a2e591ed2f6b25e44a382b5d4dd0ab033b Mon Sep 17 00:00:00 2001 From: Sergey Sayamov Date: Sun, 11 Feb 2024 17:41:33 +0300 Subject: [PATCH] Allow skipping apispec validation check on app startup --- docs/contrib/apispec.md | 3 +++ starlette_web/common/conf/global_settings.py | 4 ++++ starlette_web/contrib/apispec/apps.py | 6 ++++++ 3 files changed, 13 insertions(+) diff --git a/docs/contrib/apispec.md b/docs/contrib/apispec.md index 2bd2f20..5de6f5b 100644 --- a/docs/contrib/apispec.md +++ b/docs/contrib/apispec.md @@ -70,6 +70,9 @@ inner registry by class name. If any 2 classes have same name, APIspec struggles schema class for introspection and gives up. This results in invalid OpenAPI, where schema is mentioned, but its definition is missing. +**Note**: Validation is performed on each `app` startup, which includes uvicorn worker restarts. +To disable validation in production, set `settings.APISPEC_PERFORM_CHECKS_ON_STARTUP` to `False`. + ### Camel case support `contrib.camel_case` provides helper methods and classes to convert `snake_case` to `camelCase` and vice versa. diff --git a/starlette_web/common/conf/global_settings.py b/starlette_web/common/conf/global_settings.py index 03bf1b7..639ed11 100644 --- a/starlette_web/common/conf/global_settings.py +++ b/starlette_web/common/conf/global_settings.py @@ -58,6 +58,10 @@ EMAIL_SENDER = None +# Contrib.apispec + +APISPEC_PERFORM_CHECKS_ON_STARTUP = True + # Contrib.auth AUTH_PASSWORD_HASHERS = [ diff --git a/starlette_web/contrib/apispec/apps.py b/starlette_web/contrib/apispec/apps.py index 86f7696..8178997 100644 --- a/starlette_web/contrib/apispec/apps.py +++ b/starlette_web/contrib/apispec/apps.py @@ -12,6 +12,9 @@ class AppConfig(BaseAppConfig): app_name = "apispec" def initialize(self): + if not settings.APISPEC_PERFORM_CHECKS_ON_STARTUP: + return + try: __import__("openapi_spec_validator") except (SystemError, ImportError): @@ -24,6 +27,9 @@ def initialize(self): ) def perform_checks(self): + if not settings.APISPEC_PERFORM_CHECKS_ON_STARTUP: + return + from openapi_spec_validator import validate from openapi_spec_validator.validation.exceptions import ( OpenAPIValidationError,