From accd9cf48bd0f6c113edd98e154b2ece4159af5a Mon Sep 17 00:00:00 2001 From: p1c2u Date: Fri, 12 Feb 2021 23:41:01 +0000 Subject: [PATCH] Spec validation customization --- README.rst | 11 +++++++++++ openapi_core/schema/shortcuts.py | 12 ++++++++++-- openapi_core/schema/specs/factories.py | 7 +------ 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index 80312fa..815036b 100644 --- a/README.rst +++ b/README.rst @@ -156,6 +156,17 @@ Supported security types: Customizations ############## +Spec validation +*************** + +By default, spec dict is validated on spec creation time. Disabling the validation can improve the performance. + +.. code-block:: python + + from openapi_core import create_spec + + spec = create_spec(spec_dict, validate_spec=False) + Deserializers ************* diff --git a/openapi_core/schema/shortcuts.py b/openapi_core/schema/shortcuts.py index 5f5ad01..9fb625d 100644 --- a/openapi_core/schema/shortcuts.py +++ b/openapi_core/schema/shortcuts.py @@ -1,11 +1,19 @@ """OpenAPI core schema shortcuts module""" from jsonschema.validators import RefResolver -from openapi_spec_validator import default_handlers +from openapi_spec_validator import ( + default_handlers, openapi_v3_spec_validator, +) from openapi_core.schema.specs.factories import SpecFactory -def create_spec(spec_dict, spec_url='', handlers=default_handlers): +def create_spec( + spec_dict, spec_url='', handlers=default_handlers, + validate_spec=True, +): + if validate_spec: + openapi_v3_spec_validator.validate(spec_dict, spec_url=spec_url) + spec_resolver = RefResolver( spec_url, spec_dict, handlers=handlers) spec_factory = SpecFactory(spec_resolver) diff --git a/openapi_core/schema/specs/factories.py b/openapi_core/schema/specs/factories.py index 384ef8e..1871c84 100644 --- a/openapi_core/schema/specs/factories.py +++ b/openapi_core/schema/specs/factories.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- """OpenAPI core specs factories module""" -from openapi_spec_validator import openapi_v3_spec_validator from openapi_spec_validator.validators import Dereferencer from openapi_core.compat import lru_cache @@ -19,14 +18,10 @@ from openapi_core.schema.specs.models import Spec class SpecFactory(object): - def __init__(self, spec_resolver, config=None): + def __init__(self, spec_resolver): self.spec_resolver = spec_resolver - self.config = config or {} def create(self, spec_dict, spec_url=''): - if self.config.get('validate_spec', True): - openapi_v3_spec_validator.validate(spec_dict, spec_url=spec_url) - spec_dict_deref = self.dereferencer.dereference(spec_dict) info_spec = spec_dict_deref.get('info', {})