fix: detect circular references in GetDescendants (#113672)
* fix: detect circular references in GetDescendants * chore: use map[string]bool and instantiate at the beginning of the function
This commit is contained in:
@@ -439,7 +439,10 @@ func (ss *FolderUnifiedStoreImpl) GetDescendants(ctx context.Context, orgID int6
|
||||
}
|
||||
|
||||
descendantsMap := map[string]*folder.Folder{}
|
||||
getDescendants(nodes, tree, ancestor_uid, descendantsMap)
|
||||
err = getDescendants(nodes, tree, ancestor_uid, descendantsMap, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
descendants := []*folder.Folder{}
|
||||
for _, f := range descendantsMap {
|
||||
@@ -449,11 +452,27 @@ func (ss *FolderUnifiedStoreImpl) GetDescendants(ctx context.Context, orgID int6
|
||||
return descendants, nil
|
||||
}
|
||||
|
||||
func getDescendants(nodes map[string]*folder.Folder, tree map[string]map[string]*folder.Folder, ancestor_uid string, descendantsMap map[string]*folder.Folder) {
|
||||
for uid := range tree[ancestor_uid] {
|
||||
descendantsMap[uid] = nodes[uid]
|
||||
getDescendants(nodes, tree, uid, descendantsMap)
|
||||
func getDescendants(
|
||||
nodes map[string]*folder.Folder,
|
||||
tree map[string]map[string]*folder.Folder,
|
||||
ancestorUID string,
|
||||
descendantsMap map[string]*folder.Folder,
|
||||
seen map[string]bool,
|
||||
) error {
|
||||
if seen == nil {
|
||||
seen = map[string]bool{}
|
||||
}
|
||||
if seen[ancestorUID] {
|
||||
return folder.ErrCircularReference.Errorf("circular reference detected at folder uid: %s", ancestorUID)
|
||||
}
|
||||
seen[ancestorUID] = true
|
||||
for uid := range tree[ancestorUID] {
|
||||
descendantsMap[uid] = nodes[uid]
|
||||
if err := getDescendants(nodes, tree, uid, descendantsMap, seen); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ss *FolderUnifiedStoreImpl) CountFolderContent(ctx context.Context, orgID int64, ancestor_uid string) (folder.DescendantCounts, error) {
|
||||
|
||||
Reference in New Issue
Block a user