/
specification.py
342 lines (254 loc) · 9.62 KB
/
specification.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
"""
Python representation of the OpenAPI Specification.
Specs can be found at https://spec.openapis.org/oas/v<semver>#openapi-specification (semver := 3.x.x | 2.x.x)
We will refer to the Python object that coresponds to an OpenAPI object as POS (Python implementation for a OpenAPI Specification).
Note that even though we cover most bases, there are still a lot of OpenAPI features such as deprecation,
webhooks, securities, etc. are yet to be implemented/exposed to user.
"""
from __future__ import annotations
import typing as t
import logging
import attr
import yaml
import cattr.errors
from cattr.gen import override
from cattr.gen import make_dict_unstructure_fn
from ...utils import bentoml_cattr
logger = logging.getLogger(__name__)
_T = t.TypeVar("_T")
@attr.frozen
class Contact:
__omit_if_default__ = True
__forbid_extra_keys__ = True
name: t.Optional[str] = None
url: t.Optional[str] = None
email: t.Optional[str] = None
@attr.frozen
class ExternalDocumentation:
__omit_if_default__ = True
__forbid_extra_keys__ = True
url: str
description: t.Optional[str] = None
@attr.frozen
class Link:
__omit_if_default__ = True
__forbid_extra_keys__ = True
operationRef: t.Optional[str] = None
operationId: t.Optional[str] = None
requestBody: t.Optional[t.Any] = None
description: t.Optional[str] = None
# not yet supported: parameters
@attr.frozen
class Reference:
__rename_fields__ = {"ref": "$ref"}
ref: str
@attr.frozen
class Discriminator:
__omit_if_default__ = True
__forbid_extra_keys__ = True
propertyName: str
mapping: t.Optional[t.Dict[str, str]] = None
@attr.frozen
class Schema:
__omit_if_default__ = True
__forbid_extra_keys__ = False
__rename_fields__ = {"ref": "$ref", "not_": "not"}
type: t.Optional[str]
ref: t.Optional[str] = None
title: t.Optional[str] = None
multipleOf: t.Optional[float] = None
maximum: t.Optional[float] = None
exclusiveMaximum: t.Optional[float] = None
minimum: t.Optional[float] = None
exclusiveMinimum: t.Optional[float] = None
maxLength: t.Optional[int] = None
minLength: t.Optional[int] = None
pattern: t.Optional[str] = None
maxItems: t.Optional[int] = None
minItems: t.Optional[int] = None
uniqueItems: t.Optional[bool] = None
maxProperties: t.Optional[int] = None
minProperties: t.Optional[int] = None
required: t.Optional[t.List[str]] = None
enum: t.Optional[t.List[t.Any]] = None
allOf: t.Optional[t.List[Schema]] = None
oneOf: t.Optional[t.List[Schema]] = None
anyOf: t.Optional[t.List[Schema]] = None
not_: t.Optional[Schema] = None
items: t.Optional[t.Union[Schema, t.List[Schema]]] = None
properties: t.Optional[t.Dict[str, t.Union[Schema, Reference]]] = None
additionalProperties: t.Optional[t.Union[Schema, Reference, bool]] = None
description: t.Optional[str] = None
format: t.Optional[str] = None
default: t.Optional[t.Any] = None
nullable: t.Optional[bool] = None
discriminator: t.Optional[Discriminator] = None
readOnly: t.Optional[bool] = None
writeOnly: t.Optional[bool] = None
externalDocs: t.Optional[ExternalDocumentation] = None
example: t.Optional[t.Any] = None
deprecated: t.Optional[bool] = None
# not yet supported: xml
@attr.frozen
class Example:
__omit_if_default__ = True
__forbid_extra_keys__ = True
summary: t.Optional[str] = None
description: t.Optional[str] = None
value: t.Optional[t.Any] = None
externalValue: t.Optional[str] = None
@attr.frozen
class Encoding:
__omit_if_default__ = True
__forbid_extra_keys__ = True
contentType: t.Optional[str] = None
style: t.Optional[str] = None
explode: t.Optional[bool] = None
allowReserved: t.Optional[bool] = None
# not yet supported: headers
@attr.frozen
class MediaType:
__omit_if_default__ = True
__forbid_extra_keys__ = True
schema: t.Optional[t.Union[Schema, Reference]]
example: t.Optional[t.Any] = None
examples: t.Optional[t.Dict[str, t.Union[Example, Reference]]] = None
encoding: t.Optional[t.Dict[str, Encoding]] = None
@attr.frozen
class Response:
__omit_if_default__ = True
__forbid_extra_keys__ = True
description: str
content: t.Optional[t.Dict[str, MediaType]] = None
links: t.Optional[t.Dict[str, t.Union[Link, Reference]]] = None
# not yet supported: headers
@attr.frozen
class RequestBody:
__omit_if_default__ = True
__forbid_extra_keys__ = True
content: t.Dict[str, MediaType]
description: t.Optional[str] = None
required: t.Optional[bool] = None
@attr.frozen
class Operation:
__omit_if_default__ = True
__forbid_extra_keys__ = True
responses: t.Dict[t.Union[str, int], t.Union[Response, Reference]]
tags: t.Optional[t.List[t.Union[str, Tag]]] = None
summary: t.Optional[str] = None
description: t.Optional[str] = None
externalDocs: t.Optional[ExternalDocumentation] = None
operationId: t.Optional[str] = None
requestBody: t.Optional[t.Union[RequestBody, Reference, t.Dict[str, t.Any]]] = None
# Not yet supported: parameters, callbacks, deprecated, servers, security
@attr.frozen
class Info:
__omit_if_default__ = True
__forbid_extra_keys__ = True
__preserve_cls_structure__ = True
title: str
version: str
description: t.Optional[str] = None
contact: t.Optional[Contact] = None
# Not yet supported: termsOfService
@attr.frozen
class PathItem:
__omit_if_default__ = True
__forbid_extra_keys__ = True
__preserve_cls_structure__ = True
__rename_fields__ = {"ref": "$ref"}
ref: t.Optional[str] = None
summary: t.Optional[str] = None
description: t.Optional[str] = None
get: t.Optional[t.Union[Operation, t.Dict[str, t.Any]]] = None
put: t.Optional[t.Union[Operation, t.Dict[str, t.Any]]] = None
post: t.Optional[t.Union[Operation, t.Dict[str, t.Any]]] = None
delete: t.Optional[t.Union[Operation, t.Dict[str, t.Any]]] = None
options: t.Optional[t.Union[Operation, t.Dict[str, t.Any]]] = None
head: t.Optional[t.Union[Operation, t.Dict[str, t.Any]]] = None
patch: t.Optional[t.Union[Operation, t.Dict[str, t.Any]]] = None
trace: t.Optional[t.Union[Operation, t.Dict[str, t.Any]]] = None
# not yet supported: servers, parameters
@attr.frozen
class Tag:
__omit_if_default__ = True
__forbid_extra_keys__ = True
__preserve_cls_structure__ = True
name: str
description: t.Optional[str] = None
externalDocs: t.Optional[ExternalDocumentation] = None
@attr.frozen
class Components:
__omit_if_default__ = True
__forbid_extra_keys__ = True
__preserve_cls_structure__ = True
schemas: t.Dict[str, t.Union[Schema, Reference]]
responses: t.Optional[t.Dict[str, t.Union[Response, Reference]]] = None
examples: t.Optional[t.Dict[str, t.Union[Example, Reference]]] = None
requestBodies: t.Optional[
t.Dict[str, t.Union[RequestBody, Reference, t.Dict[str, t.Any]]]
] = None
links: t.Optional[t.Dict[str, t.Union[Link, Reference]]] = None
# Not yet supported: securitySchemes, callbacks, parameters, headers
def asdict(self) -> t.Dict[str, t.Any]:
return bentoml_cattr.unstructure(self)
@attr.frozen
class OpenAPISpecification:
__omit_if_default__ = True
__forbid_extra_keys__ = True
openapi: str
info: Info
paths: t.Dict[str, PathItem]
tags: t.Optional[t.List[Tag]] = None
components: t.Optional[Components] = None
# Not yet supported: servers, security, externalDocs, webhooks, jsonSchemaDialect
def asdict(self) -> dict[str, t.Any]:
return bentoml_cattr.unstructure(self)
@classmethod
def from_yaml_file(cls, stream: t.IO[t.Any]) -> OpenAPISpecification:
try:
yaml_content = yaml.safe_load(stream)
except yaml.YAMLError as exc:
logger.error(exc)
raise
try:
return bentoml_cattr.structure(yaml_content, cls)
except cattr.errors.ClassValidationError:
raise
def _structure_rename_fields_hook(data: t.Dict[str, t.Any], cl: t.Type[_T]) -> _T:
# pop is atomic, so we don't need to worry about performance deficit.
# See https://stackoverflow.com/a/17326099/8643197.
rev = {
k: data.pop(v) for k, v in getattr(cl, "__rename_fields__").items() if v in data
}
return cl(**rev, **data)
# handles all OpenAPI class that includes __rename_fields__
bentoml_cattr.register_structure_hook_func(
lambda cls: attr.has(cls) and hasattr(cls, "__rename_fields__"),
lambda data, cl: _structure_rename_fields_hook(data, cl),
)
bentoml_cattr.register_unstructure_hook_factory(
lambda cls: attr.has(cls) and hasattr(cls, "__rename_fields__"),
lambda cls: make_dict_unstructure_fn(
cls,
bentoml_cattr,
# for all classes under OpenAPI, we want to omit default values.
_cattrs_omit_if_default=getattr(cls, "__omit_if_default__", True),
**{k: override(rename=v) for k, v in cls.__rename_fields__.items()},
),
)
# register all class in this structure whom
# implement a '__preserve_cls_structure__' method
def _preserve_cls_structure(data: dict[str, t.Any], cl: t.Type[_T]) -> _T:
if isinstance(data, cl):
return data
return cl(**data)
bentoml_cattr.register_structure_hook_func(
lambda cls: attr.has(cls) and hasattr(cls, "__preserve_cls_structure__"),
lambda data, cls: _preserve_cls_structure(data, cls),
)
def _OpenAPISpecification_dumper(
dumper: yaml.Dumper, spec: OpenAPISpecification
) -> yaml.Node:
return dumper.represent_dict(spec.asdict())
yaml.add_representer(OpenAPISpecification, _OpenAPISpecification_dumper)