Skip to content

Commit 2e47cb2

Browse files
committed
fix #3116 instances of gethashcode in equals implementation
1 parent 1c9d50c commit 2e47cb2

File tree

9 files changed

+33
-33
lines changed

9 files changed

+33
-33
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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,24 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings)
8787

8888
public override bool Equals(object obj)
8989
{
90-
var other = obj as Indices;
91-
if (other == null) return false;
90+
if (!(obj is Indices other)) return false;
9291
return this.Match(
9392
all => other.Match(a => true, m => false),
9493
many => other.Match(
9594
a => false,
96-
m => this.GetHashCode().Equals(other.GetHashCode())
95+
m => EqualsAllIndices(m.Indices, many.Indices)
9796
)
9897
);
9998
}
10099

100+
private static bool EqualsAllIndices(IReadOnlyList<IndexName> indicesCurrent, IReadOnlyList<IndexName> indicesOther)
101+
{
102+
if (indicesCurrent == null && indicesOther == null) return true;
103+
if (indicesCurrent == null || indicesOther == null) return false;
104+
if (indicesCurrent.Count != indicesOther.Count) return false;
105+
return indicesCurrent.Zip(indicesOther, Tuple.Create).All(t=>t.Item1.Equals(t.Item2));
106+
}
107+
101108
public override int GetHashCode()
102109
{
103110
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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,24 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings)
8888

8989
public override bool Equals(object obj)
9090
{
91-
var other = obj as Types;
92-
if (other == null) return false;
91+
if (!(obj is Types other)) return false;
9392
return this.Match(
9493
all => other.Match(a => true, m => false),
9594
many => other.Match(
9695
a => false,
97-
m => this.GetHashCode().Equals(other.GetHashCode())
96+
m => EqualsAllTypes(m.Types, many.Types)
9897
)
9998
);
10099
}
101100

101+
private static bool EqualsAllTypes(IReadOnlyList<TypeName> indicesCurrent, IReadOnlyList<TypeName> indicesOther)
102+
{
103+
if (indicesCurrent == null && indicesOther == null) return true;
104+
if (indicesCurrent == null || indicesOther == null) return false;
105+
if (indicesCurrent.Count != indicesOther.Count) return false;
106+
return indicesCurrent.Zip(indicesOther, Tuple.Create).All(t=>t.Item1.Equals(t.Item2));
107+
}
108+
102109
public override int GetHashCode()
103110
{
104111
return this.Match(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,14 @@ [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"));

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)