hypothesis-openapi 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ from ._strategies import openapis
@@ -0,0 +1,20 @@
1
+ from dataclasses import is_dataclass, fields
2
+
3
+ from ._types import Missing
4
+
5
+
6
+ def asdict(obj: object) -> object:
7
+ if is_dataclass(obj):
8
+ result = {}
9
+ for f in fields(obj):
10
+ value = getattr(obj, f.name)
11
+ if not isinstance(value, Missing):
12
+ result[f.name] = asdict(value)
13
+ if hasattr(obj, "map_value"):
14
+ result = obj.map_value(result)
15
+ return result
16
+ elif isinstance(obj, (list, tuple)):
17
+ return type(obj)(asdict(v) for v in obj)
18
+ elif isinstance(obj, dict):
19
+ return type(obj)((asdict(k), asdict(v)) for k, v in obj.items())
20
+ return obj
@@ -0,0 +1,10 @@
1
+ from typing import Any, Literal
2
+ from hypothesis import strategies as st
3
+ from ._v2 import Swagger
4
+ from ._serialize import asdict
5
+
6
+
7
+ def openapis(version: Literal["2.0"] | Literal["3.0"] | Literal["3.1"]) -> "st.SearchStrategy[dict[str, Any]]":
8
+ if version == "2.0":
9
+ return st.from_type(Swagger).map(asdict) # type: ignore[arg-type]
10
+ raise ValueError(f"Unsupported version: {version}")
@@ -0,0 +1,31 @@
1
+ from typing import Annotated
2
+
3
+ from hypothesis import strategies as st
4
+
5
+
6
+ class Pattern:
7
+ def __class_getitem__(cls, pattern: str) -> object:
8
+ strategy = st.from_regex(pattern)
9
+ return Annotated[str, strategy]
10
+
11
+
12
+ class UniqueList:
13
+ def __class_getitem__(cls, inner: type) -> object:
14
+ strategy = st.lists(st.from_type(inner), unique=True)
15
+ return Annotated[list, strategy]
16
+
17
+
18
+ class CombinedDict:
19
+ def __class_getitem__(cls, args: tuple[type, type, dict]) -> object:
20
+ keys, values, defaults = args
21
+
22
+ def update(data: dict) -> dict:
23
+ data.update(defaults)
24
+ return data
25
+
26
+ strategy = st.dictionaries(st.from_type(keys), st.from_type(values)).map(update)
27
+ return Annotated[dict, strategy]
28
+
29
+
30
+ class Missing:
31
+ pass
@@ -0,0 +1,203 @@
1
+ # ruff: noqa: F722, F821
2
+ from __future__ import annotations
3
+
4
+ from dataclasses import dataclass, field
5
+ from typing import Any, Literal, TypeAlias
6
+
7
+ from ._types import CombinedDict, Missing, Pattern, UniqueList
8
+
9
+ RESPONSE_SUCCESS = {"description": "OK"}
10
+ PATH_ITEM_SAMPLE = {"get": {"responses": {"default": RESPONSE_SUCCESS}}}
11
+ PARAMETER_SAMPLE = {"name": "sample", "in": "query", "type": "string"}
12
+
13
+
14
+ @dataclass
15
+ class PathItemReference:
16
+ def map_value(self, value: dict[str, Any]) -> dict[str, Any]:
17
+ value["$ref"] = "#/x-paths/Entry"
18
+ return value
19
+
20
+
21
+ @dataclass
22
+ class ParameterReference:
23
+ def map_value(self, value: dict[str, Any]) -> dict[str, Any]:
24
+ value["$ref"] = "#/parameters/SampleParameter"
25
+ return value
26
+
27
+
28
+ @dataclass
29
+ class ResponseReference:
30
+ def map_value(self, value: dict[str, Any]) -> dict[str, Any]:
31
+ value["$ref"] = "#/responses/Success"
32
+ return value
33
+
34
+
35
+ MediaType = (
36
+ Literal["application/json"]
37
+ | Literal["application/xml"]
38
+ | Literal["text/plain"]
39
+ | Literal["text/yaml"]
40
+ | Literal["application/x-www-form-urlencoded"]
41
+ | Literal["multipart/form-data"]
42
+ | Literal["application/octet-stream"]
43
+ )
44
+
45
+
46
+ @dataclass
47
+ class Swagger:
48
+ swagger: Literal["2.0"]
49
+ info: Info
50
+ host: Pattern["^[^{}/ :\\\\]+(?::\\d+)?$"] | Missing # type: ignore[type-arg,valid-type]
51
+ basePath: Pattern["^/"] | Missing # type: ignore[type-arg,valid-type]
52
+ paths: dict[Pattern["^/"], PathItem | PathItemReference] # type: ignore[type-arg,valid-type]
53
+ consumes: MediaTypeList | Missing
54
+ produces: MediaTypeList | Missing
55
+ parameters: CombinedDict[str, Parameter, {"SampleParameter": PARAMETER_SAMPLE}] # type: ignore[type-arg,valid-type]
56
+ responses: CombinedDict[ResponseId, Response, {"Success": RESPONSE_SUCCESS}] # type: ignore[type-arg,valid-type]
57
+ definitions: Definitions
58
+
59
+ def map_value(self, value: dict[str, Any]) -> dict[str, Any]:
60
+ value["x-paths"] = {"Entry": PATH_ITEM_SAMPLE}
61
+ return value
62
+
63
+
64
+ @dataclass
65
+ class Definitions:
66
+ pass
67
+
68
+
69
+ @dataclass
70
+ class Info:
71
+ version: Literal["1.0.0"]
72
+ title: Literal["Example API"]
73
+
74
+
75
+ @dataclass
76
+ class PathItem:
77
+ get: Operation | Missing
78
+ put: Operation | Missing
79
+ post: Operation | Missing
80
+ delete: Operation | Missing
81
+ options: Operation | Missing
82
+ head: Operation | Missing
83
+ patch: Operation | Missing
84
+ parameters: ParameterList | Missing
85
+
86
+ def map_value(self, value: dict[str, Any]) -> dict[str, Any]:
87
+ if "parameters" in value:
88
+ value["parameters"] = deduplicate_parameters(value["parameters"])
89
+ return value
90
+
91
+
92
+ @dataclass
93
+ class Operation:
94
+ operationId: str | Missing
95
+ parameters: ParameterList | Missing
96
+ tags: UniqueList[str] | Missing # type: ignore[type-arg,valid-type]
97
+ consumes: MediaTypeList | Missing
98
+ produces: MediaTypeList | Missing
99
+ responses: dict[ResponseId, Response]
100
+
101
+ def map_value(self, value: dict[str, Any]) -> dict[str, Any]:
102
+ if not value["responses"]:
103
+ value["responses"] = {"default": RESPONSE_SUCCESS}
104
+ if "parameters" in value:
105
+ value["parameters"] = deduplicate_parameters(value["parameters"])
106
+ return value
107
+
108
+
109
+ def deduplicate_parameters(parameters: list[dict[str, Any]]) -> list[dict[str, Any]]:
110
+ # TODO: modify in-place
111
+ parameter_names = set()
112
+ deduplicated = []
113
+ has_reference = False
114
+ for parameter in parameters:
115
+ if "$ref" in parameter:
116
+ if has_reference:
117
+ continue
118
+ has_reference = True
119
+ deduplicated.append(parameter)
120
+ else:
121
+ name = parameter["name"]
122
+ if name in parameter_names:
123
+ continue
124
+ parameter_names.add(name)
125
+ deduplicated.append(parameter)
126
+ return deduplicated
127
+
128
+
129
+ @dataclass
130
+ class Response:
131
+ description: Literal["Ok"]
132
+
133
+
134
+ @dataclass
135
+ class BodyParameter:
136
+ name: str
137
+ schema: dict = field(default_factory=dict)
138
+
139
+ def map_value(self, value: dict[str, Any]) -> dict[str, Any]:
140
+ value["in"] = "body"
141
+ return value
142
+
143
+ def __hash__(self) -> int:
144
+ return hash(self.name)
145
+
146
+
147
+ @dataclass
148
+ class QueryParameter:
149
+ name: str
150
+ type: Literal["string", "number", "boolean", "integer", "array"]
151
+
152
+ def map_value(self, value: dict[str, Any]) -> dict[str, Any]:
153
+ value["in"] = "query"
154
+ return value
155
+
156
+ def __hash__(self) -> int:
157
+ return hash(self.name)
158
+
159
+
160
+ @dataclass
161
+ class HeaderParameter:
162
+ name: str
163
+ type: Literal["string", "number", "boolean", "integer", "array"]
164
+
165
+ def map_value(self, value: dict[str, Any]) -> dict[str, Any]:
166
+ value["in"] = "header"
167
+ return value
168
+
169
+ def __hash__(self) -> int:
170
+ return hash(self.name)
171
+
172
+
173
+ @dataclass
174
+ class PathParameter:
175
+ name: str
176
+ type: Literal["string", "number", "boolean", "integer", "array"]
177
+
178
+ def map_value(self, value: dict[str, Any]) -> dict[str, Any]:
179
+ value["in"] = "path"
180
+ value["required"] = True
181
+ return value
182
+
183
+ def __hash__(self) -> int:
184
+ return hash(self.name)
185
+
186
+
187
+ @dataclass
188
+ class FormDataParameter:
189
+ name: str
190
+ type: Literal["string", "number", "boolean", "integer", "array", "file"]
191
+
192
+ def map_value(self, value: dict[str, Any]) -> dict[str, Any]:
193
+ value["in"] = "formData"
194
+ return value
195
+
196
+ def __hash__(self) -> int:
197
+ return hash(self.name)
198
+
199
+
200
+ Parameter = BodyParameter | QueryParameter | HeaderParameter | PathParameter | FormDataParameter
201
+ ParameterList = list[Parameter | ParameterReference]
202
+ MediaTypeList: TypeAlias = UniqueList[MediaType] # type: ignore[type-arg]
203
+ ResponseId: TypeAlias = Pattern["^([0-9]{3})$|^(default)$"] # type: ignore[type-arg,valid-type]
File without changes
@@ -0,0 +1,71 @@
1
+ Metadata-Version: 2.3
2
+ Name: hypothesis-openapi
3
+ Version: 0.1.0
4
+ Summary: Hypothesis plugin for generating valid Open API documents
5
+ Project-URL: Changelog, https://github.com/Stranger6667/hypothesis-openapi/blob/main/CHANGELOG.md
6
+ Project-URL: Bug Tracker, https://github.com/Stranger6667/hypothesis-openapi
7
+ Project-URL: Funding, https://github.com/sponsors/Stranger6667
8
+ Project-URL: Source Code, https://github.com/Stranger6667/hypothesis-openapi
9
+ Author-email: Dmitry Dygalo <dmitry@dygalo.dev>
10
+ Maintainer-email: Dmitry Dygalo <dmitry@dygalo.dev>
11
+ License-Expression: MIT
12
+ License-File: LICENSE
13
+ Keywords: hypothesis,openapi,testing
14
+ Classifier: Development Status :: 3 - Alpha
15
+ Classifier: Environment :: Console
16
+ Classifier: Framework :: Hypothesis
17
+ Classifier: Intended Audience :: Developers
18
+ Classifier: License :: OSI Approved :: MIT License
19
+ Classifier: Operating System :: OS Independent
20
+ Classifier: Programming Language :: Python :: 3 :: Only
21
+ Classifier: Programming Language :: Python :: 3.10
22
+ Classifier: Programming Language :: Python :: 3.11
23
+ Classifier: Programming Language :: Python :: 3.12
24
+ Classifier: Programming Language :: Python :: Implementation :: CPython
25
+ Classifier: Topic :: Software Development :: Testing
26
+ Requires-Python: >=3.10
27
+ Requires-Dist: hypothesis<7.0,>=6.84.3
28
+ Provides-Extra: cov
29
+ Requires-Dist: coverage-enable-subprocess; extra == 'cov'
30
+ Requires-Dist: coverage[toml]>=7; extra == 'cov'
31
+ Provides-Extra: dev
32
+ Requires-Dist: coverage-enable-subprocess; extra == 'dev'
33
+ Requires-Dist: coverage>=7; extra == 'dev'
34
+ Requires-Dist: coverage[toml]>=7; extra == 'dev'
35
+ Requires-Dist: jsonschema>=4.21.0; extra == 'dev'
36
+ Requires-Dist: pytest-xdist<3.0,>=2.5; extra == 'dev'
37
+ Requires-Dist: pytest<8,>=6.2.0; extra == 'dev'
38
+ Provides-Extra: tests
39
+ Requires-Dist: coverage>=7; extra == 'tests'
40
+ Requires-Dist: jsonschema>=4.21.0; extra == 'tests'
41
+ Requires-Dist: pytest-xdist<3.0,>=2.5; extra == 'tests'
42
+ Requires-Dist: pytest<8,>=6.2.0; extra == 'tests'
43
+ Description-Content-Type: text/markdown
44
+
45
+ # hypothesis-openapi
46
+
47
+ [![Build](https://github.com/Stranger6667/hypothesis-openapi/workflows/build/badge.svg)](https://github.com/Stranger6667/hypothesis-main/actions)
48
+ [![Coverage](https://codecov.io/gh/Stranger6667/hypothesis-openapi/branch/main/graph/badge.svg)](https://codecov.io/gh/Stranger6667/hypothesis-openapi/branch/main)
49
+ [![Version](https://img.shields.io/pypi/v/hypothesis-openapi.svg)](https://pypi.org/project/hypothesis-openapi/)
50
+ [![Python versions](https://img.shields.io/pypi/pyversions/hypothesis-openapi.svg)](https://pypi.org/project/hypothesis-openapi/)
51
+ [![License](https://img.shields.io/pypi/l/hypothesis-openapi.svg)](https://opensource.org/licenses/MIT)
52
+
53
+ Hypothesis plugin for generating valid Open API documents.
54
+
55
+ ## Usage
56
+
57
+ ```python
58
+ from hypothesis import given
59
+ from hypothesis_openapi import openapis
60
+
61
+
62
+ @given(openapis(version="2.0"))
63
+ def test_openapi(spec):
64
+ assert spec["swagger"] == "2.0"
65
+ assert "info" in spec
66
+ ```
67
+
68
+ ## License
69
+
70
+ The code in this project is licensed under [MIT license](https://opensource.org/licenses/MIT).
71
+ By contributing to `hypothesis-openapi`, you agree that your contributions will be licensed under its MIT license.
@@ -0,0 +1,10 @@
1
+ hypothesis_openapi/__init__.py,sha256=bwpysqV2BDfKo0vXMp7jvHjeZdRhLgvWIIR-6J-MS-E,34
2
+ hypothesis_openapi/_serialize.py,sha256=n-ikmQRGX5jNfWn-jxJFx1hK99vCp4LgFWTbkJDs90M,635
3
+ hypothesis_openapi/_strategies.py,sha256=x4914vVljP6CDrxe8DTCHdzbBPcj2nuCSZvLsgvFMiY,398
4
+ hypothesis_openapi/_types.py,sha256=PCFAol8xkO08SxW_L41tG-5u1BcmnzPQaC5_zzSDGyU,792
5
+ hypothesis_openapi/_v2.py,sha256=lMJwCc4HmlC_A8qrmxhRmGgXzrfKXAjRnSmQmz7HzYU,5721
6
+ hypothesis_openapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
+ hypothesis_openapi-0.1.0.dist-info/METADATA,sha256=_9qzPRA5iYAOZ-S0hEsrn8J_2gmWMqgMgpxyWVCtPG8,3172
8
+ hypothesis_openapi-0.1.0.dist-info/WHEEL,sha256=zEMcRr9Kr03x1ozGwg5v9NQBKn3kndp6LSoSlVg-jhU,87
9
+ hypothesis_openapi-0.1.0.dist-info/licenses/LICENSE,sha256=EelDuCukMQzrTz9wwY6aMzr7m7qr4xwkrpJ3TwcDtdo,1070
10
+ hypothesis_openapi-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.24.2
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Dmitry Dygalo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.