Skip to content

Commit 6fcf8f6

Browse files
author
Richard P. Field III
authored
fix: Prevent duplicate task keys in FindByIdsAsync (#233)
1 parent 182851f commit 6fcf8f6

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Redis.OM/Searching/RedisCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ public T Single(Expression<Func<T, bool>> expression)
465465
public async Task<IDictionary<string, T?>> FindByIdsAsync(IEnumerable<string> ids)
466466
{
467467
var tasks = new Dictionary<string, Task<T?>>();
468-
foreach (var id in ids)
468+
foreach (var id in ids.Distinct())
469469
{
470470
tasks.Add(id, FindByIdAsync(id));
471471
}

test/Redis.OM.Unit.Tests/RediSearchTests/SearchFunctionalTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,31 @@ public async Task FindByIdsAsyncIds()
693693
Assert.Equal(37, people[ids[1]].Age);
694694
}
695695

696+
[Fact]
697+
public async Task FindByIdsAsyncIdsWithDuplicatedIds()
698+
{
699+
var person1 = new Person() {Name = "Alice", Age = 51};
700+
var person2 = new Person() {Name = "Bob", Age = 37};
701+
702+
var collection = new RedisCollection<Person>(_connection);
703+
704+
await collection.InsertAsync(person1);
705+
await collection.InsertAsync(person2);
706+
707+
var ids = new string[] {person1.Id, person2.Id, person1.Id, person2.Id};
708+
709+
var people = await collection.FindByIdsAsync(ids);
710+
Assert.NotNull(people[ids[0]]);
711+
Assert.Equal(ids[0], people[ids[0]].Id);
712+
Assert.Equal("Alice", people[ids[0]].Name);
713+
Assert.Equal(51, people[ids[0]].Age);
714+
715+
Assert.NotNull(people[ids[1]]);
716+
Assert.Equal(ids[1], people[ids[1]].Id);
717+
Assert.Equal("Bob", people[ids[1]].Name);
718+
Assert.Equal(37, people[ids[1]].Age);
719+
}
720+
696721
[Fact]
697722
public async Task FindByIdsAsyncKeys()
698723
{

0 commit comments

Comments
 (0)