-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathutils.py
24 lines (17 loc) · 828 Bytes
/
utils.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
from __future__ import annotations
import dataclasses
from typing import Type
from dataclasses_jsonschema import JsonSchemaMixin
from dataclasses_jsonschema.type_defs import JsonDict
def json_schema_array(dataclass_cls: Type[JsonSchemaMixin]) -> JsonDict:
"""Get JSON schema representing an array of `dataclass_cls`."""
WrapperDataClass = type("WrapperDataClass", (JsonSchemaMixin,), {})
WrapperDataClass.__annotations__["item"] = dataclass_cls
WrapperDataClass = dataclasses.dataclass(WrapperDataClass)
schema: JsonDict = WrapperDataClass.json_schema() # type: ignore
schema["type"] = "array"
schema["description"] = f"An array of {dataclass_cls.__name__} objects."
del schema["required"]
schema["items"] = schema["properties"]["item"]
del schema["properties"]
return schema