Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbid changing optional property of inherited attributes #4940

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions backend/infrahub/core/schema/node_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ def validate_inheritance(self, interface: GenericSchema) -> None:
raise ValueError(
f"{self.kind}'s attribute {attribute.name} inherited from {interface.kind} cannot be overriden"
)

interface_attr = interface.get_attribute(attribute.name)
# Check existing inherited attribute kind is the same as the incoming inherited attribute
interface_attr_kind = interface.get_attribute(attribute.name).kind
if attribute.kind != interface_attr_kind:
if attribute.kind != interface_attr.kind:
raise ValueError(
f"{self.kind}.{attribute.name} inherited from {interface.namespace}{interface.name} must be the same kind "
f'["{interface_attr_kind}", "{attribute.kind}"]'
f'["{interface_attr.kind}", "{attribute.kind}"]'
)
if attribute.optional != interface_attr.optional:
raise ValueError(
f"{self.kind}.{attribute.name} inherited from {interface.namespace}{interface.name} must have the same value for property "
f'"optional" ["{interface_attr.optional}", "{attribute.optional}"]'
)

for relationship in self.relationships:
Expand Down
20 changes: 20 additions & 0 deletions backend/tests/unit/core/schema_manager/test_manager_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,26 @@ async def test_schema_process_inheritance_different_generic_attribute_types_on_n
assert exc.value.args[0] == 'TestWidget.choice inherited from TestAdapter must be the same kind ["Text", "List"]'


async def test_schema_process_inheritance_different_generic_attribute_optional_on_node(
schema_diff_attr_inheritance_types,
):
"""Test that we raise an exception if a node is inheriting an attribute changing its optional property value."""
schema = SchemaBranch(cache={}, name="test")
schema_new = copy.deepcopy(schema_diff_attr_inheritance_types)
schema_new["generics"].pop()
schema_new["nodes"][0]["inherit_from"].pop()
schema_new["nodes"][0]["attributes"].append({"name": "choice", "kind": "Text", "optional": False})
schema.load_schema(schema=SchemaRoot(**schema_new))

with pytest.raises(ValueError) as exc:
schema.process_inheritance()

assert (
exc.value.args[0]
== 'TestWidget.choice inherited from TestAdapter must have the same value for property "optional" ["True", "False"]'
)


async def test_schema_branch_process_inheritance_node_level(animal_person_schema_dict):
schema = SchemaBranch(cache={}, name="test")
schema.load_schema(schema=SchemaRoot(**animal_person_schema_dict))
Expand Down
Loading