Skip to content

Commit db4c009

Browse files
authored
Fix #172: Adding spec version to OpenApiDiagnostic of Reader (#175)
1 parent 5ceedcd commit db4c009

File tree

9 files changed

+75
-24
lines changed

9 files changed

+75
-24
lines changed

src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Company>Microsoft</Company>
1111
<Title>Microsoft.OpenApi.Readers</Title>
1212
<PackageId>Microsoft.OpenApi.Readers</PackageId>
13-
<Version>1.0.0-beta009</Version>
13+
<Version>1.0.0-beta010</Version>
1414
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
1515
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
1616
<PackageTags>OpenAPI .NET</PackageTags>

src/Microsoft.OpenApi.Readers/OpenApiDiagnostic.cs

+5
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@ public class OpenApiDiagnostic : IDiagnostic
1515
/// List of all errors.
1616
/// </summary>
1717
public IList<OpenApiError> Errors { get; set; } = new List<OpenApiError>();
18+
19+
/// <summary>
20+
/// Open API specification version of the document parsed.
21+
/// </summary>
22+
public OpenApiSpecVersion SpecificationVersion { get; set; }
1823
}
1924
}

src/Microsoft.OpenApi.Readers/ParsingContext.cs

+3
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,21 @@ internal OpenApiDocument Parse(YamlDocument yamlDocument, OpenApiDiagnostic diag
4545
{
4646
VersionService = new OpenApiV2VersionService();
4747
doc = this.VersionService.LoadDocument(this.RootNode);
48+
diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi2_0;
4849
}
4950
else if (inputVersion.StartsWith("3.0."))
5051
{
5152
this.VersionService = new OpenApiV3VersionService();
5253
doc = this.VersionService.LoadDocument(this.RootNode);
54+
diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi3_0;
5355
}
5456
else
5557
{
5658
// If version number is not recognizable,
5759
// our best effort will try to deserialize the document to V3.
5860
this.VersionService = new OpenApiV3VersionService();
5961
doc = this.VersionService.LoadDocument(this.RootNode);
62+
diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi3_0;
6063
}
6164
return doc;
6265
}

src/Microsoft.OpenApi/Microsoft.OpenApi.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Company>Microsoft</Company>
1111
<Title>Microsoft.OpenApi</Title>
1212
<PackageId>Microsoft.OpenApi</PackageId>
13-
<Version>1.0.0-beta009</Version>
13+
<Version>1.0.0-beta010</Version>
1414
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
1515
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
1616
<PackageTags>OpenAPI .NET</PackageTags>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using FluentAssertions;
5+
using Xunit;
6+
7+
namespace Microsoft.OpenApi.Readers.Tests.OpenApiReaderTests
8+
{
9+
[Collection("DefaultSettings")]
10+
public class OpenApiDiagnosticTests
11+
{
12+
[Fact]
13+
public void DetectedSpecificationVersionShouldBeV2_0()
14+
{
15+
using (var stream = Resources.GetStream("V2Tests/Samples/basic.v2.yaml"))
16+
{
17+
new OpenApiStreamReader().Read(stream, out var diagnostic);
18+
19+
diagnostic.Should().NotBeNull();
20+
diagnostic.SpecificationVersion.Should().Be(OpenApiSpecVersion.OpenApi2_0);
21+
}
22+
}
23+
24+
[Fact]
25+
public void DetectedSpecificationVersionShouldBeV3_0()
26+
{
27+
using (var stream = Resources.GetStream("V3Tests/Samples/OpenApiDocument/minimalDocument.yaml"))
28+
{
29+
new OpenApiStreamReader().Read(stream, out var diagnostic);
30+
31+
diagnostic.Should().NotBeNull();
32+
diagnostic.SpecificationVersion.Should().Be( OpenApiSpecVersion.OpenApi3_0);
33+
}
34+
}
35+
}
36+
}

test/Microsoft.OpenApi.Readers.Tests/V2Tests/ComparisonTests.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ public void EquivalentV2AndV3DocumentsShouldProductEquivalentObjects(string file
2121
using (var streamV2 = Resources.GetStream(Path.Combine(SampleFolderPath, $"{fileName}.v2.yaml")))
2222
using (var streamV3 = Resources.GetStream(Path.Combine(SampleFolderPath, $"{fileName}.v3.yaml")))
2323
{
24-
var openApiDocV2 = new OpenApiStreamReader().Read(streamV2, out var contextV2);
25-
var openApiDocV3 = new OpenApiStreamReader().Read(streamV3, out var contextV3);
24+
var openApiDocV2 = new OpenApiStreamReader().Read(streamV2, out var diagnosticV2);
25+
var openApiDocV3 = new OpenApiStreamReader().Read(streamV3, out var diagnosticV3 );
2626

27-
openApiDocV3.ShouldBeEquivalentTo(
28-
openApiDocV2);
27+
openApiDocV3.ShouldBeEquivalentTo(openApiDocV2);
2928

30-
contextV2.ShouldBeEquivalentTo(contextV3);
29+
diagnosticV2.Errors.ShouldBeEquivalentTo(diagnosticV3.Errors);
3130
}
3231
}
3332
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,16 @@ public void ParseAdvancedCallbackWithReferenceShouldSucceed()
8080
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "advancedCallbackWithReference.yaml")))
8181
{
8282
// Act
83-
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
83+
var openApiDoc = new OpenApiStreamReader().Read(stream, out var diagnostic);
8484

8585
// Assert
8686
var path = openApiDoc.Paths.First().Value;
8787
var subscribeOperation = path.Operations[OperationType.Post];
8888

8989
var callback = subscribeOperation.Callbacks["simpleHook"];
9090

91-
context.ShouldBeEquivalentTo(new OpenApiDiagnostic());
91+
diagnostic.ShouldBeEquivalentTo(
92+
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
9293

9394
callback.ShouldBeEquivalentTo(
9495
new OpenApiCallback

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

+17-12
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,18 @@ public void ParseDocumentFromInlineStringShouldSucceed()
4747
});
4848

4949
context.ShouldBeEquivalentTo(
50-
new OpenApiDiagnostic());
50+
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
5151
}
5252

5353
[Fact]
5454
public void ParseBasicDocumentWithMultipleServersShouldSucceed()
5555
{
5656
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicDocumentWithMultipleServers.yaml")))
5757
{
58-
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
58+
var openApiDoc = new OpenApiStreamReader().Read(stream, out var diagnostic);
5959

60-
context.ShouldBeEquivalentTo(new OpenApiDiagnostic());
60+
diagnostic.ShouldBeEquivalentTo(
61+
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
6162

6263
openApiDoc.ShouldBeEquivalentTo(
6364
new OpenApiDocument
@@ -89,7 +90,7 @@ public void ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic()
8990
{
9091
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "brokenMinimalDocument.yaml")))
9192
{
92-
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
93+
var openApiDoc = new OpenApiStreamReader().Read(stream, out var diagnostic);
9394

9495
openApiDoc.ShouldBeEquivalentTo(
9596
new OpenApiDocument
@@ -100,13 +101,14 @@ public void ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic()
100101
}
101102
});
102103

103-
context.ShouldBeEquivalentTo(
104+
diagnostic.ShouldBeEquivalentTo(
104105
new OpenApiDiagnostic
105106
{
106107
Errors =
107108
{
108109
new OpenApiError("#/info", "title is a required property")
109-
}
110+
},
111+
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
110112
});
111113
}
112114
}
@@ -116,7 +118,7 @@ public void ParseMinimalDocumentShouldSucceed()
116118
{
117119
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "minimalDocument.yaml")))
118120
{
119-
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
121+
var openApiDoc = new OpenApiStreamReader().Read(stream, out var diagnostic);
120122

121123
openApiDoc.ShouldBeEquivalentTo(
122124
new OpenApiDocument
@@ -128,8 +130,8 @@ public void ParseMinimalDocumentShouldSucceed()
128130
}
129131
});
130132

131-
context.ShouldBeEquivalentTo(
132-
new OpenApiDiagnostic());
133+
diagnostic.ShouldBeEquivalentTo(
134+
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
133135
}
134136
}
135137

@@ -541,7 +543,8 @@ public void ParseStandardPetStoreDocumentShouldSucceed()
541543
actual.ShouldBeEquivalentTo(expected);
542544
}
543545

544-
context.ShouldBeEquivalentTo(new OpenApiDiagnostic());
546+
context.ShouldBeEquivalentTo(
547+
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
545548
}
546549

547550
[Fact]
@@ -1042,7 +1045,8 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed()
10421045
actual.ShouldBeEquivalentTo(expected);
10431046
}
10441047

1045-
context.ShouldBeEquivalentTo(new OpenApiDiagnostic());
1048+
context.ShouldBeEquivalentTo(
1049+
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
10461050
}
10471051

10481052
[Fact]
@@ -1057,7 +1061,8 @@ public void ParsePetStoreExpandedShouldSucceed()
10571061
// TODO: Create the object in memory and compare with the one read from YAML file.
10581062
}
10591063

1060-
context.ShouldBeEquivalentTo(new OpenApiDiagnostic());
1064+
context.ShouldBeEquivalentTo(
1065+
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
10611066
}
10621067
}
10631068
}

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ public void ParseBasicSchemaWithReferenceShouldSucceed()
188188
// Assert
189189
var components = openApiDoc.Components;
190190

191-
diagnostic.ShouldBeEquivalentTo(new OpenApiDiagnostic());
191+
diagnostic.ShouldBeEquivalentTo(
192+
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
192193

193194
components.ShouldBeEquivalentTo(
194195
new OpenApiComponents
@@ -279,8 +280,9 @@ public void ParseAdvancedSchemaWithReferenceShouldSucceed()
279280

280281
// Assert
281282
var components = openApiDoc.Components;
282-
283-
diagnostic.ShouldBeEquivalentTo(new OpenApiDiagnostic());
283+
284+
diagnostic.ShouldBeEquivalentTo(
285+
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
284286

285287
components.ShouldBeEquivalentTo(
286288
new OpenApiComponents

0 commit comments

Comments
 (0)