Skip to content

Commit 72b0c36

Browse files
committed
Fix duplicate key exception when building user mapping data
1 parent 47d7f25 commit 72b0c36

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/MigrationTools.Clients.TfsObjectModel/Processors/TfsExportUsersForMappingProcessor.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Linq;
@@ -67,8 +67,13 @@ protected override void InternalExecute()
6767

6868
usersToMap = usersToMap.Where(x => x.Source.FriendlyName != x.Target?.FriendlyName).ToList();
6969
Log.LogInformation("Filtered to {usersToMap} total viable mappings", usersToMap.Count);
70-
Dictionary<string, string> usermappings = usersToMap.ToDictionary(x => x.Source.FriendlyName, x => x.Target?.FriendlyName);
71-
System.IO.File.WriteAllText(CommonTools.UserMapping.Options.UserMappingFile, Newtonsoft.Json.JsonConvert.SerializeObject(usermappings, Formatting.Indented));
70+
Dictionary<string, string> usermappings = [];
71+
foreach (IdentityMapData userMapping in usersToMap)
72+
{
73+
// We cannot use ToDictionary(), because there can be multiple users with the same friendly name and so
74+
// it would throw with duplicate key. This way we just overwrite the value – last item in source wins.
75+
usermappings[userMapping.Source.FriendlyName] = userMapping.Target?.FriendlyName;
76+
}
7277
System.IO.File.WriteAllText(CommonTools.UserMapping.Options.UserMappingFile, JsonConvert.SerializeObject(usermappings, Formatting.Indented));
7378
Log.LogInformation("Writen to: {LocalExportJsonFile}", CommonTools.UserMapping.Options.UserMappingFile);
7479

0 commit comments

Comments
 (0)