Skip to content

Commit 8d20075

Browse files
committed
Clean up test
1 parent c7e3ed8 commit 8d20075

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.IO;
67
using System.Linq;
8+
using System.Runtime.Serialization.Formatters.Binary;
9+
using System.Security.Cryptography;
10+
using System.Text;
711
using Microsoft.OpenApi.Exceptions;
812
using Microsoft.OpenApi.Interfaces;
913
using Microsoft.OpenApi.Services;
@@ -62,6 +66,8 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible
6266
/// </summary>
6367
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
6468

69+
private static readonly object locker = new();
70+
6571
/// <summary>
6672
/// Parameter-less constructor
6773
/// </summary>
@@ -375,26 +381,42 @@ public IOpenApiReferenceable ResolveReference(OpenApiReference reference)
375381
return ResolveReference(reference, false);
376382
}
377383

378-
/// <summary>
379-
/// Computes the hash code for an OpenApiDocument and its property values.
380-
/// </summary>
381-
/// <returns> The hash code.</returns>
382-
public override int GetHashCode()
384+
///// <summary>
385+
///// Computes the hash code for an OpenApiDocument and its property values.
386+
///// </summary>
387+
///// <returns> The hash code.</returns>
388+
//public override int GetHashCode()
389+
//{
390+
// // select two random prime numbers e.g 1 and 3 and use them to compute hash codes
391+
// int hash = 1;
392+
// hash = hash * 3 + (Workspace == null ? 0 : Workspace.GetHashCode());
393+
// hash = hash * 3 + (Info == null ? 0 : Info.GetHashCode());
394+
// hash = hash * 3 + (Servers == null ? 0 : Servers.GetHashCode());
395+
// hash = hash * 3 + (Paths == null ? 0 : Paths.GetHashCode());
396+
// hash = hash * 3 + (Components == null ? 0 : Components.GetHashCode());
397+
// hash = hash * 3 + (SecurityRequirements == null ? 0 : SecurityRequirements.GetHashCode());
398+
// hash = hash * 3 + (Tags == null ? 0 : Tags.GetHashCode());
399+
// hash = hash * 3 + (ExternalDocs == null ? 0 : ExternalDocs.GetHashCode());
400+
// hash = hash * 3 + (Extensions == null ? 0 : Extensions.GetHashCode());
401+
402+
// return hash;
403+
//}
404+
405+
public static string GenerateHashValue(Stream input)
383406
{
384-
// select two random prime numbers e.g 1 and 3 and use them to compute hash codes
385-
int hash = 1;
386-
hash = hash * 3 + (Workspace == null ? 0 : Workspace.GetHashCode());
387-
hash = hash * 3 + (Info == null ? 0 : Info.GetHashCode());
388-
hash = hash * 3 + (Servers == null ? 0 : Servers.GetHashCode());
389-
hash = hash * 3 + (Paths == null ? 0 : Paths.GetHashCode());
390-
hash = hash * 3 + (Components == null ? 0 : Components.GetHashCode());
391-
hash = hash * 3 + (SecurityRequirements == null ? 0 : SecurityRequirements.GetHashCode());
392-
hash = hash * 3 + (Tags == null ? 0 : Tags.GetHashCode());
393-
hash = hash * 3 + (ExternalDocs == null ? 0 : ExternalDocs.GetHashCode());
394-
hash = hash * 3 + (Extensions == null ? 0 : Extensions.GetHashCode());
395-
396-
return hash;
397-
}
407+
HashAlgorithm sha = SHA512.Create();
408+
byte[] result = sha.ComputeHash(input);
409+
410+
// Build the final string by converting each byte
411+
// into hex and appending it to a StringBuilder
412+
StringBuilder sb = new StringBuilder();
413+
for (int i = 0; i < result.Length; i++)
414+
{
415+
sb.Append(result[i].ToString("X2"));
416+
}
417+
418+
return sb.ToString();
419+
}
398420

399421
/// <summary>
400422
/// Load the referenced <see cref="IOpenApiReferenceable"/> object from a <see cref="OpenApiReference"/> object

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
using System.Threading;
1010
using FluentAssertions;
1111
using Microsoft.OpenApi.Any;
12-
using Microsoft.OpenApi.Extensions;
1312
using Microsoft.OpenApi.Interfaces;
1413
using Microsoft.OpenApi.Models;
1514
using Microsoft.OpenApi.Validations;
1615
using Microsoft.OpenApi.Validations.Rules;
1716
using Microsoft.OpenApi.Writers;
18-
using Newtonsoft.Json;
1917
using Xunit;
2018
using Xunit.Abstractions;
2119

@@ -275,17 +273,8 @@ And reading in similar documents but one has a whitespace yields the same hash c
275273
var openApiDoc3 = new OpenApiStreamReader().Read(stream3, out var diagnostic3);
276274

277275
// Assert
278-
/* The assumption is, if doc1.Equals(doc2), then doc1.GetHashCode().Equals(doc2.GetHashCode())*/
279-
if (openApiDoc1.Equals(openApiDoc2) && openApiDoc2.Equals(openApiDoc3))
280-
{
281-
Assert.Equal(diagnostic1.HashCode, diagnostic2.HashCode);
282-
Assert.Equal(diagnostic2.HashCode, diagnostic3.HashCode);
283-
}
284-
285-
/*Adding a server object to the original doc to check whether the hash code changes*/
286-
openApiDoc1.Servers = new List<OpenApiServer>();
287-
var hash = openApiDoc1.GetHashCode();
288-
Assert.NotEqual(diagnostic1.HashCode, hash);
276+
Assert.Equal(diagnostic1.HashCode, diagnostic2.HashCode);
277+
Assert.Equal(diagnostic2.HashCode, diagnostic3.HashCode);
289278
}
290279

291280
[Fact]

0 commit comments

Comments
 (0)