Skip to content

Commit 60021d9

Browse files
Stuart Camrusscam
authored andcommitted
Fix ActionIds type (#3610)
Ensure GetHashcode() returns same value for out of order lists. (cherry picked from commit c1a705a)
1 parent 8c78367 commit 60021d9

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

src/Nest/CommonAbstractions/Infer/ActionIds/ActionIds.cs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,48 @@ public class ActionIds : IUrlParameter, IEquatable<ActionIds>
1111
{
1212
private readonly List<string> _actionIds;
1313

14-
public ActionIds(IEnumerable<string> actionIds) => _actionIds = actionIds?.ToList() ?? new List<string>();
14+
public ActionIds(IEnumerable<string> actionIds) => _actionIds = actionIds?.ToList();
1515

16-
public ActionIds(string actionIds) => _actionIds = actionIds.IsNullOrEmpty()
17-
? new List<string>()
18-
: actionIds.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
19-
.Select(s => s.Trim())
20-
.ToList();
21-
22-
internal IReadOnlyList<string> Ids => _actionIds;
16+
public ActionIds(string actionIds)
17+
{
18+
if (!actionIds.IsNullOrEmptyCommaSeparatedList(out var arr))
19+
_actionIds = arr.ToList();
20+
}
2321

2422
private string DebugDisplay => ((IUrlParameter)this).GetString(null);
2523

2624
public bool Equals(ActionIds other)
2725
{
28-
if (Ids == null && other.Ids == null) return true;
29-
if (Ids == null || other.Ids == null) return false;
26+
if (other == null) return false;
27+
if (_actionIds == null && other._actionIds == null) return true;
28+
if (_actionIds == null || other._actionIds == null) return false;
3029

31-
return Ids.Count == other.Ids.Count && !Ids.Except(other.Ids).Any();
30+
return _actionIds.Count == other._actionIds.Count &&
31+
_actionIds.OrderBy(id => id).SequenceEqual(other._actionIds.OrderBy(id => id));
3232
}
3333

34-
string IUrlParameter.GetString(IConnectionConfigurationValues settings) => string.Join(",", _actionIds);
34+
string IUrlParameter.GetString(IConnectionConfigurationValues settings) =>
35+
string.Join(",", _actionIds ?? Enumerable.Empty<string>());
3536

3637
public static implicit operator ActionIds(string actionIds) =>
37-
actionIds.IsNullOrEmptyCommaSeparatedList(out var list) ? null : new ActionIds(list);
38+
actionIds.IsNullOrEmptyCommaSeparatedList(out var arr) ? null : new ActionIds(arr);
3839

39-
public static implicit operator ActionIds(string[] actionIds) => actionIds.IsEmpty() ? null : new ActionIds(actionIds);
40+
public static implicit operator ActionIds(string[] actionIds) =>
41+
actionIds.IsEmpty() ? null : new ActionIds(actionIds);
4042

4143
public override bool Equals(object obj) => obj is ActionIds other && Equals(other);
4244

43-
public override int GetHashCode() => _actionIds.GetHashCode();
45+
public override int GetHashCode()
46+
{
47+
if (_actionIds == null) return 0;
48+
unchecked
49+
{
50+
var hc = 0;
51+
foreach (var id in _actionIds.OrderBy(id => id))
52+
hc = hc * 17 + id.GetHashCode();
53+
return hc;
54+
}
55+
}
4456

4557
public static bool operator ==(ActionIds left, ActionIds right) => Equals(left, right);
4658

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Elastic.Xunit.XunitPlumbing;
2+
using FluentAssertions;
3+
using Nest;
4+
5+
namespace Tests.CommonOptions
6+
{
7+
public class ActionIdsTests
8+
{
9+
[U] public void Equal()
10+
{
11+
var actionIds1 = new ActionIds("1,2,3");
12+
var actionIds2 = new ActionIds(new [] { "3", "2", "1" });
13+
14+
actionIds1.Should().Be(actionIds2);
15+
actionIds1.GetHashCode().Should().Be(actionIds2.GetHashCode());
16+
}
17+
18+
[U] public void NotEqual()
19+
{
20+
var actionIds1 = new ActionIds("1,2,3,3");
21+
var actionIds2 = new ActionIds(new [] { "3", "2", "1" });
22+
23+
actionIds1.Should().NotBe(actionIds2);
24+
actionIds1.GetHashCode().Should().NotBe(actionIds2.GetHashCode());
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)