Skip to content

Commit

Permalink
DYN-7946:PM - crash fix when removing custom nodes with identical nam…
Browse files Browse the repository at this point in the history
…es (#15694)
  • Loading branch information
dnenov authored Dec 3, 2024
1 parent f2db372 commit 25cd2ae
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2101,7 +2101,7 @@ private void RemoveItemRecursively(PackageItemRootViewModel packageItemRootViewM
}
}

private void RemoveSingleItem(PackageItemRootViewModel vm, DependencyType fileType)
internal void RemoveSingleItem(PackageItemRootViewModel vm, DependencyType fileType)
{
var fileName = vm.DisplayName;

Expand All @@ -2120,9 +2120,26 @@ private void RemoveSingleItem(PackageItemRootViewModel vm, DependencyType fileTy
else if (fileType.Equals(DependencyType.CustomNode) || fileType.Equals(DependencyType.CustomNodePreview))
{
fileName = Path.GetFileNameWithoutExtension(fileName);
CustomNodeDefinitions.Remove(CustomNodeDefinitions
.First(x => x.DisplayName == fileName));

// We allow multiple .dyf files with identical node names to be loaded at once
// We use the node Namespace as a prefix ([Namespace].[Node Name]) to allow for that
string[] nameVariations = {
fileName,
fileName.Replace(".", ""), // Edge case where the actual Display Name as the '.' removed
fileName.Contains('.') ? fileName.Split('.')[1] : fileName // Edge case for the .dyf files that were added first
};

foreach (var variation in nameVariations)
{
var customNode = CustomNodeDefinitions.FirstOrDefault(x => x.DisplayName == variation);
if (customNode != null)
{
CustomNodeDefinitions.Remove(customNode);
break; // Exit loop once found and removed
}
}

// Find and remove the corresponding key in CustomDyfFilepaths
var keyToRemove = CustomDyfFilepaths.Keys
.FirstOrDefault(k => Path.GetFileNameWithoutExtension(k) == fileName);

Expand Down
33 changes: 33 additions & 0 deletions test/DynamoCoreWpfTests/PublishPackageViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using Dynamo;
using Dynamo.Graph.Nodes;
using Dynamo.Graph.Nodes.CustomNodes;
using Dynamo.Graph.Workspaces;
using Dynamo.PackageManager;
Expand Down Expand Up @@ -77,6 +78,38 @@ public void SetsErrorState()

}

[Test]
public void CanRemoveCustomNodesWithIdenticalNames()
{
var vm = new PublishPackageViewModel(this.ViewModel);

// Arrange
var customNode1 = new CustomNodeDefinition(Guid.NewGuid(), "Geometry.Curve", new List<NodeModel>());
var customNode2 = new CustomNodeDefinition(Guid.NewGuid(), "Building.Curve", new List<NodeModel>());
var customNode3 = new CustomNodeDefinition(Guid.NewGuid(), "Curve", new List<NodeModel>());

vm.CustomNodeDefinitions.Add(customNode1);
vm.CustomNodeDefinitions.Add(customNode2);
vm.CustomNodeDefinitions.Add(customNode3);

// Initial Assert
Assert.AreEqual(3, vm.CustomNodeDefinitions.Count);

var item1 = new PackageItemRootViewModel("Geometry.Curve.dyf", "C:\\test\\Geometry.Curve.dyf");
var item2 = new PackageItemRootViewModel("Building.Curve.dyf", "C:\\test\\Building.Curve.dyf");
var item3 = new PackageItemRootViewModel("Curve.dyf", "C:\\test\\Curve.dyf");

// Assert
vm.RemoveSingleItem(item1, DependencyType.CustomNodePreview);
Assert.AreEqual(2, vm.CustomNodeDefinitions.Count);

vm.RemoveSingleItem(item2, DependencyType.CustomNodePreview);
Assert.AreEqual(1, vm.CustomNodeDefinitions.Count);

vm.RemoveSingleItem(item3, DependencyType.CustomNodePreview);
Assert.AreEqual(0, vm.CustomNodeDefinitions.Count);
}

[Test]
public void CanPublishLateInitializedJsonCustomNode()
{
Expand Down

0 comments on commit 25cd2ae

Please sign in to comment.