Skip to content

Commit 4859acc

Browse files
committed
Move to jsonschema-path
1 parent 4fac4d1 commit 4859acc

File tree

28 files changed

+253
-241
lines changed

28 files changed

+253
-241
lines changed

Diff for: docs/integrations.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,29 @@ Django can be integrated by middleware. Add ``DjangoOpenAPIMiddleware`` to your
5454
:emphasize-lines: 6,9
5555
5656
# settings.py
57-
from openapi_core import Spec
57+
from jsonschema_path import SchemaPath
5858
5959
MIDDLEWARE = [
6060
# ...
6161
'openapi_core.contrib.django.middlewares.DjangoOpenAPIMiddleware',
6262
]
6363
64-
OPENAPI_SPEC = Spec.from_dict(spec_dict)
64+
OPENAPI_SPEC = SchemaPath.from_dict(spec_dict)
6565
6666
You can skip response validation process: by setting ``OPENAPI_RESPONSE_CLS`` to ``None``
6767

6868
.. code-block:: python
6969
:emphasize-lines: 10
7070
7171
# settings.py
72-
from openapi_core import Spec
72+
from jsonschema_path import SchemaPath
7373
7474
MIDDLEWARE = [
7575
# ...
7676
'openapi_core.contrib.django.middlewares.DjangoOpenAPIMiddleware',
7777
]
7878
79-
OPENAPI_SPEC = Spec.from_dict(spec_dict)
79+
OPENAPI_SPEC = SchemaPath.from_dict(spec_dict)
8080
OPENAPI_RESPONSE_CLS = None
8181
8282
After that you have access to unmarshal result object with all validated request data from Django view through request object.

Diff for: openapi_core/shortcuts.py

+38-37
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Optional
55
from typing import Union
66

7+
from jsonschema_path import SchemaPath
78
from openapi_core.exceptions import SpecError
89
from openapi_core.finders import SpecClasses
910
from openapi_core.finders import SpecFinder
@@ -85,21 +86,21 @@
8586
}
8687

8788

88-
def get_classes(spec: Spec) -> SpecClasses:
89+
def get_classes(spec: SchemaPath) -> SpecClasses:
8990
return SpecFinder(SPECS).get_classes(spec)
9091

9192

