|
| 1 | +from jsonschema.validators import RefResolver |
| 2 | +from openapi_spec_validator import default_handlers |
| 3 | +from openapi_spec_validator import openapi_v3_spec_validator |
| 4 | +from openapi_spec_validator.validators import Dereferencer |
1 | 5 | from pathable.paths import AccessorPath |
2 | 6 |
|
3 | 7 | from openapi_core.spec.accessors import SpecAccessor |
4 | 8 |
|
5 | 9 | SPEC_SEPARATOR = "#" |
6 | 10 |
|
7 | 11 |
|
8 | | -class SpecPath(AccessorPath): |
| 12 | +class Spec(AccessorPath): |
9 | 13 | @classmethod |
10 | | - def from_spec(cls, spec_dict, dereferencer=None, *args, **kwargs): |
11 | | - separator = kwargs.pop("separator", SPEC_SEPARATOR) |
12 | | - accessor = SpecAccessor(spec_dict, dereferencer) |
| 14 | + def from_dict( |
| 15 | + cls, |
| 16 | + data, |
| 17 | + *args, |
| 18 | + url="", |
| 19 | + ref_resolver_handlers=default_handlers, |
| 20 | + separator=SPEC_SEPARATOR, |
| 21 | + ): |
| 22 | + ref_resolver = RefResolver(url, data, handlers=ref_resolver_handlers) |
| 23 | + dereferencer = Dereferencer(ref_resolver) |
| 24 | + accessor = SpecAccessor(data, dereferencer) |
13 | 25 | return cls(accessor, *args, separator=separator) |
| 26 | + |
| 27 | + |
| 28 | +class OpenAPIv30Spec(Spec): |
| 29 | + |
| 30 | + validator = openapi_v3_spec_validator |
| 31 | + |
| 32 | + @classmethod |
| 33 | + def create( |
| 34 | + cls, |
| 35 | + data, |
| 36 | + *args, |
| 37 | + url="", |
| 38 | + ref_resolver_handlers=default_handlers, |
| 39 | + separator=SPEC_SEPARATOR, |
| 40 | + validate=True, |
| 41 | + ): |
| 42 | + if validate: |
| 43 | + cls.validator.validate(data, spec_url=url) |
| 44 | + |
| 45 | + return cls.from_dict( |
| 46 | + data, |
| 47 | + *args, |
| 48 | + url=url, |
| 49 | + ref_resolver_handlers=ref_resolver_handlers, |
| 50 | + separator=separator, |
| 51 | + ) |
0 commit comments