Skip to content

Commit d806e32

Browse files
authored
fix #3116 instances of gethashcode in equals implementation (#3117)
* fix #3116 instances of gethashcode in equals implementation * Equals on types and indices now ignores order and implements == operator
1 parent fc7b358 commit d806e32

File tree

10 files changed

+51
-35
lines changed

10 files changed

+51
-35
lines changed

src/Nest/CommonAbstractions/Infer/IndexName/IndexName.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,13 @@ private bool EqualsString(string other)
9292

9393
private bool EqualsMarker(IndexName other)
9494
{
95-
if (!this.Name.IsNullOrEmpty() && other != null && !other.Name.IsNullOrEmpty())
95+
if (other == null) return false;
96+
if (!this.Name.IsNullOrEmpty() && !other.Name.IsNullOrEmpty())
9697
return EqualsString(PrefixClusterName(other,other.Name));
9798

98-
if (this.Type != null && other != null && other.Type != null)
99-
return this.GetHashCode() == other.GetHashCode();
100-
return false;
99+
if ((!this.Cluster.IsNullOrEmpty() || !other.Cluster.IsNullOrEmpty()) && this.Cluster != other.Cluster) return false;
100+
101+
return this.Type != null && other?.Type != null && this.Type == other.Type;
101102
}
102103

103104
public string GetString(IConnectionConfigurationValues settings)

src/Nest/CommonAbstractions/Infer/Indices/Indices.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings)
7474
all => "_all",
7575
many =>
7676
{
77-
var nestSettings = settings as IConnectionSettingsValues;
78-
if (nestSettings == null)
77+
if (!(settings is IConnectionSettingsValues nestSettings))
7978
throw new Exception("Tried to pass index names on querysting but it could not be resolved because no nest settings are available");
8079

8180
var infer = nestSettings.Inferrer;
@@ -85,19 +84,29 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings)
8584
);
8685
}
8786

87+
public static bool operator ==(Indices left, Indices right) => Equals(left, right);
88+
89+
public static bool operator !=(Indices left, Indices right) => !Equals(left, right);
90+
8891
public override bool Equals(object obj)
8992
{
90-
var other = obj as Indices;
91-
if (other == null) return false;
93+
if (!(obj is Indices other)) return false;
9294
return this.Match(
9395
all => other.Match(a => true, m => false),
9496
many => other.Match(
9597
a => false,
96-
m => this.GetHashCode().Equals(other.GetHashCode())
98+
m => EqualsAllIndices(m.Indices, many.Indices)
9799
)
98100
);
99101
}
100102

103+
private static bool EqualsAllIndices(IReadOnlyList<IndexName> thisIndices, IReadOnlyList<IndexName> otherIndices)
104+
{
105+
if (thisIndices == null && otherIndices == null) return true;
106+
if (thisIndices == null || otherIndices == null) return false;
107+
return thisIndices.Count == otherIndices.Count && !thisIndices.Except(otherIndices).Any();
108+
}
109+
101110
public override int GetHashCode()
102111
{
103112
return this.Match(

src/Nest/CommonAbstractions/Infer/RelationName/RelationName.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ public override bool Equals(object obj)
5353
{
5454
var s = obj as string;
5555
if (!s.IsNullOrEmpty()) return this.EqualsString(s);
56-
var pp = obj as RelationName;
57-
if (pp != null) return this.EqualsMarker(pp);
58-
56+
if (obj is RelationName pp) return this.EqualsMarker(pp);
5957
return base.Equals(obj);
6058
}
6159

@@ -70,8 +68,8 @@ public bool EqualsMarker(RelationName other)
7068
{
7169
if (!this.Name.IsNullOrEmpty() && other != null && !other.Name.IsNullOrEmpty())
7270
return EqualsString(other.Name);
73-
if (this.Type != null && other != null && other.Type != null)
74-
return this.GetHashCode() == other.GetHashCode();
71+
if (this.Type != null && other?.Type != null)
72+
return this.Type == other.Type;
7573
return false;
7674
}
7775

src/Nest/CommonAbstractions/Infer/TypeName/TypeName.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ private bool EqualsMarker(TypeName other)
6666
{
6767
if (!this.Name.IsNullOrEmpty() && other != null && !other.Name.IsNullOrEmpty())
6868
return EqualsString(other.Name);
69-
if (this.Type != null && other != null && other.Type != null)
70-
return this.GetHashCode() == other.GetHashCode();
69+
if (this.Type != null && other?.Type != null)
70+
return this.Type == other.Type;
7171
return false;
7272
}
7373

src/Nest/CommonAbstractions/Infer/Types/Types.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,30 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings)
8585
);
8686

