Skip to content

Commit 706a7ea

Browse files
authored
Merge pull request #463 from p1c2u/feature/spec-create-deprecated
Spec.create deprecated
2 parents fb33fa6 + 516b1f9 commit 706a7ea

File tree

24 files changed

+218
-187
lines changed

24 files changed

+218
-187
lines changed

README.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,9 @@ Firstly create your specification object. By default, OpenAPI spec version is de
6262

6363
.. code-block:: python
6464
65-
from json import load
6665
from openapi_core import Spec
6766
68-
with open('openapi.json', 'r') as spec_file:
69-
spec_dict = load(spec_file)
70-
71-
spec = Spec.create(spec_dict)
67+
spec = Spec.from_file_path('openapi.json')
7268
7369
Request
7470
*******

docs/customizations.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ Customizations
44
Spec validation
55
---------------
66

7-
By default, spec dict is validated on spec creation time. Disabling the validator can improve the performance.
7+
By default, the provided specification is validated on ``Spec`` object creation time.
8+
9+
If you know you have a valid specification already, disabling the validator can improve the performance.
810

911
.. code-block:: python
1012
1113
from openapi_core import Spec
1214
13-
spec = Spec.create(spec_dict, validator=None)
15+
spec = Spec.from_dict(spec_dict, validator=None)
1416
1517
Deserializers
1618
-------------

docs/integrations.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Django can be integrated by middleware. Add ``DjangoOpenAPIMiddleware`` to your
2828
'openapi_core.contrib.django.middlewares.DjangoOpenAPIMiddleware',
2929
]
3030
31-
OPENAPI_SPEC = Spec.create(spec_dict)
31+
OPENAPI_SPEC = Spec.from_dict(spec_dict)
3232
3333
After that you have access to validation result object with all validated request data from Django view through request object.
3434

docs/usage.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ Firstly create your specification object. By default, OpenAPI spec version is de
55

66
.. code-block:: python
77
8-
from json import load
98
from openapi_core import Spec
109
11-
with open('openapi.json', 'r') as spec_file:
12-
spec_dict = load(spec_file)
13-
14-
spec = Spec.create(spec_dict)
10+
spec = Spec.from_file_path('openapi.json')
1511
1612
1713
Request

openapi_core/spec/paths.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warnings
12
from typing import Any
23
from typing import Dict
34
from typing import Hashable
@@ -27,13 +28,37 @@ def create(
2728
separator: str = SPEC_SEPARATOR,
2829
validator: Optional[SupportsValidation] = openapi_spec_validator_proxy,
2930
) -> TSpec:
30-
if validator is not None:
31-
validator.validate(data, spec_url=url)
31+
warnings.warn(
32+
"Spec.create method is deprecated. Use Spec.from_dict instead.",
33+
DeprecationWarning,
34+
)
3235

3336
return cls.from_dict(
3437
data,
3538
*args,
3639
spec_url=url,
3740
ref_resolver_handlers=ref_resolver_handlers,
3841
separator=separator,
42+
validator=validator,
43+
)
44+
45+
@classmethod
46+
def from_dict(
47+
cls: Type[TSpec],
48+
data: Mapping[Hashable, Any],
49+
*args: Any,
50+
spec_url: str = "",
51+
ref_resolver_handlers: Mapping[str, Any] = default_handlers,
52+
separator: str = SPEC_SEPARATOR,
53+
validator: Optional[SupportsValidation] = openapi_spec_validator_proxy,
54+
) -> TSpec:
55+
if validator is not None:
56+
validator.validate(data, spec_url=spec_url)
57+
58+
return super().from_dict(
59+
data,
60+
*args,
61+
spec_url=spec_url,
62+
ref_resolver_handlers=ref_resolver_handlers,
63+
separator=separator,
3964
)

openapi_core/spec/shortcuts.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""OpenAPI core spec shortcuts module"""
2+
import warnings
23
from typing import Any
34
from typing import Dict
45
from typing import Hashable
@@ -18,13 +19,18 @@ def create_spec(
1819
handlers: Dict[str, Any] = default_handlers,
1920
validate_spec: bool = True,
2021
) -> Spec:
22+
warnings.warn(
23+
"create_spec function is deprecated. Use Spec.from_dict instead.",
24+
DeprecationWarning,
25+
)
26+
2127
validator: Optional[SupportsValidation] = None
2228
if validate_spec:
2329
validator = openapi_spec_validator_proxy
2430

25-
return Spec.create(
31+
return Spec.from_dict(
2632
spec_dict,
27-
url=spec_url,
33+
spec_url=spec_url,
2834
ref_resolver_handlers=handlers,
2935
validator=validator,
3036
)

openapi_core/unmarshalling/schemas/unmarshallers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,9 @@ class ArrayUnmarshaller(ComplexUnmarshaller):
294294
@property
295295
def items_unmarshaller(self) -> "BaseSchemaUnmarshaller":
296296
# sometimes we don't have any schema i.e. free-form objects
297-
items_schema = self.schema.get("items", Spec.from_dict({}))
297+
items_schema = self.schema.get(
298+
"items", Spec.from_dict({}, validator=None)
299+
)
298300
return self.unmarshallers_factory.create(items_schema)
299301

300302
def unmarshal(self, value: Any) -> Optional[List[Any]]:
@@ -383,7 +385,9 @@ def _unmarshal_properties(
383385
if additional_properties is not False:
384386
# free-form object
385387
if additional_properties is True:
386-
additional_prop_schema = Spec.from_dict({"nullable": True})
388+
additional_prop_schema = Spec.from_dict(
389+
{"nullable": True}, validator=None
390+
)
387391
# defined schema
388392
else:
389393
additional_prop_schema = self.schema / "additionalProperties"

tests/integration/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ def content_from_file(spec_file):
1616

1717
def spec_from_file(spec_file):
1818
spec_dict, spec_url = content_from_file(spec_file)
19-
return Spec.create(spec_dict, url=spec_url)
19+
return Spec.from_dict(spec_dict, spec_url=spec_url)
2020

2121

2222
def spec_from_url(spec_url):
2323
content = request.urlopen(spec_url)
2424
spec_dict = safe_load(content)
25-
return Spec.create(spec_dict, url=spec_url)
25+
return Spec.from_dict(spec_dict, spec_url=spec_url)
2626

2727

2828
class Factory(dict):

tests/integration/contrib/django/data/v3.0/djangoproject/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,4 @@
123123

124124
OPENAPI_SPEC_DICT = yaml.load(OPENAPI_SPEC_PATH.read_text(), yaml.Loader)
125125

126-
OPENAPI_SPEC = Spec.create(OPENAPI_SPEC_DICT)
126+
OPENAPI_SPEC = Spec.from_dict(OPENAPI_SPEC_DICT)

tests/integration/contrib/falcon/data/v3.0/falconproject/openapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77

88
openapi_spec_path = Path("tests/integration/data/v3.0/petstore.yaml")
99
spec_dict = yaml.load(openapi_spec_path.read_text(), yaml.Loader)
10-
spec = Spec.create(spec_dict)
10+
spec = Spec.from_dict(spec_dict)
1111
openapi_middleware = FalconOpenAPIMiddleware.from_spec(spec)

0 commit comments

Comments
 (0)