mirror of
https://github.com/czlonkowski/n8n-mcp.git
synced 2026-02-09 06:43:08 +00:00
fix: preserve array indices in multi-output nodes when removing connections
CRITICAL BUG FIX: Fixed array index corruption in multi-output nodes (Switch, IF with multiple handlers, Merge) when rewiring connections. Problem: - applyRemoveConnection() filtered out empty arrays after removing connections - This caused indices to shift in multi-output nodes - Example: Switch.main = [[H0], [H1], [H2]] -> remove H1 -> [[H0], [H2]] - H2 moved from index 2 to index 1, corrupting workflow structure Root Cause: ```typescript // Line 697 - BUGGY CODE: workflow.connections[node][output] = connections.filter(conns => conns.length > 0); ``` Solution: - Only remove trailing empty arrays - Preserve intermediate empty arrays to maintain index integrity - Example: [[H0], [], [H2]] stays [[H0], [], [H2]] not [[H0], [H2]] Impact: - Prevents production-breaking workflow corruption - Fixes rewireConnection operation for multi-output nodes - Critical for AI agents working with complex workflows Testing: - Added integration test for Switch node rewiring with array index verification - Test creates 4-output Switch node, rewires middle connection - Verifies indices 0, 2, 3 unchanged after rewiring index 1 - All 137 unit tests + 12 integration tests passing Discovered by: @agent-n8n-mcp-tester during comprehensive testing Issue: #272 (Connection Operations - Phase 1) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -691,12 +691,14 @@ export class WorkflowDiffEngine {
|
||||
workflow.connections[sourceNode.name][sourceOutput] = connections.map(conns =>
|
||||
conns.filter(conn => conn.node !== targetNode.name)
|
||||
);
|
||||
|
||||
// Clean up empty arrays
|
||||
workflow.connections[sourceNode.name][sourceOutput] =
|
||||
workflow.connections[sourceNode.name][sourceOutput].filter(conns => conns.length > 0);
|
||||
|
||||
if (workflow.connections[sourceNode.name][sourceOutput].length === 0) {
|
||||
|
||||
// Remove trailing empty arrays only (preserve intermediate empty arrays to maintain indices)
|
||||
const outputConnections = workflow.connections[sourceNode.name][sourceOutput];
|
||||
while (outputConnections.length > 0 && outputConnections[outputConnections.length - 1].length === 0) {
|
||||
outputConnections.pop();
|
||||
}
|
||||
|
||||
if (outputConnections.length === 0) {
|
||||
delete workflow.connections[sourceNode.name][sourceOutput];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user