RacksDB 0.2.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.
- RacksDB-0.2.0.dist-info/LICENSE +674 -0
- RacksDB-0.2.0.dist-info/METADATA +126 -0
- RacksDB-0.2.0.dist-info/RECORD +42 -0
- RacksDB-0.2.0.dist-info/WHEEL +5 -0
- RacksDB-0.2.0.dist-info/entry_points.txt +3 -0
- RacksDB-0.2.0.dist-info/top_level.txt +1 -0
- racksdb/__init__.py +74 -0
- racksdb/bases.py +117 -0
- racksdb/drawers/__init__.py +10 -0
- racksdb/drawers/base.py +42 -0
- racksdb/drawers/infrastructure.py +253 -0
- racksdb/drawers/room.py +160 -0
- racksdb/dtypes/__init__.py +0 -0
- racksdb/dtypes/angle.py +21 -0
- racksdb/dtypes/bytes.py +31 -0
- racksdb/dtypes/dimension.py +23 -0
- racksdb/dtypes/netif_type.py +17 -0
- racksdb/dtypes/rack_height.py +17 -0
- racksdb/dtypes/rack_width.py +26 -0
- racksdb/dtypes/storage_type.py +17 -0
- racksdb/errors.py +9 -0
- racksdb/exec.py +218 -0
- racksdb/generic/__init__.py +0 -0
- racksdb/generic/db.py +609 -0
- racksdb/generic/definedtype.py +24 -0
- racksdb/generic/dumpers/__init__.py +30 -0
- racksdb/generic/dumpers/_common.py +30 -0
- racksdb/generic/dumpers/console.py +31 -0
- racksdb/generic/dumpers/json.py +108 -0
- racksdb/generic/dumpers/yaml.py +138 -0
- racksdb/generic/errors.py +21 -0
- racksdb/generic/openapi.py +199 -0
- racksdb/generic/schema.py +367 -0
- racksdb/generic/views.py +139 -0
- racksdb/tests/__init__.py +0 -0
- racksdb/tests/test_schema.py +211 -0
- racksdb/tests/test_schema_defined_type_loader.py +23 -0
- racksdb/tests/tests_db_load.py +83 -0
- racksdb/version.py +11 -0
- racksdb/views.py +136 -0
- racksdb/web/__init__.py +0 -0
- racksdb/web/app.py +195 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Copyright (c) 2023 Rackslab
|
|
2
|
+
#
|
|
3
|
+
# This file is part of RacksDB.
|
|
4
|
+
#
|
|
5
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
6
|
+
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from ClusterShell.NodeSet import NodeSet
|
|
10
|
+
|
|
11
|
+
from ..errors import DBDumperError
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class DBDumperConsole:
|
|
15
|
+
def __init__(self, show_types=False, objects_map={}, fold=True):
|
|
16
|
+
self.objects_map = objects_map
|
|
17
|
+
self.fold = fold
|
|
18
|
+
|
|
19
|
+
def dump(
|
|
20
|
+
self,
|
|
21
|
+
obj: Any,
|
|
22
|
+
) -> str:
|
|
23
|
+
if not isinstance(obj, list):
|
|
24
|
+
raise DBDumperError(f"Unsupported type '{type(obj)}' for DBDumperConsole")
|
|
25
|
+
if self.fold:
|
|
26
|
+
nodeset = NodeSet()
|
|
27
|
+
for item in obj:
|
|
28
|
+
nodeset.update(item)
|
|
29
|
+
return str(nodeset)
|
|
30
|
+
else:
|
|
31
|
+
return "\n".join(obj)
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# Copyright (c) 2023 Rackslab
|
|
2
|
+
#
|
|
3
|
+
# This file is part of RacksDB.
|
|
4
|
+
#
|
|
5
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
6
|
+
|
|
7
|
+
from typing import Any
|
|
8
|
+
import json
|
|
9
|
+
import logging
|
|
10
|
+
|
|
11
|
+
from ._common import MapperDumper
|
|
12
|
+
from ...generic.db import DBObject, DBObjectRange, DBObjectRangeId, DBDict, DBList
|
|
13
|
+
|
|
14
|
+
logger = logging.getLogger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class GenericJSONEncoder(json.JSONEncoder, MapperDumper):
|
|
18
|
+
def __init__(self, objects_map={}, fold=True, **kwargs):
|
|
19
|
+
json.JSONEncoder.__init__(self, **kwargs)
|
|
20
|
+
MapperDumper.__init__(self, objects_map)
|
|
21
|
+
self.fold = fold
|
|
22
|
+
|
|
23
|
+
def _fill_obj_dict(self, result, obj, prop, value):
|
|
24
|
+
value = self.map(obj, prop, value)
|
|
25
|
+
if value is None:
|
|
26
|
+
return
|
|
27
|
+
if (
|
|
28
|
+
isinstance(value, DBObject)
|
|
29
|
+
or isinstance(value, DBDict)
|
|
30
|
+
or isinstance(value, DBList)
|
|
31
|
+
):
|
|
32
|
+
result[prop] = self.default(value)
|
|
33
|
+
else:
|
|
34
|
+
result[prop] = value
|
|
35
|
+
|
|
36
|
+
def default(self, obj: Any) -> Any:
|
|
37
|
+
if isinstance(obj, DBObjectRange):
|
|
38
|
+
return str(obj.rangeset)
|
|
39
|
+
elif isinstance(obj, DBObjectRangeId):
|
|
40
|
+
return obj.start
|
|
41
|
+
elif isinstance(obj, DBDict):
|
|
42
|
+
if self.fold:
|
|
43
|
+
# Force iteration over the values of the dictionnary to avoid automatic
|
|
44
|
+
# expansion performed by DBDict iterator.
|
|
45
|
+
return [item for item in obj.values()]
|
|
46
|
+
else:
|
|
47
|
+
# Use DBDict iterator to expand potential DBExpandableObject.
|
|
48
|
+
return [item for item in obj]
|
|
49
|
+
elif isinstance(obj, DBList):
|
|
50
|
+
if self.fold:
|
|
51
|
+
return [item for item in obj.itervalues()]
|
|
52
|
+
else:
|
|
53
|
+
# As a DBList is also a standard iterable Python list, json.JSONEncoder
|
|
54
|
+
# will iterate itself over the DBList, thus expanding potential
|
|
55
|
+
# expandable objects automatically. There is nothing more to do in this
|
|
56
|
+
# case.
|
|
57
|
+
return obj
|
|
58
|
+
elif isinstance(obj, DBObject):
|
|
59
|
+
result = {}
|
|
60
|
+
for attribute, value in vars(obj).items():
|
|
61
|
+
# Skip special attributes
|
|
62
|
+
if attribute in [
|
|
63
|
+
"_db",
|
|
64
|
+
"_indexes",
|
|
65
|
+
"_schema",
|
|
66
|
+
"_parent",
|
|
67
|
+
"_first",
|
|
68
|
+
"_key",
|
|
69
|
+
]:
|
|
70
|
+
continue
|
|
71
|
+
# Replace renamed attribute by overriding property
|
|
72
|
+
if attribute.startswith(obj.LOADED_PREFIX):
|
|
73
|
+
attribute = attribute[len(obj.LOADED_PREFIX) :]
|
|
74
|
+
value = getattr(obj, attribute)
|
|
75
|
+
self._fill_obj_dict(result, obj, attribute, value)
|
|
76
|
+
|
|
77
|
+
for prop in obj._computed_props():
|
|
78
|
+
self._fill_obj_dict(result, obj, prop, getattr(obj, prop))
|
|
79
|
+
return result
|
|
80
|
+
# Let the base class default method raise the TypeError
|
|
81
|
+
return json.JSONEncoder.default(self, obj)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class DBDumperJSON:
|
|
85
|
+
def __init__(self, show_types=False, objects_map={}, fold=True):
|
|
86
|
+
self.objects_map = objects_map
|
|
87
|
+
self.fold = fold
|
|
88
|
+
|
|
89
|
+
def dump(self, obj: Any) -> str:
|
|
90
|
+
# DBDict are also standard python dictionnaries, then json.JSONEncoder thinks it
|
|
91
|
+
# can handle without hurdle and it does not call GenericJSONEncoder.default()
|
|
92
|
+
# for this obj type. However:
|
|
93
|
+
#
|
|
94
|
+
# - DBDict must be represented as a list of values in dumps, the keys must not
|
|
95
|
+
# be represented as they are just defined for pratical reasons to facilitate
|
|
96
|
+
# data manipulation in library.
|
|
97
|
+
# - Keys can be DBObjectRange objects that cannot be represented by standard
|
|
98
|
+
# json.JSONEncoder.
|
|
99
|
+
#
|
|
100
|
+
# For these reasons, DBDict is converted here as a standard list of values.
|
|
101
|
+
if isinstance(obj, DBDict):
|
|
102
|
+
if self.fold:
|
|
103
|
+
obj = [item for item in obj.values()]
|
|
104
|
+
else:
|
|
105
|
+
obj = [item for item in obj]
|
|
106
|
+
return GenericJSONEncoder(objects_map=self.objects_map, fold=self.fold).encode(
|
|
107
|
+
obj
|
|
108
|
+
)
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# Copyright (c) 2022-2023 Rackslab
|
|
2
|
+
#
|
|
3
|
+
# This file is part of RacksDB.
|
|
4
|
+
#
|
|
5
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
6
|
+
|
|
7
|
+
import collections
|
|
8
|
+
import logging
|
|
9
|
+
|
|
10
|
+
import yaml
|
|
11
|
+
|
|
12
|
+
from ._common import MapperDumper
|
|
13
|
+
from ..db import DBObject, DBObjectRange, DBObjectRangeId, DBList, DBDict
|
|
14
|
+
from ..definedtype import SchemaDefinedType
|
|
15
|
+
|
|
16
|
+
logger = logging.getLogger(__name__)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class DBDumperYAML(MapperDumper):
|
|
20
|
+
def __init__(self, show_types=False, objects_map={}, fold=True):
|
|
21
|
+
super().__init__(objects_map)
|
|
22
|
+
self.show_types = show_types
|
|
23
|
+
self.fold = fold
|
|
24
|
+
self._setup()
|
|
25
|
+
# refs to last represented objects, used to inform users in case of dump
|
|
26
|
+
# recursion loops
|
|
27
|
+
self._last_objs = collections.deque([], 8)
|
|
28
|
+
|
|
29
|
+
def _represent_list(self, dumper, data):
|
|
30
|
+
|
|
31
|
+
tag = "tag:yaml.org,2002:seq"
|
|
32
|
+
if self.fold:
|
|
33
|
+
value = [dumper.represent_data(_object) for _object in data.itervalues()]
|
|
34
|
+
else:
|
|
35
|
+
value = [dumper.represent_data(_object) for _object in data]
|
|
36
|
+
return yaml.SequenceNode(tag, value)
|
|
37
|
+
|
|
38
|
+
def _represent_dict(self, dumper, data):
|
|
39
|
+
# DBDict are rendered like a list, as well as they are represented in
|
|
40
|
+
# database.
|
|
41
|
+
tag = "tag:yaml.org,2002:seq"
|
|
42
|
+
if self.fold:
|
|
43
|
+
value = [dumper.represent_data(_object) for _object in data.values()]
|
|
44
|
+
else:
|
|
45
|
+
value = [dumper.represent_data(_object) for _object in data]
|
|
46
|
+
return yaml.SequenceNode(tag, value)
|
|
47
|
+
|
|
48
|
+
def _fill_obj_node_value(self, dumper, node_value, obj, prop, value):
|
|
49
|
+
# Check the object is mapped to one of its attribute or None.
|
|
50
|
+
value = self.map(obj, prop, value)
|
|
51
|
+
if value is None:
|
|
52
|
+
return
|
|
53
|
+
node_value.append((dumper.represent_data(prop), dumper.represent_data(value)))
|
|
54
|
+
|
|
55
|
+
def _represent_dbobject(self, dumper, data):
|
|
56
|
+
|
|
57
|
+
self._last_objs.append(type(data).__name__)
|
|
58
|
+
node_value = []
|
|
59
|
+
if self.show_types:
|
|
60
|
+
tag = f"{data.__class__.__name__}"
|
|
61
|
+
else:
|
|
62
|
+
tag = "tag:yaml.org,2002:map" # YAML generic mapping type
|
|
63
|
+
|
|
64
|
+
node = yaml.MappingNode(tag, node_value)
|
|
65
|
+
|
|
66
|
+
for item_key, item_value in vars(data).items():
|
|
67
|
+
# skip special fields
|
|
68
|
+
if item_key in [
|
|
69
|
+
"_db",
|
|
70
|
+
"_indexes",
|
|
71
|
+
"_schema",
|
|
72
|
+
"_parent",
|
|
73
|
+
"_first",
|
|
74
|
+
"_key",
|
|
75
|
+
]:
|
|
76
|
+
continue
|
|
77
|
+
# If the attribute has been renamed with loaded prefix, call bases
|
|
78
|
+
# module class attribute instead.
|
|
79
|
+
if item_key.startswith(data.LOADED_PREFIX):
|
|
80
|
+
item_key = item_key[len(data.LOADED_PREFIX) :]
|
|
81
|
+
item_value = getattr(data, item_key)
|
|
82
|
+
self._fill_obj_node_value(dumper, node_value, data, item_key, item_value)
|
|
83
|
+
|
|
84
|
+
for prop in data._computed_props():
|
|
85
|
+
self._fill_obj_node_value(
|
|
86
|
+
dumper, node_value, data, prop, getattr(data, prop)
|
|
87
|
+
)
|
|
88
|
+
return node
|
|
89
|
+
|
|
90
|
+
def _represent_dbobjectrange(self, dumper, data):
|
|
91
|
+
return dumper.represent_data(str(data.rangeset))
|
|
92
|
+
|
|
93
|
+
def _represent_dbobjectrangeid(self, dumper, data):
|
|
94
|
+
return dumper.represent_data(data.start)
|
|
95
|
+
|
|
96
|
+
def _setup(self):
|
|
97
|
+
yaml.add_representer(DBDict, self._represent_dict)
|
|
98
|
+
yaml.add_representer(DBList, self._represent_list)
|
|
99
|
+
yaml.add_multi_representer(DBObject, self._represent_dbobject)
|
|
100
|
+
yaml.add_multi_representer(DBObjectRange, self._represent_dbobjectrange)
|
|
101
|
+
yaml.add_multi_representer(DBObjectRangeId, self._represent_dbobjectrangeid)
|
|
102
|
+
|
|
103
|
+
def dump(self, data):
|
|
104
|
+
noalias_dumper = yaml.dumper.Dumper
|
|
105
|
+
noalias_dumper.ignore_aliases = lambda self, data: True
|
|
106
|
+
try:
|
|
107
|
+
# Remove last newline to avoid double newline when printed by CLI.
|
|
108
|
+
return yaml.dump(data, Dumper=noalias_dumper).rstrip()
|
|
109
|
+
except RecursionError:
|
|
110
|
+
logger.error(
|
|
111
|
+
"Recursion loop detected during dump, last represented objects:"
|
|
112
|
+
"\n→ %s",
|
|
113
|
+
"\n→ ".join(self._last_objs),
|
|
114
|
+
)
|
|
115
|
+
return ""
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class SchemaDumperYAML:
|
|
119
|
+
def __init__(self):
|
|
120
|
+
self._setup()
|
|
121
|
+
|
|
122
|
+
def _represent_schemadefinedtype(self, dumper, data):
|
|
123
|
+
tag = "tag:yaml.org,2002:str" # YAML generic string type
|
|
124
|
+
node = yaml.ScalarNode(tag, f"{data.pattern} [{data.native.__name__}]")
|
|
125
|
+
return node
|
|
126
|
+
|
|
127
|
+
def _setup(self):
|
|
128
|
+
yaml.add_multi_representer(SchemaDefinedType, self._represent_schemadefinedtype)
|
|
129
|
+
|
|
130
|
+
def dump(self, schema):
|
|
131
|
+
noalias_dumper = yaml.dumper.Dumper
|
|
132
|
+
noalias_dumper.ignore_aliases = lambda self, data: True
|
|
133
|
+
# Dump all Schema object content except _schema attribute. Remove last newline
|
|
134
|
+
# to avoid double newline when printed by CLI.
|
|
135
|
+
return yaml.dump(
|
|
136
|
+
{**schema._schema, **{"_types": schema.types}},
|
|
137
|
+
Dumper=noalias_dumper,
|
|
138
|
+
).rstrip()
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Copyright (c) 2022-2023 Rackslab
|
|
2
|
+
#
|
|
3
|
+
# This file is part of RacksDB.
|
|
4
|
+
#
|
|
5
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class DBSchemaError(Exception):
|
|
9
|
+
pass
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class DBFormatError(Exception):
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class DBDumperError(Exception):
|
|
17
|
+
pass
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class DBViewError(Exception):
|
|
21
|
+
pass
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# Copyright (c) 2023 Rackslab
|
|
2
|
+
#
|
|
3
|
+
# This file is part of RacksDB.
|
|
4
|
+
#
|
|
5
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
6
|
+
|
|
7
|
+
from itertools import chain
|
|
8
|
+
|
|
9
|
+
from .schema import (
|
|
10
|
+
SchemaObject,
|
|
11
|
+
SchemaContainerList,
|
|
12
|
+
SchemaBackReference,
|
|
13
|
+
SchemaExpandable,
|
|
14
|
+
SchemaRangeId,
|
|
15
|
+
SchemaReference,
|
|
16
|
+
)
|
|
17
|
+
from .definedtype import SchemaDefinedType
|
|
18
|
+
from ..version import get_version
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class OpenAPIGenerator:
|
|
22
|
+
def __init__(self, db, views):
|
|
23
|
+
self.db = db
|
|
24
|
+
self.views = views
|
|
25
|
+
|
|
26
|
+
def generate(self):
|
|
27
|
+
"""Generate an OpenAPI schema of a GenericDB and its DBViews."""
|
|
28
|
+
result = {
|
|
29
|
+
"openapi": "3.0.0",
|
|
30
|
+
"info": {"title": f"{self.db.PREFIX} REST API", "version": get_version()},
|
|
31
|
+
"paths": {},
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
# actions including views
|
|
35
|
+
for action in chain(self.views.views_actions(), self.views.actions()):
|
|
36
|
+
result["paths"][action.path] = {"get": {"description": action.description}}
|
|
37
|
+
action_schema = result["paths"][action.path]["get"]
|
|
38
|
+
if len(action.parameters):
|
|
39
|
+
action_schema["parameters"] = []
|
|
40
|
+
for parameter in action.parameters:
|
|
41
|
+
action_schema["parameters"].append(
|
|
42
|
+
self._action_argument_description(action, parameter)
|
|
43
|
+
)
|
|
44
|
+
action_schema["responses"] = {
|
|
45
|
+
"200": {"description": "successful operation", "content": {}}
|
|
46
|
+
}
|
|
47
|
+
for response in action.responses:
|
|
48
|
+
action_schema["responses"]["200"]["content"].update(
|
|
49
|
+
self._action_reponse_description(response)
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
# components
|
|
53
|
+
result["components"] = {"schemas": {}}
|
|
54
|
+
for _type in self.db._schema.objects.values():
|
|
55
|
+
result["components"]["schemas"][_type.name] = self._object_schema(_type)
|
|
56
|
+
|
|
57
|
+
return result
|
|
58
|
+
|
|
59
|
+
def _action_argument_description(self, action, parameter):
|
|
60
|
+
"""Return the OpenAPI description of a DBActionArgument."""
|
|
61
|
+
result = {
|
|
62
|
+
"name": parameter.name,
|
|
63
|
+
"in": "path" if action.inpath(parameter) else "query",
|
|
64
|
+
"description": parameter.description,
|
|
65
|
+
"required": parameter.required or action.inpath(parameter),
|
|
66
|
+
}
|
|
67
|
+
if parameter.nargs == 0:
|
|
68
|
+
result.update({"schema": {}, "allowEmptyValue": True})
|
|
69
|
+
elif parameter.nargs == "*":
|
|
70
|
+
result.update(
|
|
71
|
+
{
|
|
72
|
+
"schema": {
|
|
73
|
+
"type": "array",
|
|
74
|
+
"items": {"type": "string"},
|
|
75
|
+
},
|
|
76
|
+
"style": "form",
|
|
77
|
+
"explode": False,
|
|
78
|
+
}
|
|
79
|
+
)
|
|
80
|
+
else:
|
|
81
|
+
result.update(
|
|
82
|
+
{
|
|
83
|
+
"schema": {
|
|
84
|
+
"type": "string",
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
)
|
|
88
|
+
if parameter.default is not None:
|
|
89
|
+
result["schema"].update({"default": parameter.default})
|
|
90
|
+
if parameter.choices is not None:
|
|
91
|
+
result["schema"].update({"enum": parameter.choices})
|
|
92
|
+
return result
|
|
93
|
+
|
|
94
|
+
def _action_reponse_description(self, response):
|
|
95
|
+
"""Return the OpenAPI description of a DBActionResponse with its content."""
|
|
96
|
+
if response.binary:
|
|
97
|
+
return {
|
|
98
|
+
response.mimetype: {"schema": {"type": "string", "format": "binary"}}
|
|
99
|
+
}
|
|
100
|
+
elif response.object is not None:
|
|
101
|
+
return {
|
|
102
|
+
response.mimetype: {
|
|
103
|
+
"schema": {
|
|
104
|
+
"type": "array",
|
|
105
|
+
"items": {"$ref": f"#/components/schemas/{response.object}"},
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
else:
|
|
110
|
+
return {response.mimetype: {"schema": {"type": "string"}}}
|
|
111
|
+
|
|
112
|
+
def _object_schema(self, _type):
|
|
113
|
+
"""Return the OpenAPI schema corresponding to an object."""
|
|
114
|
+
result = {
|
|
115
|
+
"type": "object",
|
|
116
|
+
"properties": {},
|
|
117
|
+
}
|
|
118
|
+
if _type.description is not None:
|
|
119
|
+
result["description"] = _type.description
|
|
120
|
+
for prop in _type.properties:
|
|
121
|
+
result["properties"][prop.name] = self._property_schema(prop)
|
|
122
|
+
return result
|
|
123
|
+
|
|
124
|
+
def _property_example(self, prop):
|
|
125
|
+
"""Return property example. If the property is a defined type, the example value
|
|
126
|
+
in schema is parsed with the defined type. If the property is not defined with
|
|
127
|
+
an example in schema, it checks whether the property a reference or a back
|
|
128
|
+
reference and tries to retrieve the example value on this referenced
|
|
129
|
+
property."""
|
|
130
|
+
if prop.example is not None:
|
|
131
|
+
if isinstance(prop.type, SchemaDefinedType):
|
|
132
|
+
return prop.type.parse(prop.example)
|
|
133
|
+
else:
|
|
134
|
+
return prop.example
|
|
135
|
+
else:
|
|
136
|
+
if isinstance(prop.type, SchemaReference):
|
|
137
|
+
return self._property_example(prop.type.obj.prop(prop.type.prop))
|
|
138
|
+
elif (
|
|
139
|
+
isinstance(prop.type, SchemaBackReference)
|
|
140
|
+
and prop.type.prop is not None
|
|
141
|
+
):
|
|
142
|
+
return self._property_example(prop.type.obj.prop(prop.type.prop))
|
|
143
|
+
return None
|
|
144
|
+
|
|
145
|
+
def _native_schema(self, property_type):
|
|
146
|
+
"""Convert native type into OpenAPI schema."""
|
|
147
|
+
if property_type is str:
|
|
148
|
+
return {"type": "string"}
|
|
149
|
+
elif property_type is int:
|
|
150
|
+
return {"type": "integer"}
|
|
151
|
+
elif property_type is float:
|
|
152
|
+
return {"type": "number"}
|
|
153
|
+
elif property_type is bool:
|
|
154
|
+
return {"type": "boolean"}
|
|
155
|
+
|
|
156
|
+
def _property_schema(self, prop):
|
|
157
|
+
"""Return the OpenAPI schema of an object property, depending on its type."""
|
|
158
|
+
result = {}
|
|
159
|
+
# description
|
|
160
|
+
result["description"] = prop.description
|
|
161
|
+
# example if defined
|
|
162
|
+
example = self._property_example(prop)
|
|
163
|
+
if example is not None:
|
|
164
|
+
result["example"] = example
|
|
165
|
+
|
|
166
|
+
# schema depending on property type
|
|
167
|
+
if isinstance(prop.type, SchemaObject):
|
|
168
|
+
result.update(
|
|
169
|
+
{
|
|
170
|
+
"$ref": f"#/components/schemas/{prop.type.name}",
|
|
171
|
+
}
|
|
172
|
+
)
|
|
173
|
+
elif isinstance(prop.type, SchemaExpandable):
|
|
174
|
+
result.update({"type": "string"})
|
|
175
|
+
elif isinstance(prop.type, SchemaRangeId):
|
|
176
|
+
result.update({"type": "integer"})
|
|
177
|
+
elif isinstance(prop.type, SchemaBackReference):
|
|
178
|
+
if prop.type.prop is None:
|
|
179
|
+
result.update({"$ref": f"#/components/schemas/{prop.type.obj.name}"})
|
|
180
|
+
else:
|
|
181
|
+
result.update(self._property_schema(prop.type.obj.prop(prop.type.prop)))
|
|
182
|
+
elif isinstance(prop.type, SchemaReference):
|
|
183
|
+
result.update(self._property_schema(prop.type.obj.prop(prop.type.prop)))
|
|
184
|
+
elif isinstance(prop.type, SchemaContainerList):
|
|
185
|
+
if isinstance(prop.type.content, SchemaObject):
|
|
186
|
+
content = {"$ref": f"#/components/schemas/{prop.type.content.name}"}
|
|
187
|
+
else:
|
|
188
|
+
content = self._native_schema(prop.type.content.native)
|
|
189
|
+
result.update(
|
|
190
|
+
{
|
|
191
|
+
"type": "array",
|
|
192
|
+
"items": content,
|
|
193
|
+
}
|
|
194
|
+
)
|
|
195
|
+
elif isinstance(prop.type, SchemaDefinedType):
|
|
196
|
+
result.update(self._native_schema(prop.type.native))
|
|
197
|
+
else:
|
|
198
|
+
result.update(self._native_schema(prop.type.native))
|
|
199
|
+
return result
|