Skip to content

Commit f02f6be

Browse files
author
Chris Martinez
committed
Fix ApiVersion.GetHashCode. Resolves #229
1 parent 050bedd commit f02f6be

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/Common/ApiVersion.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Microsoft.AspNetCore.Mvc
55
#endif
66
{
77
using System;
8+
using System.Collections.Generic;
89
using System.Diagnostics;
910
using System.Diagnostics.Contracts;
1011
using System.Globalization;
@@ -333,7 +334,35 @@ public static bool TryParse( string text, out ApiVersion version )
333334
/// <returns>A hash code.</returns>
334335
/// <remarks>The hash code is based on the uppercase, invariant hash of the
335336
/// <see cref="ToString()">text representation</see> of the object.</remarks>
336-
public override int GetHashCode() => ToString().ToUpperInvariant().GetHashCode();
337+
public override int GetHashCode()
338+
{
339+
var hashes = new List<int>( 4 );
340+
341+
if ( GroupVersion != null )
342+
{
343+
hashes.Add( GroupVersion.Value.GetHashCode() );
344+
}
345+
346+
if ( MajorVersion != null )
347+
{
348+
hashes.Add( MajorVersion.Value.GetHashCode() );
349+
hashes.Add( ImpliedMinorVersion.GetHashCode() );
350+
}
351+
352+
if ( !IsNullOrEmpty( Status ) )
353+
{
354+
hashes.Add( StringComparer.OrdinalIgnoreCase.GetHashCode( Status ) );
355+
}
356+
357+
var hash = hashes[0];
358+
359+
for ( var i = 1; i < hashes.Count; i++ )
360+
{
361+
hash = ( hash * 397 ) ^ hashes[i];
362+
}
363+
364+
return hash;
365+
}
337366

338367
/// <summary>
339368
/// Overloads the equality operator.

src/Microsoft.AspNet.WebApi.Versioning.ApiExplorer/Description/ApiDescriptionComparer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ public virtual int GetHashCode( VersionedApiDescription obj )
6363
var hash = comparer.GetHashCode( id );
6464
var apiVersion = obj.ApiVersion;
6565

66-
return ( hash * 397 ) ^ apiVersion?.GetHashCode() ?? 0;
66+
if ( apiVersion != null )
67+
{
68+
hash = ( hash * 397 ) ^ apiVersion.GetHashCode();
69+
}
70+
71+
return hash;
6772
}
6873

6974
/// <summary>

0 commit comments

Comments
 (0)