Skip to content

Commit

Permalink
Forbid changing optional property of inherited attributes (#4940)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazoyer authored Nov 15, 2024
1 parent f00a6ae commit 8de74d6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
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
1 change: 1 addition & 0 deletions changelog/4936.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Forbid changing the "optional" property of an inherited attribute to not break GraphQL schema generation

0 comments on commit 8de74d6

Please sign in to comment.