Skip to content

Commit 9740913

Browse files
committed
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 Conflicts: src/Nest/CommonAbstractions/Infer/Indices/Indices.cs src/Nest/CommonAbstractions/Infer/Types/Types.cs
1 parent 48e54ca commit 9740913

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

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

Lines changed: 9 additions & 7 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,6 +84,10 @@ 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
{
9093
if (!(obj is Indices other)) return false;
@@ -97,12 +100,11 @@ public override bool Equals(object obj)
97100
);
98101
}
99102

100-
private static bool EqualsAllIndices(IReadOnlyList<IndexName> indicesCurrent, IReadOnlyList<IndexName> indicesOther)
103+
private static bool EqualsAllIndices(IReadOnlyList<IndexName> thisIndices, IReadOnlyList<IndexName> otherIndices)
101104
{
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));
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();
106108
}
107109

108110
public override int GetHashCode()

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ 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
{
@@ -98,12 +101,12 @@ public override bool Equals(object obj)
98101
);
99102
}
100103

101-
private static bool EqualsAllTypes(IReadOnlyList<TypeName> indicesCurrent, IReadOnlyList<TypeName> indicesOther)
104+
private static bool EqualsAllTypes(IReadOnlyList<TypeName> thisTypes, IReadOnlyList<TypeName> otherTypes)
102105
{
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));
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();
107110
}
108111

109112
public override int GetHashCode()

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ [U] public void EqualsValidation()
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

0 commit comments

Comments
 (0)