Skip to content

Commit 076f2ef

Browse files
committed
Fix array based conversion of display labels for obsolete format
1 parent e1bb469 commit 076f2ef

File tree

2 files changed

+123
-7
lines changed

2 files changed

+123
-7
lines changed

backend/infrahub/core/schema/schema_branch.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -793,17 +793,17 @@ def validate_display_label(self) -> None:
793793
if len(node_schema.display_labels) == 1:
794794
# If the previous display_labels consist of a single attribute convert
795795
# it to an attribute based display label
796-
converted_display_label = node_schema.display_labels[0]
797-
if "__" not in converted_display_label:
798-
# Previously we allowed defining a raw attribute name as a component of a
799-
# display_label, if this is the case we need to append '__value'
800-
converted_display_label = f"{converted_display_label}__value"
801-
update_candidate.display_label = converted_display_label
796+
update_candidate.display_label = _format_display_label_component(
797+
component=node_schema.display_labels[0]
798+
)
802799
else:
803800
# If the previous display label consists of multiple attributes
804801
# convert it to a Jinja2 based display label
805802
update_candidate.display_label = " ".join(
806-
[f"{{{{ {display_label} }}}}" for display_label in node_schema.display_labels]
803+
[
804+
f"{{{{ {_format_display_label_component(component=display_label)} }}}}"
805+
for display_label in node_schema.display_labels
806+
]
807807
)
808808
self.set(name=name, schema=update_candidate)
809809

@@ -2592,3 +2592,16 @@ def manage_object_template_schemas(self) -> None:
25922592
updated_used_by_node = set(chain(template_schema_kinds, set(core_node_schema.used_by)))
25932593
core_node_schema.used_by = sorted(updated_used_by_node)
25942594
self.set(name=InfrahubKind.NODE, schema=core_node_schema)
2595+
2596+
2597+
def _format_display_label_component(component: str) -> str:
2598+
"""Return correct format for display_label.
2599+
2600+
Previously both the format of 'name' and 'name__value' was
2601+
supported this function ensures that the proper 'name__value'
2602+
format is used
2603+
"""
2604+
if "__" in component:
2605+
return component
2606+
2607+
return f"{component}__value"
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
from dataclasses import dataclass
2+
3+
import pytest
4+
5+
from infrahub.core.schema import AttributeSchema, NodeSchema, SchemaRoot
6+
from infrahub.core.schema.schema_branch import SchemaBranch
7+
8+
9+
@dataclass
10+
class DisplayLabelTestCase:
11+
name: str
12+
schema_root: SchemaRoot
13+
display_label: str
14+
15+
16+
DISPLAY_LABEL_TEST_CASES: list[DisplayLabelTestCase] = [
17+
DisplayLabelTestCase(
18+
name="single_attribute_label_no_value",
19+
schema_root=SchemaRoot(
20+
nodes=[
21+
NodeSchema(
22+
name="Widget",
23+
namespace="Test",
24+
attributes=[AttributeSchema(name="name", kind="Text"), AttributeSchema(name="status", kind="Text")],
25+
display_labels=["name"],
26+
)
27+
]
28+
),
29+
display_label="name__value",
30+
),
31+
DisplayLabelTestCase(
32+
name="single_attribute_label_with_value",
33+
schema_root=SchemaRoot(
34+
nodes=[
35+
NodeSchema(
36+
name="Widget",
37+
namespace="Test",
38+
attributes=[AttributeSchema(name="name", kind="Text"), AttributeSchema(name="status", kind="Text")],
39+
display_labels=["name__value"],
40+
)
41+
]
42+
),
43+
display_label="name__value",
44+
),
45+
DisplayLabelTestCase(
46+
name="dual_attribute_label_no_value",
47+
schema_root=SchemaRoot(
48+
nodes=[
49+
NodeSchema(
50+
name="Widget",
51+
namespace="Test",
52+
attributes=[AttributeSchema(name="name", kind="Text"), AttributeSchema(name="status", kind="Text")],
53+
display_labels=["name", "status"],
54+
)
55+
]
56+
),
57+
display_label="{{ name__value }} {{ status__value }}",
58+
),
59+
DisplayLabelTestCase(
60+
name="dual_attribute_label_mixed_value",
61+
schema_root=SchemaRoot(
62+
nodes=[
63+
NodeSchema(
64+
name="Widget",
65+
namespace="Test",
66+
attributes=[AttributeSchema(name="name", kind="Text"), AttributeSchema(name="status", kind="Text")],
67+
display_labels=["name__value", "status"],
68+
)
69+
]
70+
),
71+
display_label="{{ name__value }} {{ status__value }}",
72+
),
73+
DisplayLabelTestCase(
74+
name="defined_display_label",
75+
schema_root=SchemaRoot(
76+
nodes=[
77+
NodeSchema(
78+
name="Widget",
79+
namespace="Test",
80+
attributes=[AttributeSchema(name="name", kind="Text"), AttributeSchema(name="status", kind="Text")],
81+
display_labels=["name__value", "status"],
82+
display_label="{{ name__value|upper }}: {{ status__value|lower }}",
83+
)
84+
]
85+
),
86+
display_label="{{ name__value|upper }}: {{ status__value|lower }}",
87+
),
88+
]
89+
90+
91+
@pytest.mark.parametrize(
92+
"test_case",
93+
[pytest.param(tc, id=tc.name) for tc in DISPLAY_LABEL_TEST_CASES],
94+
)
95+
async def test_expected_final_display_label(
96+
test_case: DisplayLabelTestCase,
97+
) -> None:
98+
"""Test that the final computed display label matches the expected value."""
99+
schema_branch = SchemaBranch(cache={}, name="test")
100+
schema_branch.load_schema(schema=test_case.schema_root)
101+
schema_branch.process()
102+
node = schema_branch.get_node(name="TestWidget", duplicate=False)
103+
assert node.display_label == test_case.display_label

0 commit comments

Comments
 (0)