Skip to content

Commit 8f8afcf

Browse files
authored
Merge pull request #164 from p1c2u/refactor/use-pathable-and-yield-from
use jsonschema spec and yield from
2 parents dd89ae9 + 7cbc3f6 commit 8f8afcf

File tree

18 files changed

+284
-932
lines changed

18 files changed

+284
-932
lines changed

openapi_spec_validator/__init__.py

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
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+
6+
from openapi_schema_validator.validators import OAS30Validator
7+
from openapi_schema_validator.validators import OAS31Validator
28
from openapi_spec_validator.shortcuts import (
39
validate_spec_factory, validate_spec_url_factory,
410
)
5-
from openapi_spec_validator.handlers import UrlHandler, FileObjectHandler
611
from openapi_spec_validator.schemas import get_openapi_schema
7-
from openapi_spec_validator.factories import \
8-
Draft202012JSONSpecValidatorFactory, Draft4JSONSpecValidatorFactory
912
from openapi_spec_validator.validators import SpecValidator
1013

1114
__author__ = 'Artur Maciag'
@@ -31,45 +34,27 @@
3134
'validate_spec_url',
3235
]
3336

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-
4337
# v2.0 spec
44-
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-
)
38+
schema_v2, _ = get_openapi_schema('2.0')
39+
openapi_v2_schema_validator = Draft4Validator(schema_v2)
4940
openapi_v2_spec_validator = SpecValidator(
50-
openapi_v2_validator_factory,
41+
openapi_v2_schema_validator, OAS30Validator,
5142
resolver_handlers=default_handlers,
5243
)
5344

5445
# v3.0 spec
55-
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-
)
46+
schema_v30, _ = get_openapi_schema('3.0')
47+
openapi_v30_schema_validator = Draft4Validator(schema_v30)
6048
openapi_v30_spec_validator = SpecValidator(
61-
openapi_v30_validator_factory,
49+
openapi_v30_schema_validator, OAS30Validator,
6250
resolver_handlers=default_handlers,
6351
)
6452

6553
# v3.1 spec
66-
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-
)
54+
schema_v31, _ = get_openapi_schema('3.1')
55+
openapi_v31_schema_validator = Draft202012Validator(schema_v31)
7156
openapi_v31_spec_validator = SpecValidator(
72-
openapi_v31_validator_factory,
57+
openapi_v31_schema_validator, OAS31Validator,
7358
resolver_handlers=default_handlers,
7459
)
7560

@@ -89,8 +74,6 @@
8974

9075
# aliases to the latest v3 version
9176
schema_v3 = schema_v31
92-
schema_v3_url = schema_v31_url
93-
openapi_v3_validator_factory = openapi_v31_validator_factory
9477
openapi_v3_spec_validator = openapi_v31_spec_validator
9578
validate_v3_spec = validate_v31_spec
9679
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.

0 commit comments

Comments
 (0)