Skip to content

Commit

Permalink
Fix object templates being added in menu twice (#5815)
Browse files Browse the repository at this point in the history
Also fix spam of warning logs due to this issue.
  • Loading branch information
gmazoyer authored Feb 21, 2025
1 parent 43697d2 commit 058e0c4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backend/infrahub/core/node/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ def _get_parent_relationship_name(self) -> str | None:

async def get_object_template(self, db: InfrahubDatabase) -> Node | None:
object_template: RelationshipManager = getattr(self, OBJECT_TEMPLATE_RELATIONSHIP_NAME, None)
return None if not object_template else await object_template.get_peer(db=db)
return await object_template.get_peer(db=db) if object_template is not None else None

def get_relationships(
self, kind: RelationshipKind, exclude: Sequence[str] | None = None
Expand Down
1 change: 1 addition & 0 deletions backend/infrahub/menu/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ async def generate_menu(db: InfrahubDatabase, branch: Branch, menu_items: list[C

if schema.namespace == "Template":
object_templates_menu.children[menu_item.identifier] = menu_item
items_to_add[item_name] = True
elif not schema.menu_placement:
first_element = MenuItemDict.from_schema(model=schema)
first_element.identifier = f"{first_element.identifier}Sub"
Expand Down
42 changes: 42 additions & 0 deletions backend/tests/unit/graphql/test_mutation_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,48 @@ async def test_create_with_object_template(
assert sorted([(await sfp.interface.get_peer(db=db)).name.value for sfp in device_sfps]) == if_names


async def test_create_without_object_template(
db: InfrahubDatabase, default_branch: Branch, register_core_models_schema: SchemaBranch
):
registry.schema.register_schema(schema=DEVICE_SCHEMA, branch=default_branch.name)

query = """
mutation NewDevice($device_name: String!, $manufacturer: String!) {
TestingDeviceCreate(data: {
name: {value: $device_name}
manufacturer: {value: $manufacturer}
height: {value: 1}
weight: {value: 6}
airflow: {value: "Front to rear"}
}) {
ok
object {
id
}
}
}
"""
gql_params = await prepare_graphql_params(db=db, include_subscription=False, branch=default_branch)

result = await graphql(
schema=gql_params.schema,
source=query,
context_value=gql_params.context,
root_value=None,
variable_values={"device_name": "th2.par.asbr01", "manufacturer": "Juniper"},
)
assert not result.errors

device = await NodeManager.get_one(
db=db, kind="TestingDevice", branch=default_branch, id=result.data["TestingDeviceCreate"]["object"]["id"]
)
assert device
assert device.name.value == "th2.par.asbr01"
# Validate object not is linked to object template
device_template_node = await device.object_template.get_peer(db=db)
assert not device_template_node


# These tests have been moved at the end of the file to avoid colliding with other and breaking them


Expand Down

0 comments on commit 058e0c4

Please sign in to comment.