Skip to content

Commit 9a3b578

Browse files
committed
use jsonschema spec and yield from
1 parent dd89ae9 commit 9a3b578

File tree

18 files changed

+237
-810
lines changed

18 files changed

+237
-810
lines changed

openapi_spec_validator/__init__.py

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# -*- coding: utf-8 -*-
2+
from jsonschema.validators import Draft202012Validator
3+
from jsonschema.validators import Draft4Validator
4+
from jsonschema_spec.handlers import default_handlers
5+
26
from openapi_spec_validator.shortcuts import (
37
validate_spec_factory, validate_spec_url_factory,
48
)
5-
from openapi_spec_validator.handlers import UrlHandler, FileObjectHandler
69
from openapi_spec_validator.schemas import get_openapi_schema
7-
from openapi_spec_validator.factories import \
8-
Draft202012JSONSpecValidatorFactory, Draft4JSONSpecValidatorFactory
910
from openapi_spec_validator.validators import SpecValidator
1011

1112
__author__ = 'Artur Maciag'
@@ -31,45 +32,27 @@
3132
'validate_spec_url',
3233
]
3334

34-
file_object_handler = FileObjectHandler()
35-
all_urls_handler = UrlHandler('http', 'https', 'file')
36-
default_handlers = {
37-
'<all_urls>': all_urls_handler,
38-
'http': UrlHandler('http'),
39-
'https': UrlHandler('https'),
40-
'file': UrlHandler('file'),
41-
}
42-
4335
# v2.0 spec
4436
schema_v2, schema_v2_url = get_openapi_schema('2.0')
45-
openapi_v2_validator_factory = Draft4JSONSpecValidatorFactory(
46-
schema_v2, schema_v2_url,
47-
resolver_handlers=default_handlers,
48-
)
4937
openapi_v2_spec_validator = SpecValidator(
50-
openapi_v2_validator_factory,
38+
Draft4Validator,
39+
schema_v2, schema_v2_url,
5140
resolver_handlers=default_handlers,
5241
)
5342

5443
# v3.0 spec
5544
schema_v30, schema_v30_url = get_openapi_schema('3.0')
56-
openapi_v30_validator_factory = Draft4JSONSpecValidatorFactory(
57-
schema_v30, schema_v30_url,
58-
resolver_handlers=default_handlers,
59-
)
6045
openapi_v30_spec_validator = SpecValidator(
61-
openapi_v30_validator_factory,
46+
Draft4Validator,
47+
schema_v30, schema_v30_url,
6248
resolver_handlers=default_handlers,
6349
)
6450

6551
# v3.1 spec
6652
schema_v31, schema_v31_url = get_openapi_schema('3.1')
67-
openapi_v31_validator_factory = Draft202012JSONSpecValidatorFactory(
68-
schema_v31, schema_v31_url,
69-
resolver_handlers=default_handlers,
70-
)
7153
openapi_v31_spec_validator = SpecValidator(
72-
openapi_v31_validator_factory,
54+
Draft202012Validator,
55+
schema_v31, schema_v31_url,
7356
resolver_handlers=default_handlers,
7457
)
7558

@@ -90,7 +73,6 @@
9073
# aliases to the latest v3 version
9174
schema_v3 = schema_v31
9275
schema_v3_url = schema_v31_url
93-
openapi_v3_validator_factory = openapi_v31_validator_factory
9476
openapi_v3_spec_validator = openapi_v31_spec_validator
9577
validate_v3_spec = validate_v31_spec
9678
validate_v3_spec_url = validate_v31_spec_url

openapi_spec_validator/decorators.py

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,9 @@
22
from functools import wraps
33
import logging
44

5-
from openapi_spec_validator.managers import VisitingManager
6-
75
log = logging.getLogger(__name__)
86

97

10-
class DerefValidatorDecorator:
11-
"""Dereferences instance if it is a $ref before passing it for validation.
12-
13-
:param instance_resolver: Resolves refs in the openapi service spec
14-
"""
15-
def __init__(self, instance_resolver):
16-
self.instance_resolver = instance_resolver
17-
self.visiting = VisitingManager()
18-
19-
def __call__(self, func):
20-
def wrapped(validator, schema_element, instance, schema):
21-
if (not isinstance(instance, dict) or '$ref' not in instance
22-
or not instance['$ref'].__hash__):
23-
for res in func(validator, schema_element, instance, schema):
24-
yield res
25-
return
26-
27-
ref = instance['$ref']
28-
29-
# ref already visited
30-
if ref in self.visiting:
31-
return
32-
33-
self._attach_scope(instance)
34-
with self.visiting.visit(ref):
35-
with self.instance_resolver.resolving(ref) as target:
36-
for res in func(validator, schema_element, target, schema):
37-
yield res
38-
39-
return wrapped
40-
41-
def _attach_scope(self, instance):
42-
log.debug('Attaching x-scope to %s', instance)
43-
if 'x-scope' in instance:
44-
log.debug('Ref %s already has scope attached', instance['$ref'])
45-
return
46-
47-
instance['x-scope'] = list(self.instance_resolver._scopes_stack)
48-
49-
508
class ValidationErrorWrapper(object):
519

5210
def __init__(self, error_class):

openapi_spec_validator/factories.py

Lines changed: 0 additions & 94 deletions
This file was deleted.

openapi_spec_validator/generators.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

openapi_spec_validator/handlers/__init__.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

openapi_spec_validator/handlers/base.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

openapi_spec_validator/handlers/compat.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

openapi_spec_validator/handlers/file.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

openapi_spec_validator/handlers/requests.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

openapi_spec_validator/handlers/urllib.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

openapi_spec_validator/handlers/utils.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)