Skip to content

Commit accd9cf

Browse files
committed
Spec validation customization
1 parent 3719092 commit accd9cf

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

Diff for: README.rst

+11
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,17 @@ Supported security types:
156156
Customizations
157157
##############
158158

159+
Spec validation
160+
***************
161+
162+
By default, spec dict is validated on spec creation time. Disabling the validation can improve the performance.
163+
164+
.. code-block:: python
165+
166+
from openapi_core import create_spec
167+
168+
spec = create_spec(spec_dict, validate_spec=False)
169+
159170
Deserializers
160171
*************
161172

Diff for: openapi_core/schema/shortcuts.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
"""OpenAPI core schema shortcuts module"""
22
from jsonschema.validators import RefResolver
3-
from openapi_spec_validator import default_handlers
3+
from openapi_spec_validator import (
4+
default_handlers, openapi_v3_spec_validator,
5+
)
46

57
from openapi_core.schema.specs.factories import SpecFactory
68

79

8-
def create_spec(spec_dict, spec_url='', handlers=default_handlers):
10+
def create_spec(
11+
spec_dict, spec_url='', handlers=default_handlers,
12+
validate_spec=True,
13+
):
14+
if validate_spec:
15+
openapi_v3_spec_validator.validate(spec_dict, spec_url=spec_url)
16+
917
spec_resolver = RefResolver(
1018
spec_url, spec_dict, handlers=handlers)
1119
spec_factory = SpecFactory(spec_resolver)

Diff for: openapi_core/schema/specs/factories.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
"""OpenAPI core specs factories module"""
33

4-
from openapi_spec_validator import openapi_v3_spec_validator
54
from openapi_spec_validator.validators import Dereferencer
65

76
from openapi_core.compat import lru_cache
@@ -19,14 +18,10 @@
1918

2019
class SpecFactory(object):
2120

22-
def __init__(self, spec_resolver, config=None):
21+
def __init__(self, spec_resolver):
2322
self.spec_resolver = spec_resolver
24-
self.config = config or {}
2523

2624
def create(self, spec_dict, spec_url=''):
27-
if self.config.get('validate_spec', True):
28-
openapi_v3_spec_validator.validate(spec_dict, spec_url=spec_url)
29-
3025
spec_dict_deref = self.dereferencer.dereference(spec_dict)
3126

3227
info_spec = spec_dict_deref.get('info', {})

0 commit comments

Comments
 (0)