Spec validation customization

This commit is contained in:
p1c2u 2021-02-12 23:41:01 +00:00
parent 3719092c36
commit accd9cf48b
3 changed files with 22 additions and 8 deletions

View file

@ -156,6 +156,17 @@ Supported security types:
Customizations 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 Deserializers
************* *************

View file

@ -1,11 +1,19 @@
"""OpenAPI core schema shortcuts module""" """OpenAPI core schema shortcuts module"""
from jsonschema.validators import RefResolver 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 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_resolver = RefResolver(
spec_url, spec_dict, handlers=handlers) spec_url, spec_dict, handlers=handlers)
spec_factory = SpecFactory(spec_resolver) spec_factory = SpecFactory(spec_resolver)

View file

@ -1,7 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""OpenAPI core specs factories module""" """OpenAPI core specs factories module"""
from openapi_spec_validator import openapi_v3_spec_validator
from openapi_spec_validator.validators import Dereferencer from openapi_spec_validator.validators import Dereferencer
from openapi_core.compat import lru_cache from openapi_core.compat import lru_cache
@ -19,14 +18,10 @@ from openapi_core.schema.specs.models import Spec
class SpecFactory(object): class SpecFactory(object):
def __init__(self, spec_resolver, config=None): def __init__(self, spec_resolver):
self.spec_resolver = spec_resolver self.spec_resolver = spec_resolver
self.config = config or {}
def create(self, spec_dict, spec_url=''): 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) spec_dict_deref = self.dereferencer.dereference(spec_dict)
info_spec = spec_dict_deref.get('info', {}) info_spec = spec_dict_deref.get('info', {})