Schema-First 0.3.0__tar.gz

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.
Files changed (38) hide show
  1. schema_first-0.3.0/LICENSE +21 -0
  2. schema_first-0.3.0/PKG-INFO +130 -0
  3. schema_first-0.3.0/README.md +82 -0
  4. schema_first-0.3.0/pyproject.toml +62 -0
  5. schema_first-0.3.0/setup.cfg +4 -0
  6. schema_first-0.3.0/src/Schema_First.egg-info/PKG-INFO +130 -0
  7. schema_first-0.3.0/src/Schema_First.egg-info/SOURCES.txt +36 -0
  8. schema_first-0.3.0/src/Schema_First.egg-info/dependency_links.txt +1 -0
  9. schema_first-0.3.0/src/Schema_First.egg-info/requires.txt +12 -0
  10. schema_first-0.3.0/src/Schema_First.egg-info/top_level.txt +1 -0
  11. schema_first-0.3.0/src/schema_first/__init__.py +3 -0
  12. schema_first-0.3.0/src/schema_first/exceptions.py +2 -0
  13. schema_first-0.3.0/src/schema_first/loaders/__init__.py +3 -0
  14. schema_first-0.3.0/src/schema_first/loaders/exc.py +9 -0
  15. schema_first-0.3.0/src/schema_first/loaders/yaml_loader.py +143 -0
  16. schema_first-0.3.0/src/schema_first/openapi/__init__.py +19 -0
  17. schema_first-0.3.0/src/schema_first/openapi/exc.py +5 -0
  18. schema_first-0.3.0/src/schema_first/openapi/schemas/__init__.py +0 -0
  19. schema_first-0.3.0/src/schema_first/openapi/schemas/base.py +20 -0
  20. schema_first-0.3.0/src/schema_first/openapi/schemas/constants.py +5 -0
  21. schema_first-0.3.0/src/schema_first/openapi/schemas/fields.py +12 -0
  22. schema_first-0.3.0/src/schema_first/openapi/schemas/v3_1_1/components_object_schema.py +14 -0
  23. schema_first-0.3.0/src/schema_first/openapi/schemas/v3_1_1/contact_schema.py +9 -0
  24. schema_first-0.3.0/src/schema_first/openapi/schemas/v3_1_1/info_schema.py +21 -0
  25. schema_first-0.3.0/src/schema_first/openapi/schemas/v3_1_1/license_schema.py +19 -0
  26. schema_first-0.3.0/src/schema_first/openapi/schemas/v3_1_1/media_type_object_schema.py +8 -0
  27. schema_first-0.3.0/src/schema_first/openapi/schemas/v3_1_1/operation_object_schema.py +12 -0
  28. schema_first-0.3.0/src/schema_first/openapi/schemas/v3_1_1/path_item_object_schema.py +9 -0
  29. schema_first-0.3.0/src/schema_first/openapi/schemas/v3_1_1/reference_object_schema.py +8 -0
  30. schema_first-0.3.0/src/schema_first/openapi/schemas/v3_1_1/request_body_object_schema.py +11 -0
  31. schema_first-0.3.0/src/schema_first/openapi/schemas/v3_1_1/responses_object_schema.py +11 -0
  32. schema_first-0.3.0/src/schema_first/openapi/schemas/v3_1_1/root_schema.py +25 -0
  33. schema_first-0.3.0/src/schema_first/openapi/schemas/v3_1_1/schema_object_schema.py +17 -0
  34. schema_first-0.3.0/src/schema_first/openapi/schemas/v3_1_1/server_schema.py +19 -0
  35. schema_first-0.3.0/src/schema_first/openapi/schemas/v3_1_1/server_variable_object_schema.py +12 -0
  36. schema_first-0.3.0/src/schema_first/specification/__init__.py +47 -0
  37. schema_first-0.3.0/tests/test_specification.py +33 -0
  38. schema_first-0.3.0/tests/test_validator.py +41 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 flask-pro
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.
@@ -0,0 +1,130 @@
1
+ Metadata-Version: 2.4
2
+ Name: Schema-First
3
+ Version: 0.3.0
4
+ Summary: OpenAPI specification validator and converter to Marshmallow schemas.
5
+ Author-email: Konstantin Fadeev <fadeev@legalact.pro>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2025 flask-pro
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: changelog, https://github.com/flask-pro/schema-first/blob/master/CHANGES.md
29
+ Project-URL: repository, https://github.com/flask-pro/schema-first
30
+ Classifier: License :: OSI Approved :: MIT License
31
+ Classifier: Operating System :: OS Independent
32
+ Classifier: Programming Language :: Python :: 3
33
+ Requires-Python: >=3.14
34
+ Description-Content-Type: text/markdown
35
+ License-File: LICENSE
36
+ Requires-Dist: marshmallow>=4.0.0
37
+ Requires-Dist: PyYAML>=6.0.2
38
+ Provides-Extra: dev
39
+ Requires-Dist: bandit==1.8.6; extra == "dev"
40
+ Requires-Dist: build==1.2.1; extra == "dev"
41
+ Requires-Dist: openapi-spec-validator>=0.5.0; extra == "dev"
42
+ Requires-Dist: pre-commit==4.2.0; extra == "dev"
43
+ Requires-Dist: pytest==8.4.1; extra == "dev"
44
+ Requires-Dist: pytest-cov==6.2.1; extra == "dev"
45
+ Requires-Dist: python-dotenv==1.1.1; extra == "dev"
46
+ Requires-Dist: twine==6.1.0; extra == "dev"
47
+ Dynamic: license-file
48
+
49
+ # Schema-First
50
+
51
+ Validate and convert OpenAPI specification via Marshmallow schemas to Marshmallow schemas.
52
+
53
+ <!--TOC-->
54
+
55
+ - [Schema-First](#schema-first)
56
+ - [Features](#features)
57
+ - [Installation](#installation)
58
+ - [Example](#example)
59
+ - [Additional documentation](#additional-documentation)
60
+
61
+ <!--TOC-->
62
+
63
+ ## Features
64
+
65
+ * OpenAPI specification validate.
66
+ * Convert OpenAPI schemas to Marshmallow schemas.
67
+
68
+ ## Installation
69
+
70
+ Recommended using the latest version of Python. Schema-First supports Python 3.14 and newer.
71
+
72
+ Install and update using `pip`:
73
+
74
+ ```shell
75
+ $ pip install -U schema_first
76
+ ```
77
+
78
+ ## Example
79
+
80
+ Create specification - `openapi.yaml`:
81
+ ```yaml
82
+ openapi: 3.1.1
83
+ info:
84
+ title: Example API for testing Flask-First
85
+ version: 1.0.1
86
+ paths:
87
+ /endpoint:
88
+ get:
89
+ operationId: endpoint
90
+ responses:
91
+ '200':
92
+ content:
93
+ application/json:
94
+ schema:
95
+ properties:
96
+ message:
97
+ type: string
98
+ type: object
99
+ description: OK
100
+
101
+
102
+ ```
103
+ Create script - `main.py`:
104
+ ```python
105
+ from pathlib import Path
106
+ from pprint import pprint
107
+
108
+ from schema_first.specification import Specification
109
+
110
+ spec_file = Path('openapi.yaml')
111
+ spec = Specification(spec_file)
112
+ spec.load()
113
+
114
+ pprint(spec.reassembly_spec)
115
+ print(
116
+ 'Marshmallow schema generated from OpenAPI schema',
117
+ spec.reassembly_spec['paths']['/endpoint']['get']['responses']['200']['content'][
118
+ 'application/json'
119
+ ]['schema'],
120
+ )
121
+
122
+ ```
123
+
124
+ More example see to `./example` folder.
125
+
126
+ ## Additional documentation
127
+
128
+ * [OpenAPI Documentation](https://swagger.io/specification/).
129
+ * [OpenAPI on GitHub](https://github.com/OAI/OpenAPI-Specification).
130
+ * [JSON Schema Documentation](https://json-schema.org/specification.html).
@@ -0,0 +1,82 @@
1
+ # Schema-First
2
+
3
+ Validate and convert OpenAPI specification via Marshmallow schemas to Marshmallow schemas.
4
+
5
+ <!--TOC-->
6
+
7
+ - [Schema-First](#schema-first)
8
+ - [Features](#features)
9
+ - [Installation](#installation)
10
+ - [Example](#example)
11
+ - [Additional documentation](#additional-documentation)
12
+
13
+ <!--TOC-->
14
+
15
+ ## Features
16
+
17
+ * OpenAPI specification validate.
18
+ * Convert OpenAPI schemas to Marshmallow schemas.
19
+
20
+ ## Installation
21
+
22
+ Recommended using the latest version of Python. Schema-First supports Python 3.14 and newer.
23
+
24
+ Install and update using `pip`:
25
+
26
+ ```shell
27
+ $ pip install -U schema_first
28
+ ```
29
+
30
+ ## Example
31
+
32
+ Create specification - `openapi.yaml`:
33
+ ```yaml
34
+ openapi: 3.1.1
35
+ info:
36
+ title: Example API for testing Flask-First
37
+ version: 1.0.1
38
+ paths:
39
+ /endpoint:
40
+ get:
41
+ operationId: endpoint
42
+ responses:
43
+ '200':
44
+ content:
45
+ application/json:
46
+ schema:
47
+ properties:
48
+ message:
49
+ type: string
50
+ type: object
51
+ description: OK
52
+
53
+
54
+ ```
55
+ Create script - `main.py`:
56
+ ```python
57
+ from pathlib import Path
58
+ from pprint import pprint
59
+
60
+ from schema_first.specification import Specification
61
+
62
+ spec_file = Path('openapi.yaml')
63
+ spec = Specification(spec_file)
64
+ spec.load()
65
+
66
+ pprint(spec.reassembly_spec)
67
+ print(
68
+ 'Marshmallow schema generated from OpenAPI schema',
69
+ spec.reassembly_spec['paths']['/endpoint']['get']['responses']['200']['content'][
70
+ 'application/json'
71
+ ]['schema'],
72
+ )
73
+
74
+ ```
75
+
76
+ More example see to `./example` folder.
77
+
78
+ ## Additional documentation
79
+
80
+ * [OpenAPI Documentation](https://swagger.io/specification/).
81
+ * [OpenAPI on GitHub](https://github.com/OAI/OpenAPI-Specification).
82
+ * [JSON Schema Documentation](https://json-schema.org/specification.html).
@@ -0,0 +1,62 @@
1
+ [build-system]
2
+ build-backend = "setuptools.build_meta"
3
+ requires = ["setuptools>=42", "wheel"]
4
+
5
+ [project]
6
+ authors = [
7
+ {name = "Konstantin Fadeev", email = "fadeev@legalact.pro"}
8
+ ]
9
+ classifiers = [
10
+ "License :: OSI Approved :: MIT License",
11
+ "Operating System :: OS Independent",
12
+ "Programming Language :: Python :: 3"
13
+ ]
14
+ dependencies = [
15
+ 'marshmallow>=4.0.0',
16
+ 'PyYAML>=6.0.2'
17
+ ]
18
+ description = "OpenAPI specification validator and converter to Marshmallow schemas."
19
+ license = {file = "LICENSE"}
20
+ name = "Schema-First"
21
+ readme = "README.md"
22
+ requires-python = ">=3.14"
23
+ version = "0.3.0"
24
+
25
+ [project.optional-dependencies]
26
+ dev = [
27
+ "bandit==1.8.6",
28
+ "build==1.2.1",
29
+ 'openapi-spec-validator>=0.5.0',
30
+ "pre-commit==4.2.0",
31
+ "pytest==8.4.1",
32
+ "pytest-cov==6.2.1",
33
+ "python-dotenv==1.1.1",
34
+ "twine==6.1.0"
35
+ ]
36
+
37
+ [project.urls]
38
+ changelog = "https://github.com/flask-pro/schema-first/blob/master/CHANGES.md"
39
+ repository = "https://github.com/flask-pro/schema-first"
40
+
41
+ [tool.black]
42
+ extend-exclude = '''
43
+ # A regex preceded with ^/ will apply only to files and directories
44
+ # in the root of the project.
45
+ ^/foo.py # exclude a file named foo.py in the root of the project (in addition to the defaults)
46
+ '''
47
+ include = '\.pyi?$'
48
+ line-length = 100
49
+ skip-string-normalization = true
50
+ target-version = ['py313']
51
+
52
+ [tool.isort]
53
+ profile = "google"
54
+ src_paths = ["src", "tests"]
55
+
56
+ [tool.pycln]
57
+ all = true
58
+ silence = true
59
+
60
+ [tool.setuptools.packages.find]
61
+ include = ["schema_first*"]
62
+ where = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,130 @@
1
+ Metadata-Version: 2.4
2
+ Name: Schema-First
3
+ Version: 0.3.0
4
+ Summary: OpenAPI specification validator and converter to Marshmallow schemas.
5
+ Author-email: Konstantin Fadeev <fadeev@legalact.pro>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2025 flask-pro
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: changelog, https://github.com/flask-pro/schema-first/blob/master/CHANGES.md
29
+ Project-URL: repository, https://github.com/flask-pro/schema-first
30
+ Classifier: License :: OSI Approved :: MIT License
31
+ Classifier: Operating System :: OS Independent
32
+ Classifier: Programming Language :: Python :: 3
33
+ Requires-Python: >=3.14
34
+ Description-Content-Type: text/markdown
35
+ License-File: LICENSE
36
+ Requires-Dist: marshmallow>=4.0.0
37
+ Requires-Dist: PyYAML>=6.0.2
38
+ Provides-Extra: dev
39
+ Requires-Dist: bandit==1.8.6; extra == "dev"
40
+ Requires-Dist: build==1.2.1; extra == "dev"
41
+ Requires-Dist: openapi-spec-validator>=0.5.0; extra == "dev"
42
+ Requires-Dist: pre-commit==4.2.0; extra == "dev"
43
+ Requires-Dist: pytest==8.4.1; extra == "dev"
44
+ Requires-Dist: pytest-cov==6.2.1; extra == "dev"
45
+ Requires-Dist: python-dotenv==1.1.1; extra == "dev"
46
+ Requires-Dist: twine==6.1.0; extra == "dev"
47
+ Dynamic: license-file
48
+
49
+ # Schema-First
50
+
51
+ Validate and convert OpenAPI specification via Marshmallow schemas to Marshmallow schemas.
52
+
53
+ <!--TOC-->
54
+
55
+ - [Schema-First](#schema-first)
56
+ - [Features](#features)
57
+ - [Installation](#installation)
58
+ - [Example](#example)
59
+ - [Additional documentation](#additional-documentation)
60
+
61
+ <!--TOC-->
62
+
63
+ ## Features
64
+
65
+ * OpenAPI specification validate.
66
+ * Convert OpenAPI schemas to Marshmallow schemas.
67
+
68
+ ## Installation
69
+
70
+ Recommended using the latest version of Python. Schema-First supports Python 3.14 and newer.
71
+
72
+ Install and update using `pip`:
73
+
74
+ ```shell
75
+ $ pip install -U schema_first
76
+ ```
77
+
78
+ ## Example
79
+
80
+ Create specification - `openapi.yaml`:
81
+ ```yaml
82
+ openapi: 3.1.1
83
+ info:
84
+ title: Example API for testing Flask-First
85
+ version: 1.0.1
86
+ paths:
87
+ /endpoint:
88
+ get:
89
+ operationId: endpoint
90
+ responses:
91
+ '200':
92
+ content:
93
+ application/json:
94
+ schema:
95
+ properties:
96
+ message:
97
+ type: string
98
+ type: object
99
+ description: OK
100
+
101
+
102
+ ```
103
+ Create script - `main.py`:
104
+ ```python
105
+ from pathlib import Path
106
+ from pprint import pprint
107
+
108
+ from schema_first.specification import Specification
109
+
110
+ spec_file = Path('openapi.yaml')
111
+ spec = Specification(spec_file)
112
+ spec.load()
113
+
114
+ pprint(spec.reassembly_spec)
115
+ print(
116
+ 'Marshmallow schema generated from OpenAPI schema',
117
+ spec.reassembly_spec['paths']['/endpoint']['get']['responses']['200']['content'][
118
+ 'application/json'
119
+ ]['schema'],
120
+ )
121
+
122
+ ```
123
+
124
+ More example see to `./example` folder.
125
+
126
+ ## Additional documentation
127
+
128
+ * [OpenAPI Documentation](https://swagger.io/specification/).
129
+ * [OpenAPI on GitHub](https://github.com/OAI/OpenAPI-Specification).
130
+ * [JSON Schema Documentation](https://json-schema.org/specification.html).
@@ -0,0 +1,36 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/Schema_First.egg-info/PKG-INFO
5
+ src/Schema_First.egg-info/SOURCES.txt
6
+ src/Schema_First.egg-info/dependency_links.txt
7
+ src/Schema_First.egg-info/requires.txt
8
+ src/Schema_First.egg-info/top_level.txt
9
+ src/schema_first/__init__.py
10
+ src/schema_first/exceptions.py
11
+ src/schema_first/loaders/__init__.py
12
+ src/schema_first/loaders/exc.py
13
+ src/schema_first/loaders/yaml_loader.py
14
+ src/schema_first/openapi/__init__.py
15
+ src/schema_first/openapi/exc.py
16
+ src/schema_first/openapi/schemas/__init__.py
17
+ src/schema_first/openapi/schemas/base.py
18
+ src/schema_first/openapi/schemas/constants.py
19
+ src/schema_first/openapi/schemas/fields.py
20
+ src/schema_first/openapi/schemas/v3_1_1/components_object_schema.py
21
+ src/schema_first/openapi/schemas/v3_1_1/contact_schema.py
22
+ src/schema_first/openapi/schemas/v3_1_1/info_schema.py
23
+ src/schema_first/openapi/schemas/v3_1_1/license_schema.py
24
+ src/schema_first/openapi/schemas/v3_1_1/media_type_object_schema.py
25
+ src/schema_first/openapi/schemas/v3_1_1/operation_object_schema.py
26
+ src/schema_first/openapi/schemas/v3_1_1/path_item_object_schema.py
27
+ src/schema_first/openapi/schemas/v3_1_1/reference_object_schema.py
28
+ src/schema_first/openapi/schemas/v3_1_1/request_body_object_schema.py
29
+ src/schema_first/openapi/schemas/v3_1_1/responses_object_schema.py
30
+ src/schema_first/openapi/schemas/v3_1_1/root_schema.py
31
+ src/schema_first/openapi/schemas/v3_1_1/schema_object_schema.py
32
+ src/schema_first/openapi/schemas/v3_1_1/server_schema.py
33
+ src/schema_first/openapi/schemas/v3_1_1/server_variable_object_schema.py
34
+ src/schema_first/specification/__init__.py
35
+ tests/test_specification.py
36
+ tests/test_validator.py
@@ -0,0 +1,12 @@
1
+ marshmallow>=4.0.0
2
+ PyYAML>=6.0.2
3
+
4
+ [dev]
5
+ bandit==1.8.6
6
+ build==1.2.1
7
+ openapi-spec-validator>=0.5.0
8
+ pre-commit==4.2.0
9
+ pytest==8.4.1
10
+ pytest-cov==6.2.1
11
+ python-dotenv==1.1.1
12
+ twine==6.1.0
@@ -0,0 +1 @@
1
+ schema_first
@@ -0,0 +1,3 @@
1
+ from .specification import Specification
2
+
3
+ __all__ = ['Specification']
@@ -0,0 +1,2 @@
1
+ class SchemaFirstException(Exception):
2
+ """Common exception."""
@@ -0,0 +1,3 @@
1
+ from .yaml_loader import load_from_yaml
2
+
3
+ __all__ = ['load_from_yaml']
@@ -0,0 +1,9 @@
1
+ from ..exceptions import SchemaFirstException
2
+
3
+
4
+ class YAMLReaderError(SchemaFirstException):
5
+ """Exception for yaml file loading error."""
6
+
7
+
8
+ class ResolverError(SchemaFirstException):
9
+ """Exception for specification from yaml file resolver error."""
@@ -0,0 +1,143 @@
1
+ from collections.abc import Hashable
2
+ from copy import deepcopy
3
+ from functools import reduce
4
+ from pathlib import Path
5
+ from typing import Any
6
+
7
+ import yaml
8
+
9
+ from .exc import ResolverError
10
+ from .exc import YAMLReaderError
11
+
12
+
13
+ class YAMLReader:
14
+ """
15
+ Open OpenAPI specification from yaml file. The specification from multiple files is supported.
16
+ """
17
+
18
+ def __init__(self, path: Path):
19
+ self.path = path
20
+ self.root_file_name = self.path.name
21
+ self.store = {}
22
+
23
+ @staticmethod
24
+ def _yaml_to_dict(path: Path) -> dict:
25
+ with open(path) as f:
26
+ s = yaml.safe_load(f)
27
+ return s
28
+
29
+ def add_file_to_store(self, file_path: str) -> None:
30
+ path_to_spec_file = Path(self.path.parent, file_path)
31
+
32
+ try:
33
+ self.store[file_path] = self._yaml_to_dict(path_to_spec_file)
34
+ except FileNotFoundError:
35
+ raise YAMLReaderError(f'No such file or directory: <{file_path}>')
36
+
37
+ return self.store[file_path]
38
+
39
+ def search_file(self, obj: dict or list) -> None:
40
+ if isinstance(obj, dict):
41
+ ref = obj.get('$ref')
42
+ if ref:
43
+ try:
44
+ file_path, _ = ref.split('#/')
45
+ except (AttributeError, ValueError):
46
+ raise YAMLReaderError(f'"$ref" with value <{ref}> is not valid.')
47
+
48
+ if file_path and file_path not in self.store:
49
+ self.search_file(self.add_file_to_store(file_path))
50
+ else:
51
+ for _, v in obj.items():
52
+ self.search_file(v)
53
+
54
+ elif isinstance(obj, list):
55
+ for item in obj:
56
+ self.search_file(item)
57
+
58
+ else:
59
+ return
60
+
61
+ def load(self) -> 'YAMLReader':
62
+ root_file = self._yaml_to_dict(self.path)
63
+ self.store[self.root_file_name] = root_file
64
+ self.search_file(root_file)
65
+ return self
66
+
67
+
68
+ class RefResolver:
69
+ """Resolve links to various parts of the specification."""
70
+
71
+ def __init__(self, yaml_reader: YAMLReader):
72
+ self.yaml_reader = yaml_reader
73
+ self.resolved_spec = None
74
+
75
+ def _get_schema_via_local_ref(self, file_path: str, node_path: str) -> dict:
76
+ keys = node_path.split('/')
77
+
78
+ def get_value_of_key_from_dict(source_dict: dict, key: Hashable) -> Any:
79
+ return source_dict[key]
80
+
81
+ try:
82
+ return deepcopy(
83
+ reduce(get_value_of_key_from_dict, keys, self.yaml_reader.store[file_path])
84
+ )
85
+ except KeyError:
86
+ raise ResolverError(f'No such path: "{node_path}"')
87
+
88
+ def _get_schema(self, root_file_name: str, file_path: str or None, node_path: str) -> Any:
89
+ if file_path and node_path:
90
+ obj = self._get_schema_via_local_ref(file_path, node_path)
91
+
92
+ elif node_path and not file_path:
93
+ obj = self._get_schema_via_local_ref(root_file_name, node_path)
94
+
95
+ else:
96
+ raise NotImplementedError
97
+
98
+ return obj
99
+
100
+ def _resolving_all_refs(self, file_path: str, obj: Any) -> Any:
101
+ if isinstance(obj, dict):
102
+ ref = obj.get('$ref', ...)
103
+ if ref is not ...:
104
+ try:
105
+ file_path_from_ref, node_path = ref.split('#/')
106
+ except (AttributeError, ValueError):
107
+ raise ResolverError(
108
+ f'"$ref" with value <{ref}> is not valid in file <{file_path}>'
109
+ )
110
+
111
+ if file_path_from_ref:
112
+ obj = self._resolving_all_refs(
113
+ file_path_from_ref,
114
+ self._get_schema(file_path, file_path_from_ref, node_path),
115
+ )
116
+ else:
117
+ obj = self._resolving_all_refs(
118
+ file_path, self._get_schema(file_path, file_path_from_ref, node_path)
119
+ )
120
+
121
+ else:
122
+ for key, value in obj.items():
123
+ obj[key] = self._resolving_all_refs(file_path, value)
124
+
125
+ if isinstance(obj, list):
126
+ objs = []
127
+ for item_obj in obj:
128
+ objs.append(self._resolving_all_refs(file_path, item_obj))
129
+ obj = objs
130
+
131
+ return obj
132
+
133
+ def resolving(self) -> 'RefResolver':
134
+ root_file_path = self.yaml_reader.root_file_name
135
+ root_spec = self.yaml_reader.store[root_file_path]
136
+ self.resolved_spec = self._resolving_all_refs(root_file_path, root_spec)
137
+ return self
138
+
139
+
140
+ def load_from_yaml(path: Path) -> dict:
141
+ yaml_reader = YAMLReader(path).load()
142
+ resolved_obj = RefResolver(yaml_reader).resolving()
143
+ return resolved_obj.resolved_spec
@@ -0,0 +1,19 @@
1
+ from pathlib import Path
2
+
3
+ from marshmallow import ValidationError
4
+
5
+ from ..loaders.yaml_loader import load_from_yaml
6
+ from .exc import OpenAPIValidationError
7
+ from .schemas.v3_1_1.root_schema import RootSchema
8
+
9
+
10
+ class OpenAPI:
11
+ def __init__(self, path: Path or str):
12
+ self.path = path
13
+ self.raw_spec = load_from_yaml(self.path)
14
+
15
+ def load(self) -> dict:
16
+ try:
17
+ return RootSchema().load(self.raw_spec)
18
+ except ValidationError as e:
19
+ raise OpenAPIValidationError(e)
@@ -0,0 +1,5 @@
1
+ from ..exceptions import SchemaFirstException
2
+
3
+
4
+ class OpenAPIValidationError(SchemaFirstException):
5
+ """OpenAPI specification validation error."""
@@ -0,0 +1,20 @@
1
+ from marshmallow import RAISE
2
+ from marshmallow import Schema
3
+ from marshmallow import validates_schema
4
+ from marshmallow import ValidationError
5
+
6
+
7
+ class BaseSchema(Schema):
8
+ class Meta:
9
+ unknown = RAISE
10
+
11
+ @validates_schema
12
+ def validate_ref(self, data, **kwargs) -> None:
13
+ if 'ref' in data:
14
+ ALLOWED_FIELDS = {'ref', 'description', 'summary'}
15
+ ALL_FIELDS = set(data.keys())
16
+ if ALL_FIELDS.difference(ALLOWED_FIELDS):
17
+ raise ValidationError(
18
+ f"If there is a <'ref'> field, then only <{ALLOWED_FIELDS}>,"
19
+ f" but set <{ALL_FIELDS}>"
20
+ )
@@ -0,0 +1,5 @@
1
+ OPENAPI_VERSION = '3.1.1'
2
+ TYPES = ('array', 'boolean', 'integer', 'number', 'object', 'string')
3
+ FORMATS = ('uuid', 'date-time', 'date', 'time', 'email', 'ipv4', 'ipv6', 'uri', 'binary')
4
+ RE_VERSION = r'^[0-9]+.[0-9]+.[0-9]+$'
5
+ RE_SERVER_URL = r'^((http|https)://)|(/)*$'
@@ -0,0 +1,12 @@
1
+ from marshmallow import fields
2
+ from marshmallow import validate
3
+
4
+ ENDPOINT_FIELD = fields.String(required=True, validate=validate.Regexp(r'^[/][0-9a-z-{}/]*[^/]$'))
5
+ HTTP_CODE_FIELD = fields.String(required=True, validate=validate.Regexp(r'^[1-5]{1}\d{2}|default$'))
6
+ SUMMARY_FIELD = fields.String()
7
+ DESCRIPTION_FIELD = fields.String()
8
+ REQUIRED_DESCRIPTION_FIELD = fields.String(required=True)
9
+ MEDIA_TYPE_FIELD = fields.String(required=True)
10
+ REF_FIELD = fields.String(
11
+ required=True, data_key='$ref', validate=validate.Regexp(r'^#/[a-zA-Z/]*$')
12
+ )
@@ -0,0 +1,14 @@
1
+ from marshmallow import fields
2
+
3
+ from ..base import BaseSchema
4
+ from .responses_object_schema import ResponsesObjectSchema
5
+ from .schema_object_schema import SchemaObjectSchema
6
+
7
+
8
+ class ComponentsObjectSchema(BaseSchema):
9
+ responses = fields.Dict(
10
+ keys=fields.String(), values=fields.Nested(ResponsesObjectSchema, required=True)
11
+ )
12
+ schemas = fields.Dict(
13
+ keys=fields.String(), values=fields.Nested(SchemaObjectSchema, required=True)
14
+ )
@@ -0,0 +1,9 @@
1
+ from marshmallow import fields
2
+
3
+ from ..base import BaseSchema
4
+
5
+
6
+ class ContactSchema(BaseSchema):
7
+ name = fields.String()
8
+ url = fields.URL()
9
+ email = fields.Email()
@@ -0,0 +1,21 @@
1
+ from marshmallow import fields
2
+ from marshmallow import validate
3
+
4
+ from ..base import BaseSchema
5
+ from ..constants import RE_VERSION
6
+ from ..fields import DESCRIPTION_FIELD
7
+ from ..fields import SUMMARY_FIELD
8
+ from .contact_schema import ContactSchema
9
+ from .license_schema import LicenseSchema
10
+
11
+
12
+ class InfoSchema(BaseSchema):
13
+ title = fields.String(required=True)
14
+ version = fields.String(required=True, validate=validate.Regexp(RE_VERSION))
15
+
16
+ summary = SUMMARY_FIELD
17
+ description = DESCRIPTION_FIELD
18
+ terms_of_service = fields.String(data_key='termsOfService')
19
+
20
+ contact = fields.Nested(ContactSchema)
21
+ license = fields.Nested(LicenseSchema)
@@ -0,0 +1,19 @@
1
+ from marshmallow import fields
2
+ from marshmallow import validates_schema
3
+ from marshmallow import ValidationError
4
+
5
+ from ..base import BaseSchema
6
+
7
+
8
+ class LicenseSchema(BaseSchema):
9
+ name = fields.String(required=True)
10
+
11
+ identifier = fields.String()
12
+ url = fields.URL()
13
+
14
+ @validates_schema
15
+ def validate_exclusive(self, data, **kwargs) -> None:
16
+ if 'identifier' in data and 'url' in data:
17
+ raise ValidationError(
18
+ 'The <identifier> field is mutually exclusive of the <url> field.'
19
+ )
@@ -0,0 +1,8 @@
1
+ from marshmallow import fields
2
+
3
+ from ..base import BaseSchema
4
+ from .schema_object_schema import SchemaObjectSchema
5
+
6
+
7
+ class MediaTypeObjectSchema(BaseSchema):
8
+ schema = fields.Nested(SchemaObjectSchema)
@@ -0,0 +1,12 @@
1
+ from marshmallow import fields
2
+
3
+ from ..base import BaseSchema
4
+ from ..fields import HTTP_CODE_FIELD
5
+ from .request_body_object_schema import RequestBodyObject
6
+ from .responses_object_schema import ResponsesObjectSchema
7
+
8
+
9
+ class OperationObjectSchema(BaseSchema):
10
+ operation_id = fields.String(data_key='operationId')
11
+ requestBody = fields.Nested(RequestBodyObject)
12
+ responses = fields.Dict(keys=HTTP_CODE_FIELD, values=fields.Nested(ResponsesObjectSchema))
@@ -0,0 +1,9 @@
1
+ from marshmallow import fields
2
+
3
+ from ..base import BaseSchema
4
+ from .operation_object_schema import OperationObjectSchema
5
+
6
+
7
+ class PathItemObjectSchema(BaseSchema):
8
+ get = fields.Nested(OperationObjectSchema)
9
+ post = fields.Nested(OperationObjectSchema)
@@ -0,0 +1,8 @@
1
+ from schema_first.openapi.schemas._base import BaseSchema
2
+ from schema_first.openapi.schemas._fields import DESCRIPTION_FIELD
3
+ from schema_first.openapi.schemas._fields import REF_FIELD
4
+
5
+
6
+ class ReferenceObjectSchema(BaseSchema):
7
+ description = DESCRIPTION_FIELD
8
+ ref = REF_FIELD
@@ -0,0 +1,11 @@
1
+ from marshmallow import fields
2
+
3
+ from ..base import BaseSchema
4
+ from ..fields import DESCRIPTION_FIELD
5
+ from ..fields import MEDIA_TYPE_FIELD
6
+ from .media_type_object_schema import MediaTypeObjectSchema
7
+
8
+
9
+ class RequestBodyObject(BaseSchema):
10
+ description = DESCRIPTION_FIELD
11
+ content = fields.Dict(keys=MEDIA_TYPE_FIELD, values=fields.Nested(MediaTypeObjectSchema))
@@ -0,0 +1,11 @@
1
+ from marshmallow import fields
2
+
3
+ from ..base import BaseSchema
4
+ from ..fields import DESCRIPTION_FIELD
5
+ from ..fields import MEDIA_TYPE_FIELD
6
+ from .media_type_object_schema import MediaTypeObjectSchema
7
+
8
+
9
+ class ResponsesObjectSchema(BaseSchema):
10
+ description = DESCRIPTION_FIELD
11
+ content = fields.Dict(keys=MEDIA_TYPE_FIELD, values=fields.Nested(MediaTypeObjectSchema))
@@ -0,0 +1,25 @@
1
+ from marshmallow import fields
2
+ from marshmallow import validate
3
+
4
+ from ..base import BaseSchema
5
+ from ..constants import OPENAPI_VERSION
6
+ from ..fields import ENDPOINT_FIELD
7
+ from .components_object_schema import ComponentsObjectSchema
8
+ from .info_schema import InfoSchema
9
+ from .path_item_object_schema import PathItemObjectSchema
10
+ from .server_schema import ServerSchema
11
+
12
+
13
+ class RootSchema(BaseSchema):
14
+ openapi = fields.String(required=True, validate=validate.Equal(OPENAPI_VERSION))
15
+ info = fields.Nested(InfoSchema, required=True)
16
+ paths = fields.Dict(
17
+ required=True,
18
+ keys=ENDPOINT_FIELD,
19
+ values=fields.Nested(PathItemObjectSchema, required=True),
20
+ )
21
+
22
+ jsonSchemaDialect = fields.URL()
23
+
24
+ servers = fields.Nested(ServerSchema, many=True)
25
+ components = fields.Nested(ComponentsObjectSchema)
@@ -0,0 +1,17 @@
1
+ from marshmallow import fields
2
+ from marshmallow import validate
3
+
4
+ from ..base import BaseSchema
5
+ from ..constants import FORMATS
6
+ from ..constants import TYPES
7
+
8
+
9
+ class SchemaObjectSchema(BaseSchema):
10
+ type = fields.String(required=True, validate=validate.OneOf(TYPES))
11
+
12
+ format = fields.String(validate=validate.OneOf(FORMATS))
13
+ pattern = fields.String()
14
+
15
+ properties = fields.Dict(
16
+ keys=fields.String(required=True), values=fields.Nested(lambda: SchemaObjectSchema())
17
+ )
@@ -0,0 +1,19 @@
1
+ from marshmallow import fields
2
+ from marshmallow import validate
3
+
4
+ from ..base import BaseSchema
5
+ from ..constants import RE_SERVER_URL
6
+ from ..fields import DESCRIPTION_FIELD
7
+ from .server_variable_object_schema import ServerVariableObjectSchema
8
+
9
+
10
+ class ServerSchema(BaseSchema):
11
+ url = fields.String(
12
+ required=True, validate=[validate.Regexp(RE_SERVER_URL), validate.Length(min=1)]
13
+ )
14
+
15
+ description = DESCRIPTION_FIELD
16
+
17
+ variables = fields.Dict(
18
+ keys=fields.String(), values=fields.Nested(ServerVariableObjectSchema, required=True)
19
+ )
@@ -0,0 +1,12 @@
1
+ from marshmallow import fields
2
+ from marshmallow import validate
3
+
4
+ from ..base import BaseSchema
5
+ from ..fields import DESCRIPTION_FIELD
6
+
7
+
8
+ class ServerVariableObjectSchema(BaseSchema):
9
+ default = fields.String(required=True, validate=[validate.Length(min=1)])
10
+
11
+ enum = fields.List(fields.String(validate=[validate.Length(min=1)]))
12
+ description = DESCRIPTION_FIELD
@@ -0,0 +1,47 @@
1
+ from copy import deepcopy
2
+ from pathlib import Path
3
+ from typing import Any
4
+
5
+ from marshmallow import fields
6
+ from marshmallow import Schema
7
+
8
+ from ..openapi import OpenAPI
9
+
10
+ FIELDS_VIA_TYPES = {
11
+ 'boolean': fields.Boolean,
12
+ 'number': fields.Float,
13
+ 'string': fields.String,
14
+ 'integer': fields.Integer,
15
+ }
16
+
17
+
18
+ class Specification:
19
+ def __init__(self, spec_file: Path | str):
20
+ self.openapi = OpenAPI(spec_file)
21
+ self.reassembly_spec = None
22
+
23
+ def _convert_from_openapi_to_marshmallow_schema(self, open_api_schema: dict) -> type[Schema]:
24
+ if open_api_schema['type'] == 'object':
25
+ marshmallow_schema = {}
26
+ for field_name, field in open_api_schema['properties'].items():
27
+ marshmallow_schema[field_name] = FIELDS_VIA_TYPES[field['type']]()
28
+ else:
29
+ raise NotImplementedError(open_api_schema)
30
+
31
+ return Schema.from_dict(marshmallow_schema)
32
+
33
+ def _reassembly_of_schemas(self, obj: Any) -> Any:
34
+ if isinstance(obj, dict):
35
+ for k, v in obj.items():
36
+ if k == 'schema':
37
+ obj[k] = self._convert_from_openapi_to_marshmallow_schema(v)
38
+ else:
39
+ self._reassembly_of_schemas(v)
40
+
41
+ def load(self) -> 'Specification':
42
+ self.openapi.load()
43
+ self.reassembly_spec = deepcopy(self.openapi.raw_spec)
44
+
45
+ self._reassembly_of_schemas(self.reassembly_spec)
46
+
47
+ return self
@@ -0,0 +1,33 @@
1
+ from marshmallow import fields
2
+
3
+ from src.schema_first.openapi import OpenAPI
4
+ from src.schema_first.specification import Specification
5
+ from tests.utils import get_schema_from_request
6
+
7
+
8
+ def test_specification__minimal(fx_spec_minimal, fx_spec_as_file):
9
+ spec_file = fx_spec_as_file(fx_spec_minimal)
10
+ spec = Specification(spec_file)
11
+
12
+ assert isinstance(spec.openapi, OpenAPI)
13
+ assert spec.reassembly_spec is None
14
+
15
+ spec.load()
16
+
17
+ request_schema = get_schema_from_request(spec.reassembly_spec, '/endpoint', '200')
18
+ assert isinstance(request_schema().fields['message'], fields.String)
19
+ assert request_schema().load({'message': 'Valid string'})
20
+
21
+
22
+ def test_specification__full(fx_spec_full, fx_spec_as_file):
23
+ spec_file = fx_spec_as_file(fx_spec_full)
24
+ spec = Specification(spec_file)
25
+
26
+ assert isinstance(spec.openapi, OpenAPI)
27
+ assert spec.reassembly_spec is None
28
+
29
+ spec.load()
30
+
31
+ request_schema = get_schema_from_request(spec.reassembly_spec, '/endpoint', '200')
32
+ assert isinstance(request_schema().fields['message'], fields.String)
33
+ assert request_schema().load({'message': 'Valid string'})
@@ -0,0 +1,41 @@
1
+ import pytest
2
+
3
+ from src.schema_first.openapi import OpenAPI
4
+ from src.schema_first.openapi import OpenAPIValidationError
5
+
6
+
7
+ def test_validator_minimal(fx_spec_minimal, fx_spec_as_file):
8
+ spec_file = fx_spec_as_file(fx_spec_minimal)
9
+ open_api_spec = OpenAPI(spec_file)
10
+ open_api_spec.load()
11
+ assert open_api_spec.raw_spec == fx_spec_minimal
12
+
13
+
14
+ def test_validator__full(fx_spec_full, fx_spec_as_file):
15
+ spec_file = fx_spec_as_file(fx_spec_full)
16
+ open_api_spec = OpenAPI(spec_file)
17
+ open_api_spec.load()
18
+ assert open_api_spec.raw_spec == fx_spec_full
19
+
20
+
21
+ def test_validator__minimal__external_validator(fx_spec_minimal, fx_spec_as_file):
22
+ spec_file = fx_spec_as_file(fx_spec_minimal, external_validator=True)
23
+ open_api_spec = OpenAPI(spec_file)
24
+ open_api_spec.load()
25
+
26
+
27
+ def test_validator__full__external_validator(fx_spec_full, fx_spec_as_file):
28
+ spec_file = fx_spec_as_file(fx_spec_full, external_validator=True)
29
+ open_api_spec = OpenAPI(spec_file)
30
+ open_api_spec.load()
31
+
32
+
33
+ def test_validator__wrong_field_name(fx_spec_minimal, fx_spec_as_file):
34
+ fx_spec_minimal['wrong_field_name'] = 'wrong'
35
+
36
+ spec_file = fx_spec_as_file(fx_spec_minimal)
37
+
38
+ open_api_spec = OpenAPI(spec_file)
39
+
40
+ with pytest.raises(OpenAPIValidationError):
41
+ open_api_spec.load()