diff --git a/README.rst b/README.rst index 37022ed3..3ea9886c 100644 --- a/README.rst +++ b/README.rst @@ -62,12 +62,12 @@ Firstly create your specification object: .. code-block:: python from json import load - from openapi_core import create_spec + from openapi_core import OpenAPISpec as Spec with open('openapi.json', 'r') as spec_file: spec_dict = load(spec_file) - spec = create_spec(spec_dict) + spec = Spec.create(spec_dict) Request ******* diff --git a/docs/customizations.rst b/docs/customizations.rst index dcbf7994..5b3c63fb 100644 --- a/docs/customizations.rst +++ b/docs/customizations.rst @@ -8,9 +8,9 @@ By default, spec dict is validated on spec creation time. Disabling the validati .. code-block:: python - from openapi_core import create_spec + from openapi_core import OpenAPISpec as Spec - spec = create_spec(spec_dict, validate_spec=False) + spec = Spec.create(spec_dict, validate=False) Deserializers ------------- diff --git a/docs/integrations.rst b/docs/integrations.rst index c96d8919..b6cf22bc 100644 --- a/docs/integrations.rst +++ b/docs/integrations.rst @@ -21,14 +21,14 @@ Django can be integrated by middleware. Add `DjangoOpenAPIMiddleware` to your `M .. code-block:: python # settings.py - from openapi_core import create_spec + from openapi_core import OpenAPISpec as Spec MIDDLEWARE = [ # ... 'openapi_core.contrib.django.middlewares.DjangoOpenAPIMiddleware', ] - OPENAPI_SPEC = create_spec(spec_dict) + OPENAPI_SPEC = Spec.create(spec_dict) After that you have access to validation result object with all validated request data from Django view through request object. diff --git a/docs/usage.rst b/docs/usage.rst index 3a9a7a3d..c6492117 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -6,12 +6,12 @@ Firstly create your specification: object .. code-block:: python from json import load - from openapi_core import create_spec + from openapi_core import OpenAPISpec as Spec with open('openapi.json', 'r') as spec_file: spec_dict = load(spec_file) - spec = create_spec(spec_dict) + spec = Spec.create(spec_dict) Request diff --git a/openapi_core/__init__.py b/openapi_core/__init__.py index 8c806231..c649c154 100644 --- a/openapi_core/__init__.py +++ b/openapi_core/__init__.py @@ -1,5 +1,4 @@ """OpenAPI core module""" -from openapi_core.shortcuts import create_spec from openapi_core.shortcuts import spec_validate_body from openapi_core.shortcuts import spec_validate_data from openapi_core.shortcuts import spec_validate_headers @@ -7,6 +6,7 @@ from openapi_core.shortcuts import spec_validate_security from openapi_core.shortcuts import validate_request from openapi_core.shortcuts import validate_response +from openapi_core.spec import OpenAPIv30Spec from openapi_core.validation.request.validators import RequestBodyValidator from openapi_core.validation.request.validators import ( RequestParametersValidator, @@ -26,7 +26,9 @@ __license__ = "BSD 3-Clause License" __all__ = [ - "create_spec", + "OpenAPIv30Spec", + "OpenAPIv3Spec", + "OpenAPISpec", "validate_request", "validate_response", "spec_validate_body", @@ -42,3 +44,9 @@ "ResponseDataValidator", "ResponseHeadersValidator", ] + +# aliases to the latest v3 version +OpenAPIv3Spec = OpenAPIv30Spec + +# aliases to the latest version +OpenAPISpec = OpenAPIv3Spec diff --git a/openapi_core/shortcuts.py b/openapi_core/shortcuts.py index 59c27269..03f3cb96 100644 --- a/openapi_core/shortcuts.py +++ b/openapi_core/shortcuts.py @@ -1,6 +1,5 @@ """OpenAPI core shortcuts module""" # backward compatibility -from openapi_core.spec.shortcuts import create_spec from openapi_core.validation.request.shortcuts import spec_validate_body from openapi_core.validation.request.shortcuts import spec_validate_parameters from openapi_core.validation.request.shortcuts import spec_validate_security @@ -10,7 +9,6 @@ from openapi_core.validation.response.shortcuts import validate_response __all__ = [ - "create_spec", "validate_request", "validate_response", "spec_validate_body", diff --git a/openapi_core/spec/__init__.py b/openapi_core/spec/__init__.py index e69de29b..70f65730 100644 --- a/openapi_core/spec/__init__.py +++ b/openapi_core/spec/__init__.py @@ -0,0 +1,5 @@ +from openapi_core.spec.paths import OpenAPIv30Spec + +__all__ = [ + "OpenAPIv30Spec", +] diff --git a/openapi_core/spec/paths.py b/openapi_core/spec/paths.py index c2f68506..9388b264 100644 --- a/openapi_core/spec/paths.py +++ b/openapi_core/spec/paths.py @@ -1,3 +1,7 @@ +from jsonschema.validators import RefResolver +from openapi_spec_validator import default_handlers +from openapi_spec_validator import openapi_v3_spec_validator +from openapi_spec_validator.validators import Dereferencer from pathable.paths import AccessorPath from openapi_core.spec.accessors import SpecAccessor @@ -5,9 +9,43 @@ SPEC_SEPARATOR = "#" -class SpecPath(AccessorPath): +class Spec(AccessorPath): @classmethod - def from_spec(cls, spec_dict, dereferencer=None, *args, **kwargs): - separator = kwargs.pop("separator", SPEC_SEPARATOR) - accessor = SpecAccessor(spec_dict, dereferencer) + def from_dict( + cls, + data, + *args, + url="", + ref_resolver_handlers=default_handlers, + separator=SPEC_SEPARATOR, + ): + ref_resolver = RefResolver(url, data, handlers=ref_resolver_handlers) + dereferencer = Dereferencer(ref_resolver) + accessor = SpecAccessor(data, dereferencer) return cls(accessor, *args, separator=separator) + + +class OpenAPIv30Spec(Spec): + + validator = openapi_v3_spec_validator + + @classmethod + def create( + cls, + data, + *args, + url="", + ref_resolver_handlers=default_handlers, + separator=SPEC_SEPARATOR, + validate=True, + ): + if validate: + cls.validator.validate(data, spec_url=url) + + return cls.from_dict( + data, + *args, + url=url, + ref_resolver_handlers=ref_resolver_handlers, + separator=separator, + ) diff --git a/openapi_core/templating/paths/util.py b/openapi_core/templating/paths/util.py index 2ba3816e..a37cff3e 100644 --- a/openapi_core/templating/paths/util.py +++ b/openapi_core/templating/paths/util.py @@ -1,8 +1,8 @@ from typing import Tuple -from openapi_core.spec.paths import SpecPath +from openapi_core.spec.paths import OpenAPIv30Spec as Spec from openapi_core.templating.datatypes import TemplateResult -def template_path_len(template_path: Tuple[SpecPath, TemplateResult]) -> int: +def template_path_len(template_path: Tuple[Spec, TemplateResult]) -> int: return len(template_path[1].variables) diff --git a/tests/integration/contrib/django/data/v3.0/djangoproject/settings.py b/tests/integration/contrib/django/data/v3.0/djangoproject/settings.py index e02e990b..5c4a1d83 100644 --- a/tests/integration/contrib/django/data/v3.0/djangoproject/settings.py +++ b/tests/integration/contrib/django/data/v3.0/djangoproject/settings.py @@ -15,7 +15,7 @@ import yaml -from openapi_core import create_spec +from openapi_core import OpenAPISpec as Spec # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -123,4 +123,4 @@ OPENAPI_SPEC_DICT = yaml.load(OPENAPI_SPEC_PATH.read_text(), yaml.Loader) -OPENAPI_SPEC = create_spec(OPENAPI_SPEC_DICT) +OPENAPI_SPEC = Spec.create(OPENAPI_SPEC_DICT) diff --git a/tests/integration/contrib/falcon/data/v3.0/falconproject/openapi.py b/tests/integration/contrib/falcon/data/v3.0/falconproject/openapi.py index b199ece4..eb476806 100644 --- a/tests/integration/contrib/falcon/data/v3.0/falconproject/openapi.py +++ b/tests/integration/contrib/falcon/data/v3.0/falconproject/openapi.py @@ -2,10 +2,10 @@ import yaml -from openapi_core import create_spec +from openapi_core import OpenAPISpec as Spec from openapi_core.contrib.falcon.middlewares import FalconOpenAPIMiddleware openapi_spec_path = Path("tests/integration/data/v3.0/petstore.yaml") spec_dict = yaml.load(openapi_spec_path.read_text(), yaml.Loader) -spec = create_spec(spec_dict) +spec = Spec.create(spec_dict) openapi_middleware = FalconOpenAPIMiddleware.from_spec(spec) diff --git a/tests/integration/contrib/flask/test_flask_decorator.py b/tests/integration/contrib/flask/test_flask_decorator.py index 518a39e5..7e8c0c90 100644 --- a/tests/integration/contrib/flask/test_flask_decorator.py +++ b/tests/integration/contrib/flask/test_flask_decorator.py @@ -4,7 +4,7 @@ from flask import make_response from openapi_core.contrib.flask.decorators import FlaskOpenAPIViewDecorator -from openapi_core.shortcuts import create_spec +from openapi_core.spec import OpenAPIv30Spec as Spec from openapi_core.validation.request.datatypes import Parameters @@ -15,7 +15,7 @@ class TestFlaskOpenAPIDecorator: @pytest.fixture def spec(self, factory): specfile = "contrib/flask/data/v3.0/flask_factory.yaml" - return create_spec(factory.spec_from_file(specfile)) + return Spec.create(factory.spec_from_file(specfile)) @pytest.fixture def decorator(self, spec): diff --git a/tests/integration/contrib/flask/test_flask_validation.py b/tests/integration/contrib/flask/test_flask_validation.py index e4d0ccb2..b824ebc9 100644 --- a/tests/integration/contrib/flask/test_flask_validation.py +++ b/tests/integration/contrib/flask/test_flask_validation.py @@ -2,7 +2,7 @@ from openapi_core.contrib.flask import FlaskOpenAPIRequest from openapi_core.contrib.flask import FlaskOpenAPIResponse -from openapi_core.shortcuts import create_spec +from openapi_core.spec import OpenAPIv30Spec as Spec from openapi_core.validation.request.validators import RequestValidator from openapi_core.validation.response.validators import ResponseValidator @@ -11,7 +11,7 @@ class TestFlaskOpenAPIValidation: @pytest.fixture def flask_spec(self, factory): specfile = "contrib/flask/data/v3.0/flask_factory.yaml" - return create_spec(factory.spec_from_file(specfile)) + return Spec.create(factory.spec_from_file(specfile)) def test_response_validator_path_pattern( self, flask_spec, request_factory, response_factory diff --git a/tests/integration/contrib/flask/test_flask_views.py b/tests/integration/contrib/flask/test_flask_views.py index d61dd7dc..34551bb6 100644 --- a/tests/integration/contrib/flask/test_flask_views.py +++ b/tests/integration/contrib/flask/test_flask_views.py @@ -4,7 +4,7 @@ from flask import make_response from openapi_core.contrib.flask.views import FlaskOpenAPIView -from openapi_core.shortcuts import create_spec +from openapi_core.spec import OpenAPIv30Spec as Spec class TestFlaskOpenAPIView: @@ -14,7 +14,7 @@ class TestFlaskOpenAPIView: @pytest.fixture def spec(self, factory): specfile = "contrib/flask/data/v3.0/flask_factory.yaml" - return create_spec(factory.spec_from_file(specfile)) + return Spec.create(factory.spec_from_file(specfile)) @pytest.fixture def app(self): diff --git a/tests/integration/contrib/requests/test_requests_validation.py b/tests/integration/contrib/requests/test_requests_validation.py index 42734b36..7c053576 100644 --- a/tests/integration/contrib/requests/test_requests_validation.py +++ b/tests/integration/contrib/requests/test_requests_validation.py @@ -4,7 +4,7 @@ from openapi_core.contrib.requests import RequestsOpenAPIRequest from openapi_core.contrib.requests import RequestsOpenAPIResponse -from openapi_core.shortcuts import create_spec +from openapi_core.spec import OpenAPIv30Spec as Spec from openapi_core.validation.request.validators import RequestValidator from openapi_core.validation.response.validators import ResponseValidator @@ -13,7 +13,7 @@ class TestRequestsOpenAPIValidation: @pytest.fixture def spec(self, factory): specfile = "contrib/requests/data/v3.0/requests_factory.yaml" - return create_spec(factory.spec_from_file(specfile)) + return Spec.create(factory.spec_from_file(specfile)) @responses.activate def test_response_validator_path_pattern(self, spec): diff --git a/tests/integration/schema/test_empty.py b/tests/integration/schema/test_empty.py index 98b12c72..5ff3eeb8 100644 --- a/tests/integration/schema/test_empty.py +++ b/tests/integration/schema/test_empty.py @@ -1,7 +1,7 @@ import pytest from jsonschema.exceptions import ValidationError -from openapi_core.shortcuts import create_spec +from openapi_core.spec import OpenAPIv30Spec as Spec class TestEmpty: @@ -11,8 +11,8 @@ def spec_dict(self, factory): @pytest.fixture def spec(self, spec_dict): - return create_spec(spec_dict) + return Spec.create(spec_dict) def test_raises_on_invalid(self, spec_dict): with pytest.raises(ValidationError): - create_spec(spec_dict) + Spec.create(spec_dict) diff --git a/tests/integration/schema/test_link_spec.py b/tests/integration/schema/test_link_spec.py index 2ed33fa3..3f3ed28e 100644 --- a/tests/integration/schema/test_link_spec.py +++ b/tests/integration/schema/test_link_spec.py @@ -1,10 +1,10 @@ -from openapi_core.shortcuts import create_spec +from openapi_core.spec import OpenAPIv30Spec as Spec class TestLinkSpec: def test_no_param(self, factory): spec_dict = factory.spec_from_file("data/v3.0/links.yaml") - spec = create_spec(spec_dict) + spec = Spec.create(spec_dict) resp = spec / "paths#/status#get#responses#default" links = resp / "links" @@ -18,7 +18,7 @@ def test_no_param(self, factory): def test_param(self, factory): spec_dict = factory.spec_from_file("data/v3.0/links.yaml") - spec = create_spec(spec_dict) + spec = Spec.create(spec_dict) resp = spec / "paths#/status/{resourceId}#get#responses#default" links = resp / "links" diff --git a/tests/integration/schema/test_path_params.py b/tests/integration/schema/test_path_params.py index 028bc674..ccb8496c 100644 --- a/tests/integration/schema/test_path_params.py +++ b/tests/integration/schema/test_path_params.py @@ -1,6 +1,6 @@ import pytest -from openapi_core.shortcuts import create_spec +from openapi_core.spec import OpenAPIv30Spec as Spec class TestMinimal: @@ -10,7 +10,7 @@ class TestMinimal: @pytest.mark.parametrize("spec_path", spec_paths) def test_param_present(self, factory, spec_path): spec_dict = factory.spec_from_file(spec_path) - spec = create_spec(spec_dict) + spec = Spec.create(spec_dict) path = spec / "paths#/resource/{resId}" diff --git a/tests/integration/schema/test_spec.py b/tests/integration/schema/test_spec.py index c0b2092c..4fba32bc 100644 --- a/tests/integration/schema/test_spec.py +++ b/tests/integration/schema/test_spec.py @@ -4,7 +4,7 @@ from openapi_core.schema.servers import get_server_url from openapi_core.schema.specs import get_spec_url -from openapi_core.shortcuts import create_spec +from openapi_core.spec import OpenAPIv30Spec as Spec from openapi_core.validation.request.validators import RequestValidator from openapi_core.validation.response.validators import ResponseValidator @@ -29,7 +29,7 @@ def spec_dict(self, factory): @pytest.fixture def spec(self, spec_dict, spec_uri): - return create_spec(spec_dict, spec_uri) + return Spec.create(spec_dict, url=spec_uri) @pytest.fixture def request_validator(self, spec): diff --git a/tests/integration/validation/test_minimal.py b/tests/integration/validation/test_minimal.py index 076a7de7..a974d813 100644 --- a/tests/integration/validation/test_minimal.py +++ b/tests/integration/validation/test_minimal.py @@ -1,6 +1,6 @@ import pytest -from openapi_core.shortcuts import create_spec +from openapi_core.spec import OpenAPIv30Spec as Spec from openapi_core.templating.paths.exceptions import OperationNotFound from openapi_core.templating.paths.exceptions import PathNotFound from openapi_core.testing import MockRequest @@ -27,7 +27,7 @@ class TestMinimal: @pytest.mark.parametrize("spec_path", spec_paths) def test_hosts(self, factory, server, spec_path): spec_dict = factory.spec_from_file(spec_path) - spec = create_spec(spec_dict) + spec = Spec.create(spec_dict) validator = RequestValidator(spec) request = MockRequest(server, "get", "/status") @@ -39,7 +39,7 @@ def test_hosts(self, factory, server, spec_path): @pytest.mark.parametrize("spec_path", spec_paths) def test_invalid_operation(self, factory, server, spec_path): spec_dict = factory.spec_from_file(spec_path) - spec = create_spec(spec_dict) + spec = Spec.create(spec_dict) validator = RequestValidator(spec) request = MockRequest(server, "post", "/status") @@ -54,7 +54,7 @@ def test_invalid_operation(self, factory, server, spec_path): @pytest.mark.parametrize("spec_path", spec_paths) def test_invalid_path(self, factory, server, spec_path): spec_dict = factory.spec_from_file(spec_path) - spec = create_spec(spec_dict) + spec = Spec.create(spec_dict) validator = RequestValidator(spec) request = MockRequest(server, "get", "/nonexistent") diff --git a/tests/integration/validation/test_petstore.py b/tests/integration/validation/test_petstore.py index 8a92aa6f..5d1d6801 100644 --- a/tests/integration/validation/test_petstore.py +++ b/tests/integration/validation/test_petstore.py @@ -14,12 +14,12 @@ from openapi_core.exceptions import MissingRequiredHeader from openapi_core.exceptions import MissingRequiredParameter from openapi_core.extensions.models.models import BaseModel -from openapi_core.shortcuts import create_spec from openapi_core.shortcuts import spec_validate_body from openapi_core.shortcuts import spec_validate_data from openapi_core.shortcuts import spec_validate_headers from openapi_core.shortcuts import spec_validate_parameters from openapi_core.shortcuts import spec_validate_security +from openapi_core.spec import OpenAPIv30Spec as Spec from openapi_core.templating.media_types.exceptions import MediaTypeNotFound from openapi_core.templating.paths.exceptions import ServerNotFound from openapi_core.testing import MockRequest @@ -50,7 +50,7 @@ def spec_dict(self, factory): @pytest.fixture(scope="module") def spec(self, spec_dict, spec_uri): - return create_spec(spec_dict, spec_uri) + return Spec.create(spec_dict, url=spec_uri) @pytest.fixture(scope="module") def request_validator(self, spec): diff --git a/tests/integration/validation/test_read_only_write_only.py b/tests/integration/validation/test_read_only_write_only.py index b6dca0bf..9b2e7c7a 100644 --- a/tests/integration/validation/test_read_only_write_only.py +++ b/tests/integration/validation/test_read_only_write_only.py @@ -2,7 +2,7 @@ import pytest -from openapi_core.shortcuts import create_spec +from openapi_core.spec import OpenAPIv30Spec as Spec from openapi_core.testing import MockRequest from openapi_core.testing import MockResponse from openapi_core.unmarshalling.schemas.exceptions import InvalidSchemaValue @@ -23,7 +23,7 @@ def request_validator(spec): @pytest.fixture(scope="class") def spec(factory): spec_dict = factory.spec_from_file("data/v3.0/read_only_write_only.yaml") - return create_spec(spec_dict) + return Spec.create(spec_dict) class TestReadOnly: diff --git a/tests/integration/validation/test_security_override.py b/tests/integration/validation/test_security_override.py index d6fabee7..aef5b629 100644 --- a/tests/integration/validation/test_security_override.py +++ b/tests/integration/validation/test_security_override.py @@ -2,7 +2,7 @@ import pytest -from openapi_core.shortcuts import create_spec +from openapi_core.spec import OpenAPIv30Spec as Spec from openapi_core.testing import MockRequest from openapi_core.validation.exceptions import InvalidSecurity from openapi_core.validation.request.validators import RequestValidator @@ -16,7 +16,7 @@ def request_validator(spec): @pytest.fixture(scope="class") def spec(factory): spec_dict = factory.spec_from_file("data/v3.0/security_override.yaml") - return create_spec(spec_dict) + return Spec.create(spec_dict) class TestSecurityOverride: diff --git a/tests/integration/validation/test_validators.py b/tests/integration/validation/test_validators.py index f974b9f5..f3c52339 100644 --- a/tests/integration/validation/test_validators.py +++ b/tests/integration/validation/test_validators.py @@ -11,7 +11,7 @@ from openapi_core.exceptions import MissingRequiredRequestBody from openapi_core.exceptions import MissingResponseContent from openapi_core.extensions.models.models import BaseModel -from openapi_core.shortcuts import create_spec +from openapi_core.spec import OpenAPIv30Spec as Spec from openapi_core.templating.media_types.exceptions import MediaTypeNotFound from openapi_core.templating.paths.exceptions import OperationNotFound from openapi_core.templating.paths.exceptions import PathNotFound @@ -43,7 +43,7 @@ def spec_dict(self, factory): @pytest.fixture(scope="session") def spec(self, spec_dict): - return create_spec(spec_dict) + return Spec.create(spec_dict) @pytest.fixture(scope="session") def validator(self, spec): @@ -445,7 +445,7 @@ def spec_dict(self): @pytest.fixture(scope="session") def spec(self, spec_dict): - return create_spec(spec_dict) + return Spec.create(spec_dict) @pytest.fixture(scope="session") def validator(self, spec): @@ -501,7 +501,7 @@ def test_request_override_param(self, spec_dict): } ] validator = RequestValidator( - create_spec(spec_dict), base_url="http://example.com" + Spec.create(spec_dict), base_url="http://example.com" ) request = MockRequest("http://example.com", "get", "/resource") result = validator.validate(request) @@ -525,7 +525,7 @@ def test_request_override_param_uniqueness(self, spec_dict): } ] validator = RequestValidator( - create_spec(spec_dict), base_url="http://example.com" + Spec.create(spec_dict), base_url="http://example.com" ) request = MockRequest("http://example.com", "get", "/resource") result = validator.validate(request) @@ -546,7 +546,7 @@ def spec_dict(self, factory): @pytest.fixture def spec(self, spec_dict): - return create_spec(spec_dict) + return Spec.create(spec_dict) @pytest.fixture def validator(self, spec): diff --git a/tests/unit/casting/test_schema_casters.py b/tests/unit/casting/test_schema_casters.py index 78ed9b9f..64600bac 100644 --- a/tests/unit/casting/test_schema_casters.py +++ b/tests/unit/casting/test_schema_casters.py @@ -2,7 +2,7 @@ from openapi_core.casting.schemas.exceptions import CastError from openapi_core.casting.schemas.factories import SchemaCastersFactory -from openapi_core.spec.paths import SpecPath +from openapi_core.spec.paths import Spec class TestSchemaCaster: @@ -20,7 +20,7 @@ def test_array_invalid_type(self, caster_factory): "type": "number", }, } - schema = SpecPath.from_spec(spec) + schema = Spec.from_dict(spec) value = ["test", "test2"] with pytest.raises(CastError): @@ -33,7 +33,7 @@ def test_array_invalid_value(self, caster_factory): "type": "number", }, } - schema = SpecPath.from_spec(spec) + schema = Spec.from_dict(spec) value = 3.14 with pytest.raises(CastError): diff --git a/tests/unit/deserializing/test_parameters_deserializers.py b/tests/unit/deserializing/test_parameters_deserializers.py index b51444c6..865bfa3b 100644 --- a/tests/unit/deserializing/test_parameters_deserializers.py +++ b/tests/unit/deserializing/test_parameters_deserializers.py @@ -6,7 +6,7 @@ from openapi_core.deserializing.parameters.factories import ( ParameterDeserializersFactory, ) -from openapi_core.spec.paths import SpecPath +from openapi_core.spec.paths import Spec class TestParameterDeserializer: @@ -19,7 +19,7 @@ def create_deserializer(param): def test_unsupported(self, deserializer_factory): spec = {"name": "param", "in": "header", "style": "unsupported"} - param = SpecPath.from_spec(spec) + param = Spec.from_dict(spec) value = "" with pytest.warns(UserWarning): @@ -32,7 +32,7 @@ def test_query_empty(self, deserializer_factory): "name": "param", "in": "query", } - param = SpecPath.from_spec(spec) + param = Spec.from_dict(spec) value = "" with pytest.raises(EmptyQueryParameterValue): @@ -43,7 +43,7 @@ def test_query_valid(self, deserializer_factory): "name": "param", "in": "query", } - param = SpecPath.from_spec(spec) + param = Spec.from_dict(spec) value = "test" result = deserializer_factory(param)(value) diff --git a/tests/unit/schema/test_schema_parameters.py b/tests/unit/schema/test_schema_parameters.py index f8638490..95cc2762 100644 --- a/tests/unit/schema/test_schema_parameters.py +++ b/tests/unit/schema/test_schema_parameters.py @@ -2,7 +2,7 @@ from openapi_core.schema.parameters import get_explode from openapi_core.schema.parameters import get_style -from openapi_core.spec.paths import SpecPath +from openapi_core.spec.paths import Spec class TestGetStyle: @@ -20,7 +20,7 @@ def test_defaults(self, location, expected): "name": "default", "in": location, } - param = SpecPath.from_spec(spec) + param = Spec.from_dict(spec) result = get_style(param) assert result == expected @@ -45,7 +45,7 @@ def test_defined(self, style, location): "in": location, "style": style, } - param = SpecPath.from_spec(spec) + param = Spec.from_dict(spec) result = get_style(param) assert result == style @@ -69,7 +69,7 @@ def test_defaults_false(self, style, location): "in": location, "style": style, } - param = SpecPath.from_spec(spec) + param = Spec.from_dict(spec) result = get_explode(param) assert result is False @@ -81,7 +81,7 @@ def test_defaults_true(self, location): "in": location, "style": "form", } - param = SpecPath.from_spec(spec) + param = Spec.from_dict(spec) result = get_explode(param) assert result is True @@ -117,7 +117,7 @@ def test_defined(self, location, style, schema_type, explode): "type": schema_type, }, } - param = SpecPath.from_spec(spec) + param = Spec.from_dict(spec) result = get_explode(param) assert result == explode diff --git a/tests/unit/security/test_providers.py b/tests/unit/security/test_providers.py index 560357f4..4a982bd9 100644 --- a/tests/unit/security/test_providers.py +++ b/tests/unit/security/test_providers.py @@ -1,7 +1,7 @@ import pytest from openapi_core.security.providers import HttpProvider -from openapi_core.spec.paths import SpecPath +from openapi_core.spec.paths import Spec from openapi_core.testing import MockRequest @@ -32,7 +32,7 @@ def test_header(self, header, scheme): "/pets", headers=headers, ) - scheme = SpecPath.from_spec(spec) + scheme = Spec.from_dict(spec) provider = HttpProvider(scheme) result = provider(request) diff --git a/tests/unit/templating/test_media_types_finders.py b/tests/unit/templating/test_media_types_finders.py index d71260f6..74436b22 100644 --- a/tests/unit/templating/test_media_types_finders.py +++ b/tests/unit/templating/test_media_types_finders.py @@ -1,6 +1,6 @@ import pytest -from openapi_core.spec.paths import SpecPath +from openapi_core.spec.paths import Spec from openapi_core.templating.media_types.exceptions import MediaTypeNotFound from openapi_core.templating.media_types.finders import MediaTypeFinder from openapi_core.testing import MockResponse @@ -16,7 +16,7 @@ def spec(self): @pytest.fixture(scope="class") def content(self, spec): - return SpecPath.from_spec(spec) + return Spec.from_dict(spec) @pytest.fixture(scope="class") def finder(self, content): diff --git a/tests/unit/templating/test_paths_finders.py b/tests/unit/templating/test_paths_finders.py index 133b0b20..c0afded1 100644 --- a/tests/unit/templating/test_paths_finders.py +++ b/tests/unit/templating/test_paths_finders.py @@ -1,6 +1,6 @@ import pytest -from openapi_core.spec.paths import SpecPath +from openapi_core.spec.paths import Spec from openapi_core.templating.datatypes import TemplateResult from openapi_core.templating.paths.exceptions import OperationNotFound from openapi_core.templating.paths.exceptions import PathNotFound @@ -128,7 +128,7 @@ def spec(self, info, paths, servers): "servers": servers, "paths": paths, } - return SpecPath.from_spec(spec) + return Spec.from_dict(spec) @pytest.fixture def finder(self, spec): @@ -151,7 +151,7 @@ def spec(self, info, paths): "info": info, "paths": paths, } - return SpecPath.from_spec(spec) + return Spec.from_dict(spec) class BaseTestOperationServer(BaseTestSpecServer): @@ -171,7 +171,7 @@ def spec(self, info, paths): "info": info, "paths": paths, } - return SpecPath.from_spec(spec) + return Spec.from_dict(spec) class BaseTestServerNotFound: diff --git a/tests/unit/templating/test_responses_finders.py b/tests/unit/templating/test_responses_finders.py index 8b3500fb..bfcd9af1 100644 --- a/tests/unit/templating/test_responses_finders.py +++ b/tests/unit/templating/test_responses_finders.py @@ -2,7 +2,7 @@ import pytest -from openapi_core.spec.paths import SpecPath +from openapi_core.spec.paths import Spec from openapi_core.templating.responses.finders import ResponseFinder @@ -18,7 +18,7 @@ def spec(self): @pytest.fixture(scope="class") def responses(self, spec): - return SpecPath.from_spec(spec) + return Spec.from_dict(spec) @pytest.fixture(scope="class") def finder(self, responses): diff --git a/tests/unit/unmarshalling/test_unmarshal.py b/tests/unit/unmarshalling/test_unmarshal.py index bb484986..c8d3c2b5 100644 --- a/tests/unit/unmarshalling/test_unmarshal.py +++ b/tests/unit/unmarshalling/test_unmarshal.py @@ -5,7 +5,7 @@ from isodate.tzinfo import UTC from isodate.tzinfo import FixedOffset -from openapi_core.spec.paths import SpecPath +from openapi_core.spec.paths import Spec from openapi_core.unmarshalling.schemas.enums import UnmarshalContext from openapi_core.unmarshalling.schemas.exceptions import ( FormatterNotFoundError, @@ -38,21 +38,21 @@ def create_unmarshaller(schema, custom_formatters=None, context=None): class TestUnmarshal: def test_no_schema(self, unmarshaller_factory): - schema = None + spec = None value = "test" with pytest.raises(TypeError): - unmarshaller_factory(schema).unmarshal(value) + unmarshaller_factory(spec).unmarshal(value) def test_schema_type_invalid(self, unmarshaller_factory): - spec = { + schema = { "type": "integer", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "test" with pytest.raises(InvalidSchemaFormatValue): - unmarshaller_factory(schema).unmarshal(value) + unmarshaller_factory(spec).unmarshal(value) def test_schema_custom_format_invalid(self, unmarshaller_factory): class CustomFormatter(Formatter): @@ -64,31 +64,31 @@ def unmarshal(self, value): custom_formatters = { custom_format: formatter, } - spec = { + schema = { "type": "string", "format": "custom", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "test" with pytest.raises(InvalidSchemaFormatValue): unmarshaller_factory( - schema, + spec, custom_formatters=custom_formatters, ).unmarshal(value) class TestSchemaUnmarshallerCall: def test_deprecated(self, unmarshaller_factory): - spec = { + schema = { "type": "string", "deprecated": True, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "test" with pytest.warns(DeprecationWarning): - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == value @@ -102,118 +102,118 @@ def test_deprecated(self, unmarshaller_factory): ], ) def test_non_string_empty_value(self, schema_type, unmarshaller_factory): - spec = { + schema = { "type": schema_type, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "" with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(schema)(value) + unmarshaller_factory(spec)(value) def test_string_valid(self, unmarshaller_factory): - spec = { + schema = { "type": "string", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "test" - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == value def test_string_format_uuid_valid(self, unmarshaller_factory): - spec = { + schema = { "type": "string", "format": "uuid", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = str(uuid.uuid4()) - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == uuid.UUID(value) def test_string_format_uuid_uuid_quirks_invalid( self, unmarshaller_factory ): - spec = { + schema = { "type": "string", "format": "uuid", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = uuid.uuid4() with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(schema)(value) + unmarshaller_factory(spec)(value) def test_string_format_password(self, unmarshaller_factory): - spec = { + schema = { "type": "string", "format": "password", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "password" - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == "password" def test_string_float_invalid(self, unmarshaller_factory): - spec = { + schema = { "type": "string", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = 1.23 with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(schema)(value) + unmarshaller_factory(spec)(value) def test_string_format_date(self, unmarshaller_factory): - spec = { + schema = { "type": "string", "format": "date", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "2018-01-02" - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == datetime.date(2018, 1, 2) def test_string_format_datetime_invalid(self, unmarshaller_factory): - spec = { + schema = { "type": "string", "format": "date-time", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "2018-01-02T00:00:00" with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(schema)(value) + unmarshaller_factory(spec)(value) def test_string_format_datetime_utc(self, unmarshaller_factory): - spec = { + schema = { "type": "string", "format": "date-time", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "2018-01-02T00:00:00Z" - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) tzinfo = UTC assert result == datetime.datetime(2018, 1, 2, 0, 0, tzinfo=tzinfo) def test_string_format_datetime_tz(self, unmarshaller_factory): - spec = { + schema = { "type": "string", "format": "date-time", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "2020-04-01T12:00:00+02:00" - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) tzinfo = FixedOffset(2) assert result == datetime.datetime(2020, 4, 1, 12, 0, 0, tzinfo=tzinfo) @@ -226,11 +226,11 @@ def unmarshal(self, value): return formatted custom_format = "custom" - spec = { + schema = { "type": "string", "format": custom_format, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "x" formatter = CustomFormatter() custom_formatters = { @@ -238,7 +238,7 @@ def unmarshal(self, value): } result = unmarshaller_factory( - schema, custom_formatters=custom_formatters + spec, custom_formatters=custom_formatters )(value) assert result == formatted @@ -249,11 +249,11 @@ def unmarshal(self, value): raise ValueError custom_format = "custom" - spec = { + schema = { "type": "string", "format": custom_format, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "x" formatter = CustomFormatter() custom_formatters = { @@ -261,271 +261,271 @@ def unmarshal(self, value): } with pytest.raises(InvalidSchemaFormatValue): - unmarshaller_factory(schema, custom_formatters=custom_formatters)( + unmarshaller_factory(spec, custom_formatters=custom_formatters)( value ) def test_string_format_unknown(self, unmarshaller_factory): unknown_format = "unknown" - spec = { + schema = { "type": "string", "format": unknown_format, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "x" with pytest.raises(FormatterNotFoundError): - unmarshaller_factory(schema)(value) + unmarshaller_factory(spec)(value) def test_string_format_invalid_value(self, unmarshaller_factory): custom_format = "custom" - spec = { + schema = { "type": "string", "format": custom_format, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "x" with pytest.raises( FormatterNotFoundError, match="Formatter not found for custom format", ): - unmarshaller_factory(schema)(value) + unmarshaller_factory(spec)(value) def test_integer_valid(self, unmarshaller_factory): - spec = { + schema = { "type": "integer", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = 123 - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == int(value) def test_integer_string_invalid(self, unmarshaller_factory): - spec = { + schema = { "type": "integer", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "123" with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(schema)(value) + unmarshaller_factory(spec)(value) def test_integer_enum_invalid(self, unmarshaller_factory): - spec = { + schema = { "type": "integer", "enum": [1, 2, 3], } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "123" with pytest.raises(UnmarshalError): - unmarshaller_factory(schema)(value) + unmarshaller_factory(spec)(value) def test_integer_enum(self, unmarshaller_factory): - spec = { + schema = { "type": "integer", "enum": [1, 2, 3], } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = 2 - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == int(value) def test_integer_enum_string_invalid(self, unmarshaller_factory): - spec = { + schema = { "type": "integer", "enum": [1, 2, 3], } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "2" with pytest.raises(UnmarshalError): - unmarshaller_factory(schema)(value) + unmarshaller_factory(spec)(value) def test_integer_default_nullable(self, unmarshaller_factory): default_value = 123 - spec = { + schema = { "type": "integer", "default": default_value, "nullable": True, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = None - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result is None def test_integer_invalid(self, unmarshaller_factory): - spec = { + schema = { "type": "integer", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "abc" with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(schema)(value) + unmarshaller_factory(spec)(value) def test_array_valid(self, unmarshaller_factory): - spec = { + schema = { "type": "array", "items": { "type": "integer", }, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = [1, 2, 3] - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == value def test_array_null(self, unmarshaller_factory): - spec = { + schema = { "type": "array", "items": { "type": "integer", }, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = None with pytest.raises(TypeError): - unmarshaller_factory(schema)(value) + unmarshaller_factory(spec)(value) def test_array_nullable(self, unmarshaller_factory): - spec = { + schema = { "type": "array", "items": { "type": "integer", }, "nullable": True, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = None - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result is None def test_array_of_string_string_invalid(self, unmarshaller_factory): - spec = { + schema = { "type": "array", "items": { "type": "string", }, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "123" with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(schema)(value) + unmarshaller_factory(spec)(value) def test_array_of_integer_string_invalid(self, unmarshaller_factory): - spec = { + schema = { "type": "array", "items": { "type": "integer", }, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "123" with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(schema)(value) + unmarshaller_factory(spec)(value) def test_boolean_valid(self, unmarshaller_factory): - spec = { + schema = { "type": "boolean", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = True - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == value def test_boolean_string_invalid(self, unmarshaller_factory): - spec = { + schema = { "type": "boolean", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "True" with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(schema)(value) + unmarshaller_factory(spec)(value) def test_number_valid(self, unmarshaller_factory): - spec = { + schema = { "type": "number", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = 1.23 - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == value def test_number_string_invalid(self, unmarshaller_factory): - spec = { + schema = { "type": "number", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "1.23" with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(schema)(value) + unmarshaller_factory(spec)(value) def test_number_int(self, unmarshaller_factory): - spec = { + schema = { "type": "number", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = 1 - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == 1 assert type(result) == int def test_number_float(self, unmarshaller_factory): - spec = { + schema = { "type": "number", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = 1.2 - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == 1.2 assert type(result) == float def test_number_format_float(self, unmarshaller_factory): - spec = { + schema = { "type": "number", "format": "float", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = 1.2 - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == 1.2 def test_number_format_double(self, unmarshaller_factory): - spec = { + schema = { "type": "number", "format": "double", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = 1.2 - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == 1.2 def test_object_nullable(self, unmarshaller_factory): - spec = { + schema = { "type": "object", "properties": { "foo": { @@ -534,14 +534,14 @@ def test_object_nullable(self, unmarshaller_factory): } }, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = {"foo": None} - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == {"foo": None} def test_schema_any_one_of(self, unmarshaller_factory): - spec = { + schema = { "oneOf": [ { "type": "string", @@ -554,11 +554,11 @@ def test_schema_any_one_of(self, unmarshaller_factory): }, ], } - schema = SpecPath.from_spec(spec) - assert unmarshaller_factory(schema)(["hello"]) == ["hello"] + spec = Spec.from_dict(schema) + assert unmarshaller_factory(spec)(["hello"]) == ["hello"] def test_schema_any_all_of(self, unmarshaller_factory): - spec = { + schema = { "allOf": [ { "type": "array", @@ -568,8 +568,8 @@ def test_schema_any_all_of(self, unmarshaller_factory): } ], } - schema = SpecPath.from_spec(spec) - assert unmarshaller_factory(schema)(["hello"]) == ["hello"] + spec = Spec.from_dict(schema) + assert unmarshaller_factory(spec)(["hello"]) == ["hello"] @pytest.mark.parametrize( "value", @@ -596,7 +596,7 @@ def test_schema_any_all_of(self, unmarshaller_factory): def test_schema_any_all_of_invalid_properties( self, value, unmarshaller_factory ): - spec = { + schema = { "allOf": [ { "type": "object", @@ -619,13 +619,13 @@ def test_schema_any_all_of_invalid_properties( ], "additionalProperties": False, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(schema)(value) + unmarshaller_factory(spec)(value) def test_schema_any_all_of_any(self, unmarshaller_factory): - spec = { + schema = { "allOf": [ {}, { @@ -634,17 +634,17 @@ def test_schema_any_all_of_any(self, unmarshaller_factory): }, ], } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "2018-01-02" - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == datetime.date(2018, 1, 2) def test_schema_any(self, unmarshaller_factory): - spec = {} - schema = SpecPath.from_spec(spec) - assert unmarshaller_factory(schema)("string") == "string" + schema = {} + spec = Spec.from_dict(schema) + assert unmarshaller_factory(spec)("string") == "string" @pytest.mark.parametrize( "value", @@ -658,17 +658,17 @@ def test_schema_any(self, unmarshaller_factory): def test_schema_free_form_object( self, value, additional_properties, unmarshaller_factory ): - spec = { + schema = { "type": "object", "additionalProperties": additional_properties, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = unmarshaller_factory(schema)(value) + result = unmarshaller_factory(spec)(value) assert result == value def test_read_only_properties(self, unmarshaller_factory): - spec = { + schema = { "type": "object", "required": ["id"], "properties": { @@ -678,18 +678,18 @@ def test_read_only_properties(self, unmarshaller_factory): } }, } - obj_schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) # readOnly properties may be admitted in a Response context - result = unmarshaller_factory( - obj_schema, context=UnmarshalContext.RESPONSE - )({"id": 10}) + result = unmarshaller_factory(spec, context=UnmarshalContext.RESPONSE)( + {"id": 10} + ) assert result == { "id": 10, } def test_read_only_properties_invalid(self, unmarshaller_factory): - spec = { + schema = { "type": "object", "required": ["id"], "properties": { @@ -699,16 +699,16 @@ def test_read_only_properties_invalid(self, unmarshaller_factory): } }, } - obj_schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) # readOnly properties are not admitted on a Request context with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(obj_schema, context=UnmarshalContext.REQUEST)( + unmarshaller_factory(spec, context=UnmarshalContext.REQUEST)( {"id": 10} ) def test_write_only_properties(self, unmarshaller_factory): - spec = { + schema = { "type": "object", "required": ["id"], "properties": { @@ -718,18 +718,18 @@ def test_write_only_properties(self, unmarshaller_factory): } }, } - obj_schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) # readOnly properties may be admitted in a Response context - result = unmarshaller_factory( - obj_schema, context=UnmarshalContext.REQUEST - )({"id": 10}) + result = unmarshaller_factory(spec, context=UnmarshalContext.REQUEST)( + {"id": 10} + ) assert result == { "id": 10, } def test_write_only_properties_invalid(self, unmarshaller_factory): - spec = { + schema = { "type": "object", "required": ["id"], "properties": { @@ -739,10 +739,10 @@ def test_write_only_properties_invalid(self, unmarshaller_factory): } }, } - obj_schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) # readOnly properties are not admitted on a Request context with pytest.raises(InvalidSchemaValue): - unmarshaller_factory( - obj_schema, context=UnmarshalContext.RESPONSE - )({"id": 10}) + unmarshaller_factory(spec, context=UnmarshalContext.RESPONSE)( + {"id": 10} + ) diff --git a/tests/unit/unmarshalling/test_validate.py b/tests/unit/unmarshalling/test_validate.py index 5a409dd7..60bf8f07 100644 --- a/tests/unit/unmarshalling/test_validate.py +++ b/tests/unit/unmarshalling/test_validate.py @@ -4,7 +4,7 @@ import pytest from openapi_core.extensions.models.models import Model -from openapi_core.spec.paths import SpecPath +from openapi_core.spec.paths import Spec from openapi_core.unmarshalling.schemas.exceptions import ( FormatterNotFoundError, ) @@ -37,14 +37,14 @@ def create_validator(schema): ], ) def test_null(self, schema_type, validator_factory): - spec = { + schema = { "type": schema_type, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = None with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "schema_type", @@ -57,334 +57,334 @@ def test_null(self, schema_type, validator_factory): ], ) def test_nullable(self, schema_type, validator_factory): - spec = { + schema = { "type": schema_type, "nullable": True, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = None - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None def test_string_format_custom_missing(self, validator_factory): custom_format = "custom" - spec = { + schema = { "type": "string", "format": custom_format, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) value = "x" with pytest.raises(FormatterNotFoundError): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [False, True]) def test_boolean(self, value, validator_factory): - spec = { + schema = { "type": "boolean", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [1, 3.14, "true", [True, False]]) def test_boolean_invalid(self, value, validator_factory): - spec = { + schema = { "type": "boolean", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [(1, 2)]) def test_array_no_schema(self, value, validator_factory): - spec = { + schema = { "type": "array", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [[1, 2]]) def test_array(self, value, validator_factory): - spec = { + schema = { "type": "array", "items": { "type": "integer", }, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [False, 1, 3.14, "true", (3, 4)]) def test_array_invalid(self, value, validator_factory): - spec = { + schema = { "type": "array", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [1, 3]) def test_integer(self, value, validator_factory): - spec = { + schema = { "type": "integer", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [False, 3.14, "true", [1, 2]]) def test_integer_invalid(self, value, validator_factory): - spec = { + schema = { "type": "integer", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [0, 1, 2]) def test_integer_minimum_invalid(self, value, validator_factory): - spec = { + schema = { "type": "integer", "minimum": 3, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [4, 5, 6]) def test_integer_minimum(self, value, validator_factory): - spec = { + schema = { "type": "integer", "minimum": 3, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [4, 5, 6]) def test_integer_maximum_invalid(self, value, validator_factory): - spec = { + schema = { "type": "integer", "maximum": 3, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [0, 1, 2]) def test_integer_maximum(self, value, validator_factory): - spec = { + schema = { "type": "integer", "maximum": 3, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [1, 2, 4]) def test_integer_multiple_of_invalid(self, value, validator_factory): - spec = { + schema = { "type": "integer", "multipleOf": 3, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [3, 6, 18]) def test_integer_multiple_of(self, value, validator_factory): - spec = { + schema = { "type": "integer", "multipleOf": 3, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [1, 3.14]) def test_number(self, value, validator_factory): - spec = { + schema = { "type": "number", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [False, "true", [1, 3]]) def test_number_invalid(self, value, validator_factory): - spec = { + schema = { "type": "number", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [0, 1, 2]) def test_number_minimum_invalid(self, value, validator_factory): - spec = { + schema = { "type": "number", "minimum": 3, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [3, 4, 5]) def test_number_minimum(self, value, validator_factory): - spec = { + schema = { "type": "number", "minimum": 3, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [1, 2, 3]) def test_number_exclusive_minimum_invalid(self, value, validator_factory): - spec = { + schema = { "type": "number", "minimum": 3, "exclusiveMinimum": True, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [4, 5, 6]) def test_number_exclusive_minimum(self, value, validator_factory): - spec = { + schema = { "type": "number", "minimum": 3, "exclusiveMinimum": True, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [4, 5, 6]) def test_number_maximum_invalid(self, value, validator_factory): - spec = { + schema = { "type": "number", "maximum": 3, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [1, 2, 3]) def test_number_maximum(self, value, validator_factory): - spec = { + schema = { "type": "number", "maximum": 3, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [3, 4, 5]) def test_number_exclusive_maximum_invalid(self, value, validator_factory): - spec = { + schema = { "type": "number", "maximum": 3, "exclusiveMaximum": True, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [0, 1, 2]) def test_number_exclusive_maximum(self, value, validator_factory): - spec = { + schema = { "type": "number", "maximum": 3, "exclusiveMaximum": True, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [1, 2, 4]) def test_number_multiple_of_invalid(self, value, validator_factory): - spec = { + schema = { "type": "number", "multipleOf": 3, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [3, 6, 18]) def test_number_multiple_of(self, value, validator_factory): - spec = { + schema = { "type": "number", "multipleOf": 3, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", ["true", b"test"]) def test_string(self, value, validator_factory): - spec = { + schema = { "type": "string", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [False, 1, 3.14, [1, 3]]) def test_string_invalid(self, value, validator_factory): - spec = { + schema = { "type": "string", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", @@ -399,14 +399,14 @@ def test_string_invalid(self, value, validator_factory): ], ) def test_string_format_date_invalid(self, value, validator_factory): - spec = { + schema = { "type": "string", "format": "date", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", @@ -416,13 +416,13 @@ def test_string_format_date_invalid(self, value, validator_factory): ], ) def test_string_format_date(self, value, validator_factory): - spec = { + schema = { "type": "string", "format": "date", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @@ -433,13 +433,13 @@ def test_string_format_date(self, value, validator_factory): ], ) def test_string_format_uuid(self, value, validator_factory): - spec = { + schema = { "type": "string", "format": "uuid", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @@ -457,14 +457,14 @@ def test_string_format_uuid(self, value, validator_factory): ], ) def test_string_format_uuid_invalid(self, value, validator_factory): - spec = { + schema = { "type": "string", "format": "uuid", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", @@ -479,14 +479,14 @@ def test_string_format_uuid_invalid(self, value, validator_factory): ], ) def test_string_format_datetime_invalid(self, value, validator_factory): - spec = { + schema = { "type": "string", "format": "date-time", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", @@ -504,13 +504,13 @@ def test_string_format_datetime_invalid(self, value, validator_factory): def test_string_format_datetime_strict_rfc3339( self, value, validator_factory ): - spec = { + schema = { "type": "string", "format": "date-time", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @@ -529,13 +529,13 @@ def test_string_format_datetime_strict_rfc3339( "openapi_schema_validator._format." "DATETIME_HAS_ISODATE", True ) def test_string_format_datetime_isodate(self, value, validator_factory): - spec = { + schema = { "type": "string", "format": "date-time", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @@ -552,14 +552,14 @@ def test_string_format_datetime_isodate(self, value, validator_factory): ], ) def test_string_format_binary_invalid(self, value, validator_factory): - spec = { + schema = { "type": "string", "format": "binary", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", @@ -569,13 +569,13 @@ def test_string_format_binary_invalid(self, value, validator_factory): ], ) def test_string_format_binary(self, value, validator_factory): - spec = { + schema = { "type": "string", "format": "binary", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @@ -587,13 +587,13 @@ def test_string_format_binary(self, value, validator_factory): ], ) def test_string_format_byte(self, value, validator_factory): - spec = { + schema = { "type": "string", "format": "byte", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @@ -606,14 +606,14 @@ def test_string_format_byte(self, value, validator_factory): ], ) def test_string_format_byte_invalid(self, value, validator_factory): - spec = { + schema = { "type": "string", "format": "byte", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", @@ -626,35 +626,35 @@ def test_string_format_byte_invalid(self, value, validator_factory): ) def test_string_format_unknown(self, value, validator_factory): unknown_format = "unknown" - spec = { + schema = { "type": "string", "format": unknown_format, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(FormatterNotFoundError): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", ["", "a", "ab"]) def test_string_min_length_invalid(self, value, validator_factory): - spec = { + schema = { "type": "string", "minLength": 3, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", ["abc", "abcd"]) def test_string_min_length(self, value, validator_factory): - spec = { + schema = { "type": "string", "minLength": 3, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @@ -665,70 +665,70 @@ def test_string_min_length(self, value, validator_factory): ], ) def test_string_max_length_invalid_schema(self, value, validator_factory): - spec = { + schema = { "type": "string", "maxLength": -1, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", ["ab", "abc"]) def test_string_max_length_invalid(self, value, validator_factory): - spec = { + schema = { "type": "string", "maxLength": 1, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", ["", "a"]) def test_string_max_length(self, value, validator_factory): - spec = { + schema = { "type": "string", "maxLength": 1, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", ["foo", "bar"]) def test_string_pattern_invalid(self, value, validator_factory): - spec = { + schema = { "type": "string", "pattern": "baz", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", ["bar", "foobar"]) def test_string_pattern(self, value, validator_factory): - spec = { + schema = { "type": "string", "pattern": "bar", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", ["true", False, 1, 3.14, [1, 3]]) def test_object_not_an_object(self, value, validator_factory): - spec = { + schema = { "type": "object", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", @@ -745,14 +745,14 @@ def test_object_multiple_one_of(self, value, validator_factory): "type": "object", }, ] - spec = { + schema = { "type": "object", "oneOf": one_of, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", @@ -769,14 +769,14 @@ def test_object_different_type_one_of(self, value, validator_factory): "type": "string", }, ] - spec = { + schema = { "type": "object", "oneOf": one_of, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", @@ -809,14 +809,14 @@ def test_object_no_one_of(self, value, validator_factory): }, }, ] - spec = { + schema = { "type": "object", "oneOf": one_of, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", @@ -858,13 +858,13 @@ def test_unambiguous_one_of(self, value, validator_factory): "additionalProperties": False, }, ] - spec = { + schema = { "type": "object", "oneOf": one_of, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @@ -875,13 +875,13 @@ def test_unambiguous_one_of(self, value, validator_factory): ], ) def test_object_default_property(self, value, validator_factory): - spec = { + schema = { "type": "object", "default": "value1", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @@ -894,14 +894,14 @@ def test_object_default_property(self, value, validator_factory): def test_object_min_properties_invalid_schema( self, value, validator_factory ): - spec = { + schema = { "type": "object", "minProperties": 2, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", @@ -912,15 +912,15 @@ def test_object_min_properties_invalid_schema( ], ) def test_object_min_properties_invalid(self, value, validator_factory): - spec = { + schema = { "type": "object", "properties": {k: {"type": "number"} for k in ["a", "b", "c"]}, "minProperties": 4, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", @@ -931,13 +931,13 @@ def test_object_min_properties_invalid(self, value, validator_factory): ], ) def test_object_min_properties(self, value, validator_factory): - spec = { + schema = { "type": "object", "properties": {k: {"type": "number"} for k in ["a", "b", "c"]}, "minProperties": 1, } - schema = SpecPath.from_spec(spec) - result = validator_factory(schema).validate(value) + spec = Spec.from_dict(schema) + result = validator_factory(spec).validate(value) assert result is None @@ -950,14 +950,14 @@ def test_object_min_properties(self, value, validator_factory): def test_object_max_properties_invalid_schema( self, value, validator_factory ): - spec = { + schema = { "type": "object", "maxProperties": -1, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", @@ -968,15 +968,15 @@ def test_object_max_properties_invalid_schema( ], ) def test_object_max_properties_invalid(self, value, validator_factory): - spec = { + schema = { "type": "object", "properties": {k: {"type": "number"} for k in ["a", "b", "c"]}, "maxProperties": 0, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", @@ -987,14 +987,14 @@ def test_object_max_properties_invalid(self, value, validator_factory): ], ) def test_object_max_properties(self, value, validator_factory): - spec = { + schema = { "type": "object", "properties": {k: {"type": "number"} for k in ["a", "b", "c"]}, "maxProperties": 3, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @@ -1005,12 +1005,12 @@ def test_object_max_properties(self, value, validator_factory): ], ) def test_object_additional_properties(self, value, validator_factory): - spec = { + schema = { "type": "object", } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @@ -1023,14 +1023,14 @@ def test_object_additional_properties(self, value, validator_factory): def test_object_additional_properties_false( self, value, validator_factory ): - spec = { + schema = { "type": "object", "additionalProperties": False, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", @@ -1044,42 +1044,42 @@ def test_object_additional_properties_object( additional_properties = { "type": "integer", } - spec = { + schema = { "type": "object", "additionalProperties": additional_properties, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [[], [1], [1, 2]]) def test_list_min_items_invalid(self, value, validator_factory): - spec = { + schema = { "type": "array", "items": { "type": "number", }, "minItems": 3, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(Exception): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [[], [1], [1, 2]]) def test_list_min_items(self, value, validator_factory): - spec = { + schema = { "type": "array", "items": { "type": "number", }, "minItems": 0, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @@ -1090,45 +1090,45 @@ def test_list_min_items(self, value, validator_factory): ], ) def test_list_max_items_invalid_schema(self, value, validator_factory): - spec = { + schema = { "type": "array", "items": { "type": "number", }, "maxItems": -1, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [[1, 2], [2, 3, 4]]) def test_list_max_items_invalid(self, value, validator_factory): - spec = { + schema = { "type": "array", "items": { "type": "number", }, "maxItems": 1, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(Exception): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [[1, 2, 1], [2, 2]]) def test_list_unique_items_invalid(self, value, validator_factory): - spec = { + schema = { "type": "array", "items": { "type": "number", }, "uniqueItems": True, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(Exception): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", @@ -1146,7 +1146,7 @@ def test_list_unique_items_invalid(self, value, validator_factory): ], ) def test_object_with_properties(self, value, validator_factory): - spec = { + schema = { "type": "object", "properties": { "somestr": { @@ -1157,9 +1157,9 @@ def test_object_with_properties(self, value, validator_factory): }, }, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) - result = validator_factory(schema).validate(value) + result = validator_factory(spec).validate(value) assert result is None @@ -1186,7 +1186,7 @@ def test_object_with_properties(self, value, validator_factory): ], ) def test_object_with_invalid_properties(self, value, validator_factory): - spec = { + schema = { "type": "object", "properties": { "somestr": { @@ -1198,7 +1198,7 @@ def test_object_with_invalid_properties(self, value, validator_factory): }, "additionalProperties": False, } - schema = SpecPath.from_spec(spec) + spec = Spec.from_dict(schema) with pytest.raises(Exception): - validator_factory(schema).validate(value) + validator_factory(spec).validate(value)