-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
schemas.py
556 lines (472 loc) · 23.8 KB
/
schemas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# pylint: disable=too-many-instance-attributes
"""Schema objects provide a convenient interface to raw schemas.
Their responsibilities:
- Provide a unified way to work with different types of schemas
- Give all endpoints / methods combinations that are available directly from the schema;
They give only static definitions of endpoints.
"""
import itertools
from collections.abc import Mapping
from copy import deepcopy
from functools import lru_cache
from typing import Any, Callable, Dict, Generator, Iterator, List, Optional, Tuple, Union, overload
from urllib.parse import urljoin, urlsplit
from urllib.request import urlopen
import attr
import hypothesis
import jsonschema
import yaml
from requests.structures import CaseInsensitiveDict
from ._hypothesis import make_test_or_exception
from .converter import to_json_schema, to_json_schema_recursive
from .exceptions import InvalidSchema
from .filters import should_skip_by_operation_id, should_skip_by_tag, should_skip_endpoint, should_skip_method
from .hooks import HookContext, HookDispatcher, HookLocation, HookScope, dispatch, warn_deprecated_hook
from .models import Endpoint, EndpointDefinition, empty_object
from .specs.openapi.security import OpenAPISecurityProcessor, SwaggerSecurityProcessor
from .types import Filter, GenericTest, Hook, NotSet
from .utils import NOT_SET, GenericResponse, StringDatesYAMLLoader, deprecated, traverse_schema
# Reference resolving will stop after this depth
RECURSION_DEPTH_LIMIT = 100
def load_file_impl(location: str, opener: Callable) -> Dict[str, Any]:
"""Load a schema from the given file."""
with opener(location) as fd:
return yaml.load(fd, StringDatesYAMLLoader)
@lru_cache()
def load_file(location: str) -> Dict[str, Any]:
"""Load a schema from the given file."""
return load_file_impl(location, open)
@lru_cache()
def load_file_uri(location: str) -> Dict[str, Any]:
"""Load a schema from the given file uri."""
return load_file_impl(location, urlopen)
class ConvertingResolver(jsonschema.RefResolver):
"""A custom resolver converts resolved OpenAPI schemas to JSON Schema.
When recursive schemas are validated we need to have resolved documents properly converted.
This approach is the simplest one, since this logic isolated in a single place.
"""
def __init__(self, *args: Any, conversion_kwargs: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self.conversion_kwargs = conversion_kwargs
def resolve(self, ref: str) -> Tuple[str, Any]:
url, document = super().resolve(ref)
document = traverse_schema(document, to_json_schema, **self.conversion_kwargs)
return url, document
@attr.s() # pragma: no mutate
class BaseSchema(Mapping):
nullable_name: str
raw_schema: Dict[str, Any] = attr.ib() # pragma: no mutate
location: Optional[str] = attr.ib(default=None) # pragma: no mutate
base_url: Optional[str] = attr.ib(default=None) # pragma: no mutate
method: Optional[Filter] = attr.ib(default=None) # pragma: no mutate
endpoint: Optional[Filter] = attr.ib(default=None) # pragma: no mutate
tag: Optional[Filter] = attr.ib(default=None) # pragma: no mutate
operation_id: Optional[Filter] = attr.ib(default=None) # pragma: no mutate
app: Any = attr.ib(default=None) # pragma: no mutate
hooks: HookDispatcher = attr.ib(factory=lambda: HookDispatcher(scope=HookScope.SCHEMA)) # pragma: no mutate
test_function: Optional[GenericTest] = attr.ib(default=None) # pragma: no mutate
validate_schema: bool = attr.ib(default=True) # pragma: no mutate
def __iter__(self) -> Iterator[str]:
return iter(self.endpoints)
def __getitem__(self, item: str) -> CaseInsensitiveDict:
return self.endpoints[item]
def __len__(self) -> int:
return len(self.endpoints)
@property # pragma: no mutate
def spec_version(self) -> str:
raise NotImplementedError
@property # pragma: no mutate
def verbose_name(self) -> str:
raise NotImplementedError
@property
def endpoints(self) -> Dict[str, CaseInsensitiveDict]:
if not hasattr(self, "_endpoints"):
# pylint: disable=attribute-defined-outside-init
endpoints = self.get_all_endpoints()
self._endpoints = endpoints_to_dict(endpoints)
return self._endpoints
@property
def resolver(self) -> jsonschema.RefResolver:
if not hasattr(self, "_resolver"):
# pylint: disable=attribute-defined-outside-init
self._resolver = ConvertingResolver(
self.location or "",
self.raw_schema,
conversion_kwargs={"nullable_name": self.nullable_name},
handlers={"file": load_file_uri, "": load_file},
)
return self._resolver
@property
def endpoints_count(self) -> int:
return len(list(self.get_all_endpoints()))
def get_all_endpoints(self) -> Generator[Endpoint, None, None]:
raise NotImplementedError
def get_all_tests(
self, func: Callable, settings: Optional[hypothesis.settings] = None, seed: Optional[int] = None
) -> Generator[Tuple[Endpoint, Union[Callable, InvalidSchema]], None, None]:
"""Generate all endpoints and Hypothesis tests for them."""
test: Union[Callable, InvalidSchema]
for endpoint in self.get_all_endpoints():
test = make_test_or_exception(endpoint, func, settings, seed)
yield endpoint, test
def parametrize( # pylint: disable=too-many-arguments
self,
method: Optional[Filter] = NOT_SET,
endpoint: Optional[Filter] = NOT_SET,
tag: Optional[Filter] = NOT_SET,
operation_id: Optional[Filter] = NOT_SET,
validate_schema: Union[bool, NotSet] = NOT_SET,
) -> Callable:
"""Mark a test function as a parametrized one."""
def wrapper(func: GenericTest) -> GenericTest:
HookDispatcher.add_dispatcher(func)
func._schemathesis_test = self.clone(func, method, endpoint, tag, operation_id, validate_schema) # type: ignore
return func
return wrapper
def clone( # pylint: disable=too-many-arguments
self,
test_function: Optional[GenericTest] = None,
method: Optional[Filter] = NOT_SET,
endpoint: Optional[Filter] = NOT_SET,
tag: Optional[Filter] = NOT_SET,
operation_id: Optional[Filter] = NOT_SET,
validate_schema: Union[bool, NotSet] = NOT_SET,
) -> "BaseSchema":
if method is NOT_SET:
method = self.method
if endpoint is NOT_SET:
endpoint = self.endpoint
if tag is NOT_SET:
tag = self.tag
if operation_id is NOT_SET:
operation_id = self.operation_id
if validate_schema is NOT_SET:
validate_schema = self.validate_schema
return self.__class__(
self.raw_schema,
location=self.location,
base_url=self.base_url,
method=method,
endpoint=endpoint,
tag=tag,
operation_id=operation_id,
app=self.app,
hooks=self.hooks,
test_function=test_function,
validate_schema=validate_schema, # type: ignore
)
def _get_response_schema(
self, definition: Dict[str, Any], scope: str
) -> Tuple[List[str], Optional[Dict[str, Any]]]:
"""Extract response schema from `responses`."""
raise NotImplementedError
@deprecated("'register_hook` is deprecated, use `hooks.register' instead")
def register_hook(self, place: str, hook: Hook) -> None:
warn_deprecated_hook(hook)
if place not in HookLocation.__members__:
raise KeyError(place)
self.hooks.register_hook_with_name(f"before_generate_{place}", hook, skip_validation=True)
@deprecated("'with_hook` is deprecated, use `hooks.apply' instead")
def with_hook(self, place: str, hook: Hook) -> Callable[[GenericTest], GenericTest]:
"""Register a hook for a specific test."""
warn_deprecated_hook(hook)
if place not in HookLocation.__members__:
raise KeyError(place)
return self.hooks.apply(f"before_generate_{place}", hook, skip_validation=True)
def get_local_hook_dispatcher(self) -> Optional[HookDispatcher]:
"""Get a HookDispatcher instance bound to the test if present."""
# It might be not present when it is used without pytest via `Endpoint.as_strategy()`
if self.test_function is not None:
return self.test_function._schemathesis_hooks # type: ignore
return None
def dispatch_hook(self, name: str, context: HookContext, *args: Any, **kwargs: Any) -> None:
"""Dispatch a hook via all available dispatchers."""
dispatch(name, context, *args, **kwargs)
self.hooks.dispatch(name, context, *args, **kwargs)
local_dispatcher = self.get_local_hook_dispatcher()
if local_dispatcher is not None:
local_dispatcher.dispatch(name, context, *args, **kwargs)
def get_content_types(self, endpoint: Endpoint, response: GenericResponse) -> List[str]:
"""Content types available for this endpoint."""
raise NotImplementedError
class SwaggerV20(BaseSchema): # pylint: disable=too-many-public-methods
nullable_name = "x-nullable"
example_field = "x-example"
operations: Tuple[str, ...] = ("get", "put", "post", "delete", "options", "head", "patch")
security = SwaggerSecurityProcessor()
def __repr__(self) -> str:
info = self.raw_schema["info"]
return f"{self.__class__.__name__} for {info['title']} ({info['version']})"
@property
def spec_version(self) -> str:
return self.raw_schema["swagger"]
@property
def verbose_name(self) -> str:
return f"Swagger {self.spec_version}"
@property
def base_path(self) -> str:
"""Base path for the schema."""
path: str = self.raw_schema.get("basePath", "/") # pragma: no mutate
if not path.endswith("/"):
path += "/"
return path
def get_full_path(self, path: str) -> str:
"""Compute full path for the given path."""
return urljoin(self.base_path, path.lstrip("/")) # pragma: no mutate
def get_all_endpoints(self) -> Generator[Endpoint, None, None]:
try:
paths = self.raw_schema["paths"] # pylint: disable=unsubscriptable-object
context = HookContext()
for path, methods in paths.items():
full_path = self.get_full_path(path)
if should_skip_endpoint(full_path, self.endpoint):
continue
self.dispatch_hook("before_process_path", context, path, methods)
# We need to know a proper scope in what methods are.
# It will allow us to provide a proper reference resolving in `response_schema_conformance` and avoid
# recursion errors
if "$ref" in methods:
scope, raw_methods = deepcopy(self.resolver.resolve(methods["$ref"]))
else:
raw_methods, scope = deepcopy(methods), self.resolver.resolution_scope
methods = self.resolve(methods)
common_parameters = get_common_parameters(methods)
for method, resolved_definition in methods.items():
# Only method definitions are parsed
if (
method not in self.operations
or should_skip_method(method, self.method)
or should_skip_by_tag(resolved_definition.get("tags"), self.tag)
or should_skip_by_operation_id(resolved_definition.get("operationId"), self.operation_id)
):
continue
parameters = itertools.chain(resolved_definition.get("parameters", ()), common_parameters)
# To prevent recursion errors we need to pass not resolved schema as well
# It could be used for response validation
raw_definition = EndpointDefinition(raw_methods[method], scope)
yield self.make_endpoint(full_path, method, parameters, resolved_definition, raw_definition)
except (KeyError, AttributeError, jsonschema.exceptions.RefResolutionError):
raise InvalidSchema("Schema parsing failed. Please check your schema.")
def make_endpoint( # pylint: disable=too-many-arguments
self,
full_path: str,
method: str,
parameters: Iterator[Dict[str, Any]],
resolved_definition: Dict[str, Any],
raw_definition: EndpointDefinition,
) -> Endpoint:
"""Create JSON schemas for query, body, etc from Swagger parameters definitions."""
base_url = self.base_url
if base_url is not None:
base_url = base_url.rstrip("/") # pragma: no mutate
endpoint = Endpoint(
path=full_path,
method=method.upper(),
definition=raw_definition,
base_url=base_url,
app=self.app,
schema=self,
)
for parameter in parameters:
self.process_parameter(endpoint, parameter)
self.security.process_definitions(self.raw_schema, endpoint, self.resolver)
return endpoint
def process_parameter(self, endpoint: Endpoint, parameter: Dict[str, Any]) -> None:
"""Convert each Parameter object to a JSON schema."""
parameter = deepcopy(parameter)
parameter = self.resolve(parameter)
self.process_by_type(endpoint, parameter)
def process_by_type(self, endpoint: Endpoint, parameter: Dict[str, Any]) -> None:
if parameter["in"] == "path":
self.process_path(endpoint, parameter)
elif parameter["in"] == "query":
self.process_query(endpoint, parameter)
elif parameter["in"] == "header":
self.process_header(endpoint, parameter)
elif parameter["in"] == "body":
# Could be only one parameter with "in=body"
self.process_body(endpoint, parameter)
elif parameter["in"] == "formData":
self.process_form_data(endpoint, parameter)
def process_path(self, endpoint: Endpoint, parameter: Dict[str, Any]) -> None:
endpoint.path_parameters = self.add_parameter(endpoint.path_parameters, parameter)
def process_header(self, endpoint: Endpoint, parameter: Dict[str, Any]) -> None:
endpoint.headers = self.add_parameter(endpoint.headers, parameter)
def process_query(self, endpoint: Endpoint, parameter: Dict[str, Any]) -> None:
endpoint.query = self.add_parameter(endpoint.query, parameter)
def process_body(self, endpoint: Endpoint, parameter: Dict[str, Any]) -> None:
# "schema" is a required field
endpoint.body = parameter["schema"]
def process_form_data(self, endpoint: Endpoint, parameter: Dict[str, Any]) -> None:
endpoint.form_data = self.add_parameter(endpoint.form_data, parameter)
def add_parameter(self, container: Optional[Dict[str, Any]], parameter: Dict[str, Any]) -> Dict[str, Any]:
"""Add parameter object to the container."""
name = parameter["name"]
container = container or empty_object()
container["properties"][name] = self.parameter_to_json_schema(parameter)
if parameter.get("required", False):
container["required"].append(name)
return self.add_examples(container, parameter)
def add_examples(self, container: Dict[str, Any], parameter: Dict[str, Any]) -> Dict[str, Any]:
if self.example_field in parameter:
examples = container.setdefault("example", {}) # examples should be merged together
examples[parameter["name"]] = parameter[self.example_field]
return container
def parameter_to_json_schema(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Convert Parameter object to a JSON schema."""
return {
key: value
for key, value in data.items()
# Do not include keys not supported by JSON schema
if not (key == "required" and not isinstance(value, list))
}
@overload # pragma: no mutate
def resolve(
self, item: Dict[str, Any], recursion_level: int = 0
) -> Dict[str, Any]: # pylint: disable=function-redefined
pass
@overload # pragma: no mutate
def resolve(self, item: List, recursion_level: int = 0) -> List: # pylint: disable=function-redefined
pass
# pylint: disable=function-redefined
def resolve(self, item: Union[Dict[str, Any], List], recursion_level: int = 0) -> Union[Dict[str, Any], List]:
"""Recursively resolve all references in the given object."""
if recursion_level > RECURSION_DEPTH_LIMIT:
return item
if isinstance(item, dict):
item = self.prepare(item)
if "$ref" in item:
with self.resolver.resolving(item["$ref"]) as resolved:
return self.resolve(resolved, recursion_level + 1)
for key, sub_item in item.items():
item[key] = self.resolve(sub_item, recursion_level)
elif isinstance(item, list):
for idx, sub_item in enumerate(item):
item[idx] = self.resolve(sub_item, recursion_level)
return item
def resolve_in_scope(self, definition: Dict[str, Any], scope: str) -> Tuple[List[str], Dict[str, Any]]:
scopes = [scope]
# if there is `$ref` then we have a scope change that should be used during validation later to
# resolve nested references correctly
if "$ref" in definition:
with self.resolver.in_scope(scope):
new_scope, definition = deepcopy(self.resolver.resolve(definition["$ref"]))
scopes.append(new_scope)
return scopes, definition
def prepare(self, item: Dict[str, Any]) -> Dict[str, Any]:
"""Parse schema extension, e.g. "x-nullable" field."""
return to_json_schema(item, self.nullable_name)
def _get_response_schema(
self, definition: Dict[str, Any], scope: str
) -> Tuple[List[str], Optional[Dict[str, Any]]]:
scopes, definition = self.resolve_in_scope(deepcopy(definition), scope)
schema = definition.get("schema")
if not schema:
return scopes, None
# Extra conversion to JSON Schema is needed here if there was one $ref in the input
# because it is not converted
return scopes, to_json_schema_recursive(schema, self.nullable_name)
def get_content_types(self, endpoint: Endpoint, response: GenericResponse) -> List[str]:
produces = endpoint.definition.raw.get("produces", None)
if produces:
return produces
return self.raw_schema.get("produces", [])
class OpenApi30(SwaggerV20): # pylint: disable=too-many-ancestors
nullable_name = "nullable"
example_field = "example"
operations = SwaggerV20.operations + ("trace",)
security = OpenAPISecurityProcessor()
@property
def spec_version(self) -> str:
return self.raw_schema["openapi"]
@property
def verbose_name(self) -> str:
return f"Open API {self.spec_version}"
@property
def base_path(self) -> str:
"""Base path for the schema."""
servers = self.raw_schema.get("servers", [])
if servers:
# assume we're the first server in list
server = servers[0]
url = server["url"].format(**{k: v["default"] for k, v in server.get("variables", {}).items()})
path = urlsplit(url).path
else:
path = "/"
if not path.endswith("/"):
path += "/"
return path
def make_endpoint( # pylint: disable=too-many-arguments
self,
full_path: str,
method: str,
parameters: Iterator[Dict[str, Any]],
resolved_definition: Dict[str, Any],
raw_definition: EndpointDefinition,
) -> Endpoint:
"""Create JSON schemas for query, body, etc from Swagger parameters definitions."""
endpoint = super().make_endpoint(full_path, method, parameters, resolved_definition, raw_definition)
if "requestBody" in resolved_definition:
self.process_body(endpoint, resolved_definition["requestBody"])
return endpoint
def process_by_type(self, endpoint: Endpoint, parameter: Dict[str, Any]) -> None:
if parameter["in"] == "cookie":
self.process_cookie(endpoint, parameter)
else:
super().process_by_type(endpoint, parameter)
def add_examples(self, container: Dict[str, Any], parameter: Dict[str, Any]) -> Dict[str, Any]:
if self.example_field in parameter["schema"]:
examples = container.setdefault("example", {}) # examples should be merged together
examples[parameter["name"]] = parameter["schema"][self.example_field]
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#parameter-object
# > Furthermore, if referencing a schema which contains an example,
# > the example value SHALL override the example provided by the schema
return super().add_examples(container, parameter)
def process_cookie(self, endpoint: Endpoint, parameter: Dict[str, Any]) -> None:
endpoint.cookies = self.add_parameter(endpoint.cookies, parameter)
def process_body(self, endpoint: Endpoint, parameter: Dict[str, Any]) -> None:
# Take the first media type object
options = iter(parameter["content"].values())
parameter = next(options)
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#media-type-object
# > Furthermore, if referencing a schema which contains an example,
# > the example value SHALL override the example provided by the schema
if "example" in parameter:
parameter["schema"]["example"] = parameter["example"]
super().process_body(endpoint, parameter)
def parameter_to_json_schema(self, data: Dict[str, Any]) -> Dict[str, Any]:
# "schema" field is required for all parameters in Open API 3.0
return super().parameter_to_json_schema(data["schema"])
def _get_response_schema(
self, definition: Dict[str, Any], scope: str
) -> Tuple[List[str], Optional[Dict[str, Any]]]:
scopes, definition = self.resolve_in_scope(deepcopy(definition), scope)
options = iter(definition.get("content", {}).values())
option = next(options, None)
if option:
# Extra conversion to JSON Schema is needed here if there was one $ref in the input
# because it is not converted
return scopes, to_json_schema_recursive(option["schema"], self.nullable_name)
return scopes, None
def get_content_types(self, endpoint: Endpoint, response: GenericResponse) -> List[str]:
try:
responses = endpoint.definition.raw["responses"]
except KeyError:
# Possible to get if `validate_schema=False` is passed during schema creation
raise InvalidSchema("Schema parsing failed. Please check your schema.")
definitions = responses.get(str(response.status_code), {}).get("content", {})
return list(definitions.keys())
def get_common_parameters(methods: Dict[str, Any]) -> List[Dict[str, Any]]:
"""Common parameters are deep copied from the methods definitions.
Copying is needed because of further modifications.
"""
common_parameters = methods.get("parameters")
if common_parameters is not None:
return deepcopy(common_parameters)
return []
def endpoints_to_dict(endpoints: Generator[Endpoint, None, None]) -> Dict[str, CaseInsensitiveDict]:
output: Dict[str, CaseInsensitiveDict] = {}
for endpoint in endpoints:
output.setdefault(endpoint.path, CaseInsensitiveDict())
output[endpoint.path][endpoint.method] = endpoint
return output