Skip to content

Spec validation customization #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
*************

Expand Down
12 changes: 10 additions & 2 deletions openapi_core/schema/shortcuts.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
7 changes: 1 addition & 6 deletions openapi_core/schema/specs/factories.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,14 +18,10 @@

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', {})
Expand Down