mirror of
https://github.com/correl/openapi-core.git
synced 2024-12-29 11:09:25 +00:00
Spec validation customization
This commit is contained in:
parent
3719092c36
commit
accd9cf48b
3 changed files with 22 additions and 8 deletions
11
README.rst
11
README.rst
|
@ -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
|
||||||
*************
|
*************
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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', {})
|
||||||
|
|
Loading…
Reference in a new issue