9293
def unmarshal_apicall_request(
9394
request: Request,
94-
spec: Spec,
95+
spec: SchemaPath,
9596
base_url: Optional[str] = None,
9697
cls: Optional[RequestUnmarshallerType] = None,
9798
**unmarshaller_kwargs: Any,
9899
) -> RequestUnmarshalResult:
99100
if not isinstance(request, Request):
100101
raise TypeError("'request' argument is not type of Request")
101-
if not isinstance(spec, Spec):
102-
raise TypeError("'spec' argument is not type of Spec")
102+
if not isinstance(spec, SchemaPath):
103+
raise TypeError("'spec' argument is not type of SchemaPath")
103104
if cls is None:
104105
classes = get_classes(spec)
105106
cls = classes.request_unmarshaller_cls
@@ -113,15 +114,15 @@ def unmarshal_apicall_request(
113114

114115
def unmarshal_webhook_request(
115116
request: WebhookRequest,
116-
spec: Spec,
117+
spec: SchemaPath,
117118
base_url: Optional[str] = None,
118119
cls: Optional[WebhookRequestUnmarshallerType] = None,
119120
**unmarshaller_kwargs: Any,
120121
) -> RequestUnmarshalResult:
121122
if not isinstance(request, WebhookRequest):
122123
raise TypeError("'request' argument is not type of WebhookRequest")
123-
if not isinstance(spec, Spec):
124-
raise TypeError("'spec' argument is not type of Spec")
124+
if not isinstance(spec, SchemaPath):
125+
raise TypeError("'spec' argument is not type of SchemaPath")
125126
if cls is None:
126127
classes = get_classes(spec)
127128
cls = classes.webhook_request_unmarshaller_cls
@@ -139,15 +140,15 @@ def unmarshal_webhook_request(
139140

140141
def unmarshal_request(
141142
request: AnyRequest,
142-
spec: Spec,
143+
spec: SchemaPath,
143144
base_url: Optional[str] = None,
144145
cls: Optional[AnyRequestUnmarshallerType] = None,
145146
**unmarshaller_kwargs: Any,
146147
) -> RequestUnmarshalResult:
147148
if not isinstance(request, (Request, WebhookRequest)):
148149
raise TypeError("'request' argument is not type of (Webhook)Request")
149-
if not isinstance(spec, Spec):
150-
raise TypeError("'spec' argument is not type of Spec")
150+
if not isinstance(spec, SchemaPath):
151+
raise TypeError("'spec' argument is not type of SchemaPath")
151152
if isinstance(request, WebhookRequest):
152153
if cls is None or issubclass(cls, WebhookRequestUnmarshaller):
153154
return unmarshal_webhook_request(
@@ -179,7 +180,7 @@ def unmarshal_request(
179180
def unmarshal_apicall_response(
180181
request: Request,
181182
response: Response,
182-
spec: Spec,
183+
spec: SchemaPath,
183184
base_url: Optional[str] = None,
184185
cls: Optional[ResponseUnmarshallerType] = None,
185186
**unmarshaller_kwargs: Any,
@@ -188,8 +189,8 @@ def unmarshal_apicall_response(
188189
raise TypeError("'request' argument is not type of Request")
189190
if not isinstance(response, Response):
190191
raise TypeError("'response' argument is not type of Response")
191-
if not isinstance(spec, Spec):
192-
raise TypeError("'spec' argument is not type of Spec")
192+
if not isinstance(spec, SchemaPath):
193+
raise TypeError("'spec' argument is not type of SchemaPath")
193194
if cls is None:
194195
classes = get_classes(spec)
195196
cls = classes.response_unmarshaller_cls
@@ -204,7 +205,7 @@ def unmarshal_apicall_response(
204205
def unmarshal_webhook_response(
205206
request: WebhookRequest,
206207
response: Response,
207-
spec: Spec,
208+
spec: SchemaPath,
208209
base_url: Optional[str] = None,
209210
cls: Optional[WebhookResponseUnmarshallerType] = None,
210211
**unmarshaller_kwargs: Any,
@@ -213,8 +214,8 @@ def unmarshal_webhook_response(
213214
raise TypeError("'request' argument is not type of WebhookRequest")
214215
if not isinstance(response, Response):
215216
raise TypeError("'response' argument is not type of Response")
216-
if not isinstance(spec, Spec):
217-
raise TypeError("'spec' argument is not type of Spec")
217+
if not isinstance(spec, SchemaPath):
218+
raise TypeError("'spec' argument is not type of SchemaPath")
218219
if cls is None:
219220
classes = get_classes(spec)
220221
cls = classes.webhook_response_unmarshaller_cls
@@ -233,7 +234,7 @@ def unmarshal_webhook_response(
233234
def unmarshal_response(
234235
request: AnyRequest,
235236
response: Response,
236-
spec: Spec,
237+
spec: SchemaPath,
237238
base_url: Optional[str] = None,
238239
cls: Optional[AnyResponseUnmarshallerType] = None,
239240
**unmarshaller_kwargs: Any,
@@ -242,8 +243,8 @@ def unmarshal_response(
242243
raise TypeError("'request' argument is not type of (Webhook)Request")
243244
if not isinstance(response, Response):
244245
raise TypeError("'response' argument is not type of Response")
245-
if not isinstance(spec, Spec):
246-
raise TypeError("'spec' argument is not type of Spec")
246+
if not isinstance(spec, SchemaPath):
247+
raise TypeError("'spec' argument is not type of SchemaPath")
247248
if isinstance(request, WebhookRequest):
248249
if cls is None or issubclass(cls, WebhookResponseUnmarshaller):
249250
return unmarshal_webhook_response(
@@ -276,15 +277,15 @@ def unmarshal_response(
276277

277278
def validate_request(
278279
request: AnyRequest,
279-
spec: Spec,
280+
spec: SchemaPath,
280281
base_url: Optional[str] = None,
281282
cls: Optional[AnyRequestValidatorType] = None,
282283
**validator_kwargs: Any,
283284
) -> Optional[RequestUnmarshalResult]:
284285
if not isinstance(request, (Request, WebhookRequest)):
285286
raise TypeError("'request' argument is not type of (Webhook)Request")
286-
if not isinstance(spec, Spec):
287-
raise TypeError("'spec' argument is not type of Spec")
287+
if not isinstance(spec, SchemaPath):
288+
raise TypeError("'spec' argument is not type of SchemaPath")
288289

289290
if isinstance(request, WebhookRequest):
290291
if cls is None or issubclass(cls, WebhookRequestValidator):
@@ -317,7 +318,7 @@ def validate_request(
317318
def validate_response(
318319
request: Union[Request, WebhookRequest, Spec],
319320
response: Union[Response, Request, WebhookRequest],
320-
spec: Union[Spec, Response],
321+
spec: Union[SchemaPath, Response],
321322
base_url: Optional[str] = None,
322323
cls: Optional[AnyResponseValidatorType] = None,
323324
**validator_kwargs: Any,
@@ -326,8 +327,8 @@ def validate_response(
326327
raise TypeError("'request' argument is not type of (Webhook)Request")
327328
if not isinstance(response, Response):
328329
raise TypeError("'response' argument is not type of Response")
329-
if not isinstance(spec, Spec):
330-
raise TypeError("'spec' argument is not type of Spec")
330+
if not isinstance(spec, SchemaPath):
331+
raise TypeError("'spec' argument is not type of SchemaPath")
331332

332333
if isinstance(request, WebhookRequest):
333334
if cls is None or issubclass(cls, WebhookResponseValidator):
@@ -361,15 +362,15 @@ def validate_response(
361362

362363
def validate_apicall_request(
363364
request: Request,
364-
spec: Spec,
365+
spec: SchemaPath,
365366
base_url: Optional[str] = None,
366367
cls: Optional[RequestValidatorType] = None,
367368
**validator_kwargs: Any,
368369
) -> None:
369370
if not isinstance(request, Request):
370371
raise TypeError("'request' argument is not type of Request")
371-
if not isinstance(spec, Spec):
372-
raise TypeError("'spec' argument is not type of Spec")
372+
if not isinstance(spec, SchemaPath):
373+
raise TypeError("'spec' argument is not type of SchemaPath")
373374
if cls is None:
374375
classes = get_classes(spec)
375376
cls = classes.request_validator_cls
@@ -381,15 +382,15 @@ def validate_apicall_request(
381382

382383
def validate_webhook_request(
383384
request: WebhookRequest,
384-
spec: Spec,
385+
spec: SchemaPath,
385386
base_url: Optional[str] = None,
386387
cls: Optional[WebhookRequestValidatorType] = None,
387388
**validator_kwargs: Any,
388389
) -> None:
389390
if not isinstance(request, WebhookRequest):
390391
raise TypeError("'request' argument is not type of WebhookRequest")
391-
if not isinstance(spec, Spec):
392-
raise TypeError("'spec' argument is not type of Spec")
392+
if not isinstance(spec, SchemaPath):
393+
raise TypeError("'spec' argument is not type of SchemaPath")
393394
if cls is None:
394395
classes = get_classes(spec)
395396
cls = classes.webhook_request_validator_cls
@@ -406,7 +407,7 @@ def validate_webhook_request(
406407
def validate_apicall_response(
407408
request: Request,
408409
response: Response,
409-
spec: Spec,
410+
spec: SchemaPath,
410411
base_url: Optional[str] = None,
411412
cls: Optional[ResponseValidatorType] = None,
412413
**validator_kwargs: Any,
@@ -415,8 +416,8 @@ def validate_apicall_response(
415416
raise TypeError("'request' argument is not type of Request")
416417
if not isinstance(response, Response):
417418
raise TypeError("'response' argument is not type of Response")
418-
if not isinstance(spec, Spec):
419-
raise TypeError("'spec' argument is not type of Spec")
419+
if not isinstance(spec, SchemaPath):
420+
raise TypeError("'spec' argument is not type of SchemaPath")
420421
if cls is None:
421422
classes = get_classes(spec)
422423
cls = classes.response_validator_cls
@@ -429,7 +430,7 @@ def validate_apicall_response(
429430
def validate_webhook_response(
430431
request: WebhookRequest,
431432
response: Response,
432-
spec: Spec,
433+
spec: SchemaPath,
433434
base_url: Optional[str] = None,
434435
cls: Optional[WebhookResponseValidatorType] = None,
435436
**validator_kwargs: Any,
@@ -438,8 +439,8 @@ def validate_webhook_response(
438439
raise TypeError("'request' argument is not type of WebhookRequest")
439440
if not isinstance(response, Response):
440441
raise TypeError("'response' argument is not type of Response")
441-
if not isinstance(spec, Spec):
442-
raise TypeError("'spec' argument is not type of Spec")
442+
if not isinstance(spec, SchemaPath):
443+
raise TypeError("'spec' argument is not type of SchemaPath")
443444
if cls is None:
444445
classes = get_classes(spec)
445446
cls = classes.webhook_response_validator_cls

Diff for: openapi_core/spec/paths.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import warnings
12
from typing import Any
23
from typing import Hashable
34
from typing import Mapping
45
from typing import Type
56
from typing import TypeVar
67

7-
from jsonschema_spec import SchemaPath
8+
from jsonschema_path import SchemaPath
89
from openapi_spec_validator.validation import openapi_spec_validator_proxy
910

1011
TSpec = TypeVar("TSpec", bound="Spec")
@@ -20,21 +21,14 @@ def from_dict(
2021
*args: Any,
2122
**kwargs: Any,
2223
) -> TSpec:
24+
warnings.warn(
25+
"Spec is deprecated. Use SchemaPath from jsonschema-path package.",
26+
DeprecationWarning,
27+
)
2328
validator = kwargs.pop("validator", openapi_spec_validator_proxy)
2429
if validator is not None:
2530
base_uri = kwargs.get("base_uri", "")
2631
spec_url = kwargs.get("spec_url")
2732
validator.validate(data, base_uri=base_uri, spec_url=spec_url)
2833

2934
return super().from_dict(data, *args, **kwargs)
30-
31-
def exists(self) -> bool:
32-
try:
33-
self.content()
34-
except KeyError:
35-
return False
36-
else:
37-
return True
38-
39-
def uri(self) -> str:
40-
return f"#/{str(self)}"

Diff for: openapi_core/templating/paths/finders.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def __init__(self, spec: Spec, base_url: Optional[str] = None):
7676
def _get_paths_iter(self, name: str) -> Iterator[Path]:
7777
paths = self.spec / "paths"
7878
if not paths.exists():
79-
raise PathsNotFound(paths.uri())
79+
raise PathsNotFound(paths.as_uri())
8080
template_paths: List[Path] = []
8181
for path_pattern, path in list(paths.items()):
8282
# simple path.
@@ -145,7 +145,7 @@ class WebhookPathFinder(BasePathFinder):
145145
def _get_paths_iter(self, name: str) -> Iterator[Path]:
146146
webhooks = self.spec / "webhooks"
147147
if not webhooks.exists():
148-
raise PathsNotFound(webhooks.uri())
148+
raise PathsNotFound(webhooks.as_uri())
149149
for webhook_name, path in list(webhooks.items()):
150150
if name == webhook_name:
151151
path_result = TemplateResult(webhook_name, {})

0 commit comments

Comments
 (0)