8787
}
88+
public static bool operator ==(Types left, Types right) => Equals(left, right);
89+
90+
public static bool operator !=(Types left, Types right) => !Equals(left, right);
8891

8992
public override bool Equals(object obj)
9093
{
91-
var other = obj as Types;
92-
if (other == null) return false;
94+
if (!(obj is Types other)) return false;
9395
return this.Match(
9496
all => other.Match(a => true, m => false),
9597
many => other.Match(
9698
a => false,
97-
m => this.GetHashCode().Equals(other.GetHashCode())
99+
m => EqualsAllTypes(m.Types, many.Types)
98100
)
99101
);
100102
}
101103

104+
private static bool EqualsAllTypes(IReadOnlyList<TypeName> thisTypes, IReadOnlyList<TypeName> otherTypes)
105+
{
106+
if (thisTypes == null && otherTypes == null) return true;
107+
if (thisTypes == null || otherTypes == null) return false;
108+
if (thisTypes.Count != otherTypes.Count) return false;
109+
return thisTypes.Count == otherTypes.Count && !thisTypes.Except(otherTypes).Any();
110+
}
111+
102112
public override int GetHashCode()
103113
{
104114
return this.Match(

src/Tests/ClientConcepts/HighLevel/Inference/IndexNameInference.doc.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,22 @@ [U] public void EqualsValidation()
170170
{
171171
var clusterIndex = (IndexName)"cluster_one:p";
172172
var index = (IndexName)"p";
173+
Index<Project>("cluster_one").Should().NotBe(Index<Project>("cluster_two"));
173174

174175
clusterIndex.Should().NotBe(index);
175176
clusterIndex.Should().Be("cluster_one:p");
176177
clusterIndex.Should().Be((IndexName)"cluster_one:p");
177178

178179
Index<Project>().Should().Be(Index<Project>());
179180
Index<Project>().Should().NotBe(Index<Project>("cluster_two"));
180-
Index<Project>("cluster_one").Should().NotBe(Index<Project>("cluster_two"));
181181
Index<Project>("cluster_one").Should().NotBe("cluster_one:project");
182182
Index<Project>().Should().NotBe(Index<Developer>());
183183
Index<Project>("cluster_one").Should().NotBe(Index<Developer>("cluster_one"));
184+
185+
Nest.Indices indices1 = "foo,bar";
186+
Nest.Indices indices2 = "bar,foo";
187+
indices1.Should().Be(indices2);
188+
(indices1 == indices2).Should().BeTrue();
184189
}
185190

186191
//hide

src/Tests/ClientConcepts/HighLevel/Inference/TypesAndRelationsInference.doc.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ [U] public void EqualsValidation()
129129

130130
Relation<Project>().Should().Be(Relation<Project>());
131131
Relation<Project>().Should().NotBe(Relation<Developer>());
132+
133+
134+
Nest.Types types1 = "foo,bar";
135+
Nest.Types types2 = "bar,foo";
136+
types1.Should().Be(types2);
137+
(types1 == types2).Should().BeTrue();
132138
}
133139

134140
//hide

src/Tests/Framework/Configuration/TestConfigurationBase.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using Tests.Framework.Versions;
1+
using Tests.Framework.Versions;
72

83
namespace Tests.Framework.Configuration
94
{

src/Tests/Framework/Configuration/Versions/ElasticsearchVersion.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System;
22
using System.Collections.Concurrent;
3-
using System.Net;
4-
using System.Text.RegularExpressions;
5-
using Nest;
63
using Version = SemVer.Version;
74

85
namespace Tests.Framework.Versions

src/Tests/Framework/ElasticsearchVersionTests.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ namespace Tests.Framework
55
{
66
public class MockElasticsearchVersionResolver : ElasticsearchVersionResolver
77
{
8-
public MockElasticsearchVersionResolver()
9-
{
10-
11-
}
8+
public MockElasticsearchVersionResolver() { }
129

1310
public override string LatestSnapshot => "7.3.1-SNAPSHOT";
1411
public override string LatestVersion => "7.3.1";
@@ -18,8 +15,6 @@ public override string SnapshotZipFilename(string version)
1815
}
1916
}
2017

21-
22-
2318
public class ElasticsearchVersionTests
2419
{
2520
public ElasticsearchVersion Create(string version) => ElasticsearchVersion.Create(version, new MockElasticsearchVersionResolver());

0 commit comments

Comments
 (0)