Skip to content

Commit 7a58221

Browse files
committed
Simplify using statement, add tests, test file and cleanup
1 parent 500f0c7 commit 7a58221

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public OpenApiDocument Read(YamlDocument input, out OpenApiDiagnostic diagnostic
7979
{
8080
diagnostic.Warnings.Add(item);
8181
}
82-
8382
}
8483

8584
return document;

test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>net6.0</TargetFrameworks>
44
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
@@ -137,6 +137,9 @@
137137
<EmbeddedResource Include="V3Tests\Samples\OpenApiDocument\minimalDocument.yaml">
138138
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
139139
</EmbeddedResource>
140+
<EmbeddedResource Include="V3Tests\Samples\OpenApiDocument\minimalDocumentWithWhitespace.yaml">
141+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
142+
</EmbeddedResource>
140143
<EmbeddedResource Include="V3Tests\Samples\OpenApiDocument\apiWithFullHeaderComponent.yaml">
141144
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
142145
</EmbeddedResource>

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

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -243,26 +243,55 @@ public void ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic()
243243
[Fact]
244244
public void ParseMinimalDocumentShouldSucceed()
245245
{
246-
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "minimalDocument.yaml")))
247-
{
248-
var openApiDoc = new OpenApiStreamReader().Read(stream, out var diagnostic);
246+
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "minimalDocument.yaml"));
247+
var openApiDoc = new OpenApiStreamReader().Read(stream, out var diagnostic);
249248

250-
openApiDoc.Should().BeEquivalentTo(
251-
new OpenApiDocument
249+
openApiDoc.Should().BeEquivalentTo(
250+
new OpenApiDocument
251+
{
252+
Info = new OpenApiInfo
252253
{
253-
Info = new OpenApiInfo
254-
{
255-
Title = "Simple Document",
256-
Version = "0.9.1"
257-
},
258-
Paths = new OpenApiPaths()
259-
});
254+
Title = "Simple Document",
255+
Version = "0.9.1"
256+
},
257+
Paths = new OpenApiPaths()
258+
});
260259

261-
diagnostic.Should().BeEquivalentTo(
262-
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
263-
}
260+
diagnostic.Should().BeEquivalentTo(
261+
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
264262
}
265263

264+
[Fact]
265+
public void TestHashCodesForSimilarOpenApiDocuments()
266+
{
267+
// Arrange
268+
using var stream1 = Resources.GetStream(Path.Combine(SampleFolderPath, "minimalDocument.yaml"));
269+
using var stream2 = Resources.GetStream(Path.Combine(SampleFolderPath, "minimalDocument.yaml"));
270+
using var stream3 = Resources.GetStream(Path.Combine(SampleFolderPath, "minimalDocumentWithWhitespace.yaml"));
271+
272+
// Act
273+
/*
274+
Test whether reading in the same document twice yields the same hash code,
275+
And reading in similar documents but one has a whitespace yields the same hash code
276+
*/
277+
var openApiDoc1 = new OpenApiStreamReader().Read(stream1, out var diagnostic1);
278+
var openApiDoc2 = new OpenApiStreamReader().Read(stream2, out var diagnostic2);
279+
var openApiDoc3 = new OpenApiStreamReader().Read(stream3, out var diagnostic3);
280+
281+
// Assert
282+
/* The assumption is, if doc1.Equals(doc2), then doc1.GetHashCode().Equals(doc2.GetHashCode())*/
283+
if (openApiDoc1.Equals(openApiDoc2) && openApiDoc2.Equals(openApiDoc3))
284+
{
285+
Assert.Equal(diagnostic1.HashCode, diagnostic2.HashCode);
286+
Assert.Equal(diagnostic2.HashCode, diagnostic3.HashCode);
287+
}
288+
289+
/*Adding a server object to the original doc to check whether the hash code changes*/
290+
openApiDoc1.Servers = new List<OpenApiServer>();
291+
var hash = openApiDoc1.GetHashCode();
292+
Assert.NotEqual(diagnostic1.HashCode, hash);
293+
}
294+
266295
[Fact]
267296
public void ParseStandardPetStoreDocumentShouldSucceed()
268297
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
openapi : 3.0.0
2+
info:
3+
title: Simple Document
4+
version: 0.9.1
5+
6+
paths: {}
7+
8+
9+

0 commit comments

Comments
 (0)