Skip to content

use jsonschema spec and yield from #164

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
Aug 31, 2022
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
47 changes: 15 additions & 32 deletions openapi_spec_validator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# -*- coding: utf-8 -*-
from jsonschema.validators import Draft202012Validator
from jsonschema.validators import Draft4Validator
from jsonschema_spec.handlers import default_handlers

from openapi_schema_validator.validators import OAS30Validator
from openapi_schema_validator.validators import OAS31Validator
from openapi_spec_validator.shortcuts import (
validate_spec_factory, validate_spec_url_factory,
)
from openapi_spec_validator.handlers import UrlHandler, FileObjectHandler
from openapi_spec_validator.schemas import get_openapi_schema
from openapi_spec_validator.factories import \
Draft202012JSONSpecValidatorFactory, Draft4JSONSpecValidatorFactory
from openapi_spec_validator.validators import SpecValidator

__author__ = 'Artur Maciag'
Expand All @@ -31,45 +34,27 @@
'validate_spec_url',
]

file_object_handler = FileObjectHandler()
all_urls_handler = UrlHandler('http', 'https', 'file')
default_handlers = {
'<all_urls>': all_urls_handler,
'http': UrlHandler('http'),
'https': UrlHandler('https'),
'file': UrlHandler('file'),
}

# v2.0 spec
schema_v2, schema_v2_url = get_openapi_schema('2.0')
openapi_v2_validator_factory = Draft4JSONSpecValidatorFactory(
schema_v2, schema_v2_url,
resolver_handlers=default_handlers,
)
schema_v2, _ = get_openapi_schema('2.0')
openapi_v2_schema_validator = Draft4Validator(schema_v2)
openapi_v2_spec_validator = SpecValidator(
openapi_v2_validator_factory,
openapi_v2_schema_validator, OAS30Validator,
resolver_handlers=default_handlers,
)

# v3.0 spec
schema_v30, schema_v30_url = get_openapi_schema('3.0')
openapi_v30_validator_factory = Draft4JSONSpecValidatorFactory(
schema_v30, schema_v30_url,
resolver_handlers=default_handlers,
)
schema_v30, _ = get_openapi_schema('3.0')
openapi_v30_schema_validator = Draft4Validator(schema_v30)
openapi_v30_spec_validator = SpecValidator(
openapi_v30_validator_factory,
openapi_v30_schema_validator, OAS30Validator,
resolver_handlers=default_handlers,
)

# v3.1 spec
schema_v31, schema_v31_url = get_openapi_schema('3.1')
openapi_v31_validator_factory = Draft202012JSONSpecValidatorFactory(
schema_v31, schema_v31_url,
resolver_handlers=default_handlers,
)
schema_v31, _ = get_openapi_schema('3.1')
openapi_v31_schema_validator = Draft202012Validator(schema_v31)
openapi_v31_spec_validator = SpecValidator(
openapi_v31_validator_factory,
openapi_v31_schema_validator, OAS31Validator,
resolver_handlers=default_handlers,
)

Expand All @@ -89,8 +74,6 @@

# aliases to the latest v3 version
schema_v3 = schema_v31
schema_v3_url = schema_v31_url
openapi_v3_validator_factory = openapi_v31_validator_factory
openapi_v3_spec_validator = openapi_v31_spec_validator
validate_v3_spec = validate_v31_spec
validate_v3_spec_url = validate_v31_spec_url
Expand Down
42 changes: 0 additions & 42 deletions openapi_spec_validator/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,9 @@
from functools import wraps
import logging

from openapi_spec_validator.managers import VisitingManager

log = logging.getLogger(__name__)


class DerefValidatorDecorator:
"""Dereferences instance if it is a $ref before passing it for validation.

:param instance_resolver: Resolves refs in the openapi service spec
"""
def __init__(self, instance_resolver):
self.instance_resolver = instance_resolver
self.visiting = VisitingManager()

def __call__(self, func):
def wrapped(validator, schema_element, instance, schema):
if (not isinstance(instance, dict) or '$ref' not in instance
or not instance['$ref'].__hash__):
for res in func(validator, schema_element, instance, schema):
yield res
return

ref = instance['$ref']

# ref already visited
if ref in self.visiting:
return

self._attach_scope(instance)
with self.visiting.visit(ref):
with self.instance_resolver.resolving(ref) as target:
for res in func(validator, schema_element, target, schema):
yield res

return wrapped

def _attach_scope(self, instance):
log.debug('Attaching x-scope to %s', instance)
if 'x-scope' in instance:
log.debug('Ref %s already has scope attached', instance['$ref'])
return

instance['x-scope'] = list(self.instance_resolver._scopes_stack)


class ValidationErrorWrapper(object):

def __init__(self, error_class):
Expand Down
94 changes: 0 additions & 94 deletions openapi_spec_validator/factories.py

This file was deleted.

41 changes: 0 additions & 41 deletions openapi_spec_validator/generators.py

This file was deleted.

11 changes: 0 additions & 11 deletions openapi_spec_validator/handlers/__init__.py

This file was deleted.

8 changes: 0 additions & 8 deletions openapi_spec_validator/handlers/base.py

This file was deleted.

8 changes: 0 additions & 8 deletions openapi_spec_validator/handlers/compat.py

This file was deleted.

33 changes: 0 additions & 33 deletions openapi_spec_validator/handlers/file.py

This file was deleted.

31 changes: 0 additions & 31 deletions openapi_spec_validator/handlers/requests.py

This file was deleted.

24 changes: 0 additions & 24 deletions openapi_spec_validator/handlers/urllib.py

This file was deleted.

Loading