Skip to content

Commit

Permalink
Fix #172: Adding spec version to OpenApiDiagnostic of Reader (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-lin authored Jan 11, 2018
1 parent 5ceedcd commit db4c009
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Company>Microsoft</Company>
<Title>Microsoft.OpenApi.Readers</Title>
<PackageId>Microsoft.OpenApi.Readers</PackageId>
<Version>1.0.0-beta009</Version>
<Version>1.0.0-beta010</Version>
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>OpenAPI .NET</PackageTags>
Expand Down
5 changes: 5 additions & 0 deletions src/Microsoft.OpenApi.Readers/OpenApiDiagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@ public class OpenApiDiagnostic : IDiagnostic
/// List of all errors.
/// </summary>
public IList<OpenApiError> Errors { get; set; } = new List<OpenApiError>();

/// <summary>
/// Open API specification version of the document parsed.
/// </summary>
public OpenApiSpecVersion SpecificationVersion { get; set; }
}
}
3 changes: 3 additions & 0 deletions src/Microsoft.OpenApi.Readers/ParsingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,21 @@ internal OpenApiDocument Parse(YamlDocument yamlDocument, OpenApiDiagnostic diag
{
VersionService = new OpenApiV2VersionService();
doc = this.VersionService.LoadDocument(this.RootNode);
diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi2_0;
}
else if (inputVersion.StartsWith("3.0."))
{
this.VersionService = new OpenApiV3VersionService();
doc = this.VersionService.LoadDocument(this.RootNode);
diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi3_0;
}
else
{
// If version number is not recognizable,
// our best effort will try to deserialize the document to V3.
this.VersionService = new OpenApiV3VersionService();
doc = this.VersionService.LoadDocument(this.RootNode);
diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi3_0;
}
return doc;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Company>Microsoft</Company>
<Title>Microsoft.OpenApi</Title>
<PackageId>Microsoft.OpenApi</PackageId>
<Version>1.0.0-beta009</Version>
<Version>1.0.0-beta010</Version>
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>OpenAPI .NET</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using FluentAssertions;
using Xunit;

namespace Microsoft.OpenApi.Readers.Tests.OpenApiReaderTests
{
[Collection("DefaultSettings")]
public class OpenApiDiagnosticTests
{
[Fact]
public void DetectedSpecificationVersionShouldBeV2_0()
{
using (var stream = Resources.GetStream("V2Tests/Samples/basic.v2.yaml"))
{
new OpenApiStreamReader().Read(stream, out var diagnostic);

diagnostic.Should().NotBeNull();
diagnostic.SpecificationVersion.Should().Be(OpenApiSpecVersion.OpenApi2_0);
}
}

[Fact]
public void DetectedSpecificationVersionShouldBeV3_0()
{
using (var stream = Resources.GetStream("V3Tests/Samples/OpenApiDocument/minimalDocument.yaml"))
{
new OpenApiStreamReader().Read(stream, out var diagnostic);

diagnostic.Should().NotBeNull();
diagnostic.SpecificationVersion.Should().Be( OpenApiSpecVersion.OpenApi3_0);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ public void EquivalentV2AndV3DocumentsShouldProductEquivalentObjects(string file
using (var streamV2 = Resources.GetStream(Path.Combine(SampleFolderPath, $"{fileName}.v2.yaml")))
using (var streamV3 = Resources.GetStream(Path.Combine(SampleFolderPath, $"{fileName}.v3.yaml")))
{
var openApiDocV2 = new OpenApiStreamReader().Read(streamV2, out var contextV2);
var openApiDocV3 = new OpenApiStreamReader().Read(streamV3, out var contextV3);
var openApiDocV2 = new OpenApiStreamReader().Read(streamV2, out var diagnosticV2);
var openApiDocV3 = new OpenApiStreamReader().Read(streamV3, out var diagnosticV3 );

openApiDocV3.ShouldBeEquivalentTo(
openApiDocV2);
openApiDocV3.ShouldBeEquivalentTo(openApiDocV2);

contextV2.ShouldBeEquivalentTo(contextV3);
diagnosticV2.Errors.ShouldBeEquivalentTo(diagnosticV3.Errors);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@ public void ParseAdvancedCallbackWithReferenceShouldSucceed()
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "advancedCallbackWithReference.yaml")))
{
// Act
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
var openApiDoc = new OpenApiStreamReader().Read(stream, out var diagnostic);

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

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

context.ShouldBeEquivalentTo(new OpenApiDiagnostic());
diagnostic.ShouldBeEquivalentTo(
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });

callback.ShouldBeEquivalentTo(
new OpenApiCallback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ public void ParseDocumentFromInlineStringShouldSucceed()
});

context.ShouldBeEquivalentTo(
new OpenApiDiagnostic());
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
}

[Fact]
public void ParseBasicDocumentWithMultipleServersShouldSucceed()
{
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicDocumentWithMultipleServers.yaml")))
{
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
var openApiDoc = new OpenApiStreamReader().Read(stream, out var diagnostic);

context.ShouldBeEquivalentTo(new OpenApiDiagnostic());
diagnostic.ShouldBeEquivalentTo(
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });

openApiDoc.ShouldBeEquivalentTo(
new OpenApiDocument
Expand Down Expand Up @@ -89,7 +90,7 @@ public void ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic()
{
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "brokenMinimalDocument.yaml")))
{
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
var openApiDoc = new OpenApiStreamReader().Read(stream, out var diagnostic);

openApiDoc.ShouldBeEquivalentTo(
new OpenApiDocument
Expand All @@ -100,13 +101,14 @@ public void ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic()
}
});

context.ShouldBeEquivalentTo(
diagnostic.ShouldBeEquivalentTo(
new OpenApiDiagnostic
{
Errors =
{
new OpenApiError("#/info", "title is a required property")
}
},
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
});
}
}
Expand All @@ -116,7 +118,7 @@ public void ParseMinimalDocumentShouldSucceed()
{
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "minimalDocument.yaml")))
{
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
var openApiDoc = new OpenApiStreamReader().Read(stream, out var diagnostic);

openApiDoc.ShouldBeEquivalentTo(
new OpenApiDocument
Expand All @@ -128,8 +130,8 @@ public void ParseMinimalDocumentShouldSucceed()
}
});

context.ShouldBeEquivalentTo(
new OpenApiDiagnostic());
diagnostic.ShouldBeEquivalentTo(
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
}
}

Expand Down Expand Up @@ -541,7 +543,8 @@ public void ParseStandardPetStoreDocumentShouldSucceed()
actual.ShouldBeEquivalentTo(expected);
}

context.ShouldBeEquivalentTo(new OpenApiDiagnostic());
context.ShouldBeEquivalentTo(
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
}

[Fact]
Expand Down Expand Up @@ -1042,7 +1045,8 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed()
actual.ShouldBeEquivalentTo(expected);
}

context.ShouldBeEquivalentTo(new OpenApiDiagnostic());
context.ShouldBeEquivalentTo(
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
}

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

context.ShouldBeEquivalentTo(new OpenApiDiagnostic());
context.ShouldBeEquivalentTo(
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ public void ParseBasicSchemaWithReferenceShouldSucceed()
// Assert
var components = openApiDoc.Components;

diagnostic.ShouldBeEquivalentTo(new OpenApiDiagnostic());
diagnostic.ShouldBeEquivalentTo(
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });

components.ShouldBeEquivalentTo(
new OpenApiComponents
Expand Down Expand Up @@ -279,8 +280,9 @@ public void ParseAdvancedSchemaWithReferenceShouldSucceed()

// Assert
var components = openApiDoc.Components;

diagnostic.ShouldBeEquivalentTo(new OpenApiDiagnostic());

diagnostic.ShouldBeEquivalentTo(
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });

components.ShouldBeEquivalentTo(
new OpenApiComponents
Expand Down

0 comments on commit db4c009

Please sign in to comment.