@@ -2135,28 +2135,33 @@ def add_groups(self) -> None:
21352135
21362136 # On auto-generated templates, also expose template-side fields whose peers drive instance group membership at template application time
21372137 if isinstance (schema , TemplateSchema ):
2138- schema , changed = self ._add_template_group_for_instances_relationships (schema = schema , changed = changed )
2138+ schema , changed = self ._add_template_group_for_instances_relationships (
2139+ schema = schema , schema_duplicated = changed
2140+ )
21392141
21402142 if changed :
21412143 self .set (name = node_name , schema = schema )
21422144
21432145 def _add_template_group_for_instances_relationships (
2144- self , schema : MainSchemaTypes , changed : bool
2146+ self , schema : MainSchemaTypes , schema_duplicated : bool
21452147 ) -> tuple [MainSchemaTypes , bool ]:
21462148 """Append `member_of_groups_for_instances` / `subscriber_of_groups_for_instances` on a template.
21472149
2148- These are generic relationships using distinct identifiers so adding peers does not put the template node into
2149- a group.
2150+ These are generic relationships using distinct identifiers so adding peers does not put the template object
2151+ into a group.
2152+
2153+ `schema_duplicated` reflects whether the caller has already duplicated `schema`. The helper duplicates on
2154+ first mutation if needed and returns the updated state so the caller knows whether anything was changed.
21502155 """
21512156 for rel_name , identifier in (
21522157 ("member_of_groups_for_instances" , "template_group_member_for_instances" ),
21532158 ("subscriber_of_groups_for_instances" , "template_group_subscriber_for_instances" ),
21542159 ):
21552160 if rel_name in schema .relationship_names :
21562161 continue
2157- if not changed :
2162+ if not schema_duplicated :
21582163 schema = schema .duplicate ()
2159- changed = True
2164+ schema_duplicated = True
21602165 schema .relationships .append (
21612166 RelationshipSchema (
21622167 name = rel_name ,
@@ -2168,7 +2173,7 @@ def _add_template_group_for_instances_relationships(
21682173 branch = BranchSupportType .AWARE ,
21692174 )
21702175 )
2171- return schema , changed
2176+ return schema , schema_duplicated
21722177
21732178 def _get_hierarchy_child_rel (self , peer : str , hierarchical : str | None , read_only : bool ) -> RelationshipSchema :
21742179 return RelationshipSchema (
0 commit comments