Schema-First 0.3.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.
- schema_first/__init__.py +3 -0
- schema_first/exceptions.py +2 -0
- schema_first/loaders/__init__.py +3 -0
- schema_first/loaders/exc.py +9 -0
- schema_first/loaders/yaml_loader.py +143 -0
- schema_first/openapi/__init__.py +19 -0
- schema_first/openapi/exc.py +5 -0
- schema_first/openapi/schemas/__init__.py +0 -0
- schema_first/openapi/schemas/base.py +20 -0
- schema_first/openapi/schemas/constants.py +5 -0
- schema_first/openapi/schemas/fields.py +12 -0
- schema_first/openapi/schemas/v3_1_1/components_object_schema.py +14 -0
- schema_first/openapi/schemas/v3_1_1/contact_schema.py +9 -0
- schema_first/openapi/schemas/v3_1_1/info_schema.py +21 -0
- schema_first/openapi/schemas/v3_1_1/license_schema.py +19 -0
- schema_first/openapi/schemas/v3_1_1/media_type_object_schema.py +8 -0
- schema_first/openapi/schemas/v3_1_1/operation_object_schema.py +12 -0
- schema_first/openapi/schemas/v3_1_1/path_item_object_schema.py +9 -0
- schema_first/openapi/schemas/v3_1_1/reference_object_schema.py +8 -0
- schema_first/openapi/schemas/v3_1_1/request_body_object_schema.py +11 -0
- schema_first/openapi/schemas/v3_1_1/responses_object_schema.py +11 -0
- schema_first/openapi/schemas/v3_1_1/root_schema.py +25 -0
- schema_first/openapi/schemas/v3_1_1/schema_object_schema.py +17 -0
- schema_first/openapi/schemas/v3_1_1/server_schema.py +19 -0
- schema_first/openapi/schemas/v3_1_1/server_variable_object_schema.py +12 -0
- schema_first/specification/__init__.py +47 -0
- schema_first-0.3.0.dist-info/METADATA +130 -0
- schema_first-0.3.0.dist-info/RECORD +31 -0
- schema_first-0.3.0.dist-info/WHEEL +5 -0
- schema_first-0.3.0.dist-info/licenses/LICENSE +21 -0
- schema_first-0.3.0.dist-info/top_level.txt +1 -0
schema_first/__init__.py
ADDED
|
@@ -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)
|
|
File without changes
|
|
@@ -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,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,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,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,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,31 @@
|
|
|
1
|
+
schema_first/__init__.py,sha256=qDMatC3VgB38cXdc14do-mA2OGBw3sSGKQ7BXgLZZbU,70
|
|
2
|
+
schema_first/exceptions.py,sha256=o6XpzuyPSkqA6oll8Bl-0YuDb2Hl5WXWDrssqf82Hz8,67
|
|
3
|
+
schema_first/loaders/__init__.py,sha256=A_Y3u-l05oOJhiEjKJtNVe2x-RSpbRfAxINFBZJa-BE,70
|
|
4
|
+
schema_first/loaders/exc.py,sha256=B4aX4fp0rqa2t9AEusURDaaMnf7IvTDjD8N20iFjqMI,256
|
|
5
|
+
schema_first/loaders/yaml_loader.py,sha256=ZvM07y3N0tXIPyE6RTU1Y422SDbLxQdQrQ-WbNuHKO8,4668
|
|
6
|
+
schema_first/openapi/__init__.py,sha256=pGEzd39iX3c27e60lTGIgKEDcvBskVm02-CoMF4r5pY,517
|
|
7
|
+
schema_first/openapi/exc.py,sha256=O1HJzy_z1ugVgo9X6Kk7FQW_0CECpTP2YkczpapCHPA,150
|
|
8
|
+
schema_first/openapi/schemas/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
+
schema_first/openapi/schemas/base.py,sha256=Tn0K6tpLcbC986wIiQJxnwB7hHOC_syXAxIQOhgRB_Q,656
|
|
10
|
+
schema_first/openapi/schemas/constants.py,sha256=3uuZCsy8JgpAGWv_R76Ac-eIV1K7EjqfTKBzvvVBOtA,269
|
|
11
|
+
schema_first/openapi/schemas/fields.py,sha256=yqcxlSowpl-jQcn0ZIlTMqr5b0e4sz3GvjuZro_ZHy4,548
|
|
12
|
+
schema_first/openapi/schemas/v3_1_1/components_object_schema.py,sha256=Y8ueFyhUZ1JbaQbjFQ9LMk9EuMqXtqUFb-RAQVf6I-s,461
|
|
13
|
+
schema_first/openapi/schemas/v3_1_1/contact_schema.py,sha256=cd6xfdSwOVNebFIWCIejJ7jNmvkSsFUkq5oLt9LIOG0,174
|
|
14
|
+
schema_first/openapi/schemas/v3_1_1/info_schema.py,sha256=QuSgXySyBERFo_L-dItjsUQ_Et8kvf5vfG_KzT62KLc,658
|
|
15
|
+
schema_first/openapi/schemas/v3_1_1/license_schema.py,sha256=XnRGAcMY5DfccL56ytsMHS3fCMhwU_JBG2iSQRslyOU,539
|
|
16
|
+
schema_first/openapi/schemas/v3_1_1/media_type_object_schema.py,sha256=IU2dMzRNzP54UwBt4Jnv-FH5RbLZnVTXJa8hCpGENds,205
|
|
17
|
+
schema_first/openapi/schemas/v3_1_1/operation_object_schema.py,sha256=i17v_GuEHk4jUBjryD54zkGk9Vj4bZ3SAwPIcZQVODA,462
|
|
18
|
+
schema_first/openapi/schemas/v3_1_1/path_item_object_schema.py,sha256=qyxzo8H5lY73PlwfN4ApSChi043lor48gRZQIPW18B0,258
|
|
19
|
+
schema_first/openapi/schemas/v3_1_1/reference_object_schema.py,sha256=ADtUN2YcdgBN_geEUb9w8zgevdHmZ-jy8gSmwme7QCw,283
|
|
20
|
+
schema_first/openapi/schemas/v3_1_1/request_body_object_schema.py,sha256=HmXgJJTEUqX_gYqePilQy4xR2V53HMfutD_ePgIpfgU,368
|
|
21
|
+
schema_first/openapi/schemas/v3_1_1/responses_object_schema.py,sha256=Ot9o81SkAUQBGt6mPqZNlYKL0TDJhbcnqO3Q-tIEddM,372
|
|
22
|
+
schema_first/openapi/schemas/v3_1_1/root_schema.py,sha256=OodBrQ0muvLMS2eMbdCk-b4UUa66Se3pe1UP_2DKn_o,832
|
|
23
|
+
schema_first/openapi/schemas/v3_1_1/schema_object_schema.py,sha256=k7OYm-P8Xb8UejooOW0b9SeyY58duRA4YLADsKmbb_I,492
|
|
24
|
+
schema_first/openapi/schemas/v3_1_1/server_schema.py,sha256=hmItEqewYhxu6KM4i4aZQoXNugypiSjk1fTASwKvdyY,563
|
|
25
|
+
schema_first/openapi/schemas/v3_1_1/server_variable_object_schema.py,sha256=Vmzmr3mPgypZSNKHWBIvua-WbG3kfQRcO_9i2_Es190,370
|
|
26
|
+
schema_first/specification/__init__.py,sha256=JzIXIgScJc2L8aB4D4eQ-1pWraGJGfyk8df2Qib48co,1449
|
|
27
|
+
schema_first-0.3.0.dist-info/licenses/LICENSE,sha256=sJdhNcoY62rN1Yfydvkd0C9Eosua0IgV_5c-VsimkUo,1066
|
|
28
|
+
schema_first-0.3.0.dist-info/METADATA,sha256=ZJs0A4d3QQfhyk8oP37TroiboEYIk95LuZf6oL1Jei8,4233
|
|
29
|
+
schema_first-0.3.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
30
|
+
schema_first-0.3.0.dist-info/top_level.txt,sha256=53JdsiwXq5IWwnuglvlRVUGyemQ90ly3cS6E6bTyxR4,13
|
|
31
|
+
schema_first-0.3.0.dist-info/RECORD,,
|
|
@@ -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 @@
|
|
|
1
|
+
schema_first
|