Skip to content

Commit 14887b6

Browse files
authored
[core][feat] Add kind description in multi tenant mode (#2204)
1 parent 634d527 commit 14887b6

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

fixcore/fixcore/model/exportable_model.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def json_export_simple_schema(
1919
with_properties: bool = True,
2020
with_relatives: bool = True,
2121
with_metadata: bool = True,
22-
aggregate_roots_only: bool = False,
2322
) -> List[Json]:
2423
def export_simple(kind: SimpleKind) -> Json:
2524
result = kind.as_json()

fixcore/fixcore/model/model_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def code_model() -> Model:
256256
The model is only loaded on demand and only once.
257257
"""
258258
load_plugin_classes()
259-
return Model.from_kinds([from_js(m, Kind) for m in export_model()]) # type: ignore
259+
return Model.from_kinds([from_js(m, Kind) for m in export_model(with_kind_description=True)]) # type: ignore
260260

261261

262262
class ModelHandlerFromCodeAndDB(ModelHandlerDB):

fixlib/fixlib/core/model_export.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ def dataclasses_to_fixcore_model(
150150
aggregate_root: Optional[Type[Any]] = None,
151151
walk_subclasses: bool = True,
152152
use_optional_as_required: bool = False,
153-
with_description: bool = True,
153+
with_kind_description: bool = False,
154+
with_prop_description: bool = False,
154155
) -> List[Json]:
155156
"""
156157
Analyze all transitive dataclasses and create the model
@@ -163,7 +164,8 @@ def dataclasses_to_fixcore_model(
163164
:param aggregate_root: if a type is a subtype of this type, it will be considered an aggregate root.
164165
:param walk_subclasses: if true, all subclasses of the given classes will be analyzed as well.
165166
:param use_optional_as_required: if true, all non-optional fields will be considered required.
166-
:param with_description: if true, include the description for classes and properties.
167+
:param with_kind_description: if true, include the description for classes.
168+
:param with_prop_description: if true, include the description for properties.
167169
:return: the model definition in the fixcore json format.
168170
"""
169171

@@ -176,7 +178,7 @@ def prop(field: Attribute) -> List[Json]: # type: ignore
176178
meta = field.metadata.copy()
177179
kind = meta.pop("type_hint", model_name(field.type))
178180
desc = meta.pop("description", None)
179-
desc = desc if with_description else None
181+
desc = desc if with_prop_description else None
180182
required = meta.pop("required", use_optional_as_required and not is_optional(field.type)) # type: ignore
181183
synthetic = meta.pop("synthetic", None)
182184
synthetic = synthetic if synthetic else {}
@@ -253,7 +255,13 @@ def export_data_class(clazz: type) -> None:
253255
metadata["service"] = s
254256
if (slc := getattr(clazz, "categories", None)) and callable(slc) and (sl := slc()):
255257
metadata["categories"] = sl
256-
if with_description and (s := clazz.__dict__.get("kind_description", None)) and isinstance(s, str):
258+
if ( # only export kind description on aggregate roots
259+
with_kind_description
260+
and (ar := aggregate_root)
261+
and issubclass(clazz, ar)
262+
and (s := clazz.__dict__.get("kind_description", None))
263+
and isinstance(s, str)
264+
):
257265
metadata["description"] = s
258266

259267
model.append(
@@ -293,9 +301,9 @@ def literal_name(en: Enum) -> str:
293301
# Use this model exporter, if a dynamic object is exported
294302
# with given name and properties.
295303
def dynamic_object_to_fixcore_model(
296-
name: str, properties: Dict[str, type], aggregate_root: bool = True, traverse_dependant: bool = True
304+
name: str, properties: Dict[str, type], aggregate_root: bool = True, traverse_dependant: bool = True, **kwargs: Any
297305
) -> List[Json]:
298-
dependant = dataclasses_to_fixcore_model(set(properties.values())) if traverse_dependant else []
306+
dependant = dataclasses_to_fixcore_model(set(properties.values()), **kwargs) if traverse_dependant else []
299307
# append definition for top level object
300308
dependant.append(
301309
{

fixlib/fixlib/graph/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def export_model(graph: Optional[Graph] = None, **kwargs: Any) -> List[Json]:
398398
for node in graph.nodes:
399399
classes.add(type(node))
400400

401-
model = resource_classes_to_fixcore_model(classes, aggregate_root=BaseResource, with_description=False, **kwargs)
401+
model = resource_classes_to_fixcore_model(classes, aggregate_root=BaseResource, **kwargs)
402402
for resource_model in model:
403403
if resource_model.get("fqn") == "resource":
404404
resource_model.get("properties", []).append(

fixlib/test/core/model_export_test.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def test_enum_to_model() -> None:
125125

126126

127127
def test_dataclasses_to_fixcore_model() -> None:
128-
result = dataclasses_to_fixcore_model({DataClassExample})
128+
result = dataclasses_to_fixcore_model({DataClassExample}, with_kind_description=True, with_prop_description=True)
129129
assert len(result) == 5
130130
for r in result:
131131
props = {p["name"]: p for p in r.get("properties", [])}
@@ -187,7 +187,12 @@ class GcpTestConfigConfig:
187187
def test_config_export():
188188
# Let's assume a dynamic top level object of name Config
189189
# The properties are defined by name and related type.
190-
result = dynamic_object_to_fixcore_model("config", {"aws": AwsTestConfig, "gcp": GcpTestConfigConfig})
190+
result = dynamic_object_to_fixcore_model(
191+
"config",
192+
{"aws": AwsTestConfig, "gcp": GcpTestConfigConfig},
193+
with_kind_description=True,
194+
with_prop_description=True,
195+
)
191196
result_dict = {a["fqn"]: a for a in result}
192197
assert len(result_dict["gcp_config"]["properties"]) == 1
193198
assert len(result_dict["aws_config"]["properties"]) == 2

0 commit comments

Comments
 (0)