From 05710d8cffcb68dfdb36b0c78e5872196b46a8d6 Mon Sep 17 00:00:00 2001 From: Scott Lin Date: Tue, 16 Jan 2018 11:26:58 -0800 Subject: [PATCH 1/3] Throwing OpenApiUnsupportedSpecVersionException when spec version is not recognized in Reader --- .../Exceptions/OpenApiReaderException.cs | 40 ++++++++++++++++ .../OpenApiUnsupportedSpecVersionException.cs | 48 +++++++++++++++++++ .../ParsingContext.cs | 37 +++++++------- .../Microsoft.OpenApi.Readers.Tests.csproj | 3 ++ .../Samples/unsupported.v1.yaml | 8 ++++ .../UnsupportedSpecVersionTests.cs | 24 ++++++++++ 6 files changed, 141 insertions(+), 19 deletions(-) create mode 100644 src/Microsoft.OpenApi.Readers/Exceptions/OpenApiReaderException.cs create mode 100644 src/Microsoft.OpenApi.Readers/Exceptions/OpenApiUnsupportedSpecVersionException.cs create mode 100644 test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/Samples/unsupported.v1.yaml create mode 100644 test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/UnsupportedSpecVersionTests.cs diff --git a/src/Microsoft.OpenApi.Readers/Exceptions/OpenApiReaderException.cs b/src/Microsoft.OpenApi.Readers/Exceptions/OpenApiReaderException.cs new file mode 100644 index 000000000..271733b2e --- /dev/null +++ b/src/Microsoft.OpenApi.Readers/Exceptions/OpenApiReaderException.cs @@ -0,0 +1,40 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +using System; +using System.Runtime.Serialization; + +namespace Microsoft.OpenApi.Readers.Exceptions +{ + /// + /// Defines an exception indicating OpenAPI Reader encountered an issue while reading. + /// + [Serializable] + public class OpenApiReaderException : Exception + { + /// + /// Initializes the class. + /// + public OpenApiReaderException() { } + + /// + /// Initializes the class with a custom message. + /// + /// Plain text error message for this exception. + public OpenApiReaderException(string message) : base(message) { } + + /// + /// Initializes the class with a custom message and inner exception. + /// + /// Plain text error message for this exception. + /// Inner exception that caused this exception to be thrown. + public OpenApiReaderException(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Initializes the class based on serialization info and context. + /// + /// Info needed to serialize or deserialize this exception. + /// Context needed to serialize or deserialize this exception. + protected OpenApiReaderException(SerializationInfo info, StreamingContext context) : base(info, context) { } + } +} \ No newline at end of file diff --git a/src/Microsoft.OpenApi.Readers/Exceptions/OpenApiUnsupportedSpecVersionException.cs b/src/Microsoft.OpenApi.Readers/Exceptions/OpenApiUnsupportedSpecVersionException.cs new file mode 100644 index 000000000..c15ec299d --- /dev/null +++ b/src/Microsoft.OpenApi.Readers/Exceptions/OpenApiUnsupportedSpecVersionException.cs @@ -0,0 +1,48 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +using System; +using System.Globalization; +using System.Runtime.Serialization; + +namespace Microsoft.OpenApi.Readers.Exceptions +{ + /// + /// Defines an exception indicating OpenAPI Reader encountered an unsupported specification version while reading. + /// + [Serializable] + public class OpenApiUnsupportedSpecVersionException : OpenApiReaderException + { + const string messagePattern = "OpenAPI specification version {0} is not supported."; + + /// + /// Initializes the class. + /// + public OpenApiUnsupportedSpecVersionException() { } + + /// + /// Initializes the class with a specification version. + /// + /// Version that caused this exception to be thrown. + public OpenApiUnsupportedSpecVersionException(string specificationVersion) + : base(string.Format(CultureInfo.InvariantCulture, messagePattern, specificationVersion)) { } + + /// + /// Initializes the class with a specification version and + /// inner exception. + /// + /// Version that caused this exception to be thrown. + /// Inner exception that caused this exception to be thrown. + public OpenApiUnsupportedSpecVersionException(string specificationVersion, Exception innerException) + : base(string.Format(CultureInfo.InvariantCulture, messagePattern, specificationVersion), innerException) { } + + /// + /// Initializes the class based on serialization info and + /// context. + /// + /// Info needed to serialize or deserialize this exception. + /// Context needed to serialize or deserialize this exception. + protected OpenApiUnsupportedSpecVersionException(SerializationInfo info, StreamingContext context) + : base(info, context) { } + } +} \ No newline at end of file diff --git a/src/Microsoft.OpenApi.Readers/ParsingContext.cs b/src/Microsoft.OpenApi.Readers/ParsingContext.cs index f47112cdb..400aad511 100644 --- a/src/Microsoft.OpenApi.Readers/ParsingContext.cs +++ b/src/Microsoft.OpenApi.Readers/ParsingContext.cs @@ -6,6 +6,7 @@ using System.Linq; using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Models; +using Microsoft.OpenApi.Readers.Exceptions; using Microsoft.OpenApi.Readers.Interface; using Microsoft.OpenApi.Readers.ParseNodes; using Microsoft.OpenApi.Readers.V2; @@ -41,26 +42,24 @@ internal OpenApiDocument Parse(YamlDocument yamlDocument, OpenApiDiagnostic diag OpenApiDocument doc; - if (inputVersion == "2.0") + switch (inputVersion) { - 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; + case string version when version == "2.0": + VersionService = new OpenApiV2VersionService(); + doc = this.VersionService.LoadDocument(this.RootNode); + diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi2_0; + break; + + case string version when version.StartsWith("3.0"): + this.VersionService = new OpenApiV3VersionService(); + doc = this.VersionService.LoadDocument(this.RootNode); + diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi3_0; + break; + + default: + throw new OpenApiUnsupportedSpecVersionException(inputVersion); } + return doc; } @@ -81,7 +80,7 @@ private static string GetVersion(RootNode rootNode) return versionNode?.GetScalarValue(); } - private void ComputeTags(List tags, Func loadTag ) + private void ComputeTags(List tags, Func loadTag) { // Precompute the tags array so that each tag reference does not require a new deserialization. var tagListPointer = new JsonPointer("#/tags"); diff --git a/test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj b/test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj index f4b89b493..0c14da164 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj +++ b/test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj @@ -13,6 +13,9 @@ ..\..\src\Microsoft.OpenApi.snk + + Never + Never diff --git a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/Samples/unsupported.v1.yaml b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/Samples/unsupported.v1.yaml new file mode 100644 index 000000000..ff9d27eac --- /dev/null +++ b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/Samples/unsupported.v1.yaml @@ -0,0 +1,8 @@ +swagger: 1.0 +info: + title: This is a simple example + version: 1.0.0 +host: example.org +basePath: /api +schemes: ["http", "https"] +paths: {} diff --git a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/UnsupportedSpecVersionTests.cs b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/UnsupportedSpecVersionTests.cs new file mode 100644 index 000000000..d1dde76b4 --- /dev/null +++ b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/UnsupportedSpecVersionTests.cs @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +using System; +using FluentAssertions; +using Microsoft.OpenApi.Readers.Exceptions; +using Xunit; + +namespace Microsoft.OpenApi.Readers.Tests.OpenApiReaderTests +{ + [Collection("DefaultSettings")] + public class UnsupportedSpecVersionTests + { + [Fact] + public void ThrowOpenApiUnsupportedSpecVersionException() + { + using (var stream = Resources.GetStream("OpenApiReaderTests/Samples/unsupported.v1.yaml")) + { + Action act = () => new OpenApiStreamReader().Read(stream, out var diagnostic); + act.ShouldThrow(); + } + } + } +} \ No newline at end of file From 53c61c502d81d8d446ea3cbeb562315aeea1c747 Mon Sep 17 00:00:00 2001 From: Scott Lin Date: Tue, 16 Jan 2018 11:53:04 -0800 Subject: [PATCH 2/3] Removed serialization constructor from exception to make it consistent with writer exceptions and added spec version property to exception --- .../Exceptions/OpenApiReaderException.cs | 8 ----- .../OpenApiUnsupportedSpecVersionException.cs | 34 +++++++++++-------- .../Samples/unsupported.v1.yaml | 2 +- .../UnsupportedSpecVersionTests.cs | 11 ++++-- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/Microsoft.OpenApi.Readers/Exceptions/OpenApiReaderException.cs b/src/Microsoft.OpenApi.Readers/Exceptions/OpenApiReaderException.cs index 271733b2e..25aac4978 100644 --- a/src/Microsoft.OpenApi.Readers/Exceptions/OpenApiReaderException.cs +++ b/src/Microsoft.OpenApi.Readers/Exceptions/OpenApiReaderException.cs @@ -2,7 +2,6 @@ // Licensed under the MIT license. using System; -using System.Runtime.Serialization; namespace Microsoft.OpenApi.Readers.Exceptions { @@ -29,12 +28,5 @@ public OpenApiReaderException(string message) : base(message) { } /// Plain text error message for this exception. /// Inner exception that caused this exception to be thrown. public OpenApiReaderException(string message, Exception innerException) : base(message, innerException) { } - - /// - /// Initializes the class based on serialization info and context. - /// - /// Info needed to serialize or deserialize this exception. - /// Context needed to serialize or deserialize this exception. - protected OpenApiReaderException(SerializationInfo info, StreamingContext context) : base(info, context) { } } } \ No newline at end of file diff --git a/src/Microsoft.OpenApi.Readers/Exceptions/OpenApiUnsupportedSpecVersionException.cs b/src/Microsoft.OpenApi.Readers/Exceptions/OpenApiUnsupportedSpecVersionException.cs index c15ec299d..84faf9d4e 100644 --- a/src/Microsoft.OpenApi.Readers/Exceptions/OpenApiUnsupportedSpecVersionException.cs +++ b/src/Microsoft.OpenApi.Readers/Exceptions/OpenApiUnsupportedSpecVersionException.cs @@ -3,7 +3,6 @@ using System; using System.Globalization; -using System.Runtime.Serialization; namespace Microsoft.OpenApi.Readers.Exceptions { @@ -15,17 +14,20 @@ public class OpenApiUnsupportedSpecVersionException : OpenApiReaderException { const string messagePattern = "OpenAPI specification version {0} is not supported."; - /// - /// Initializes the class. - /// - public OpenApiUnsupportedSpecVersionException() { } - /// /// Initializes the class with a specification version. /// /// Version that caused this exception to be thrown. public OpenApiUnsupportedSpecVersionException(string specificationVersion) - : base(string.Format(CultureInfo.InvariantCulture, messagePattern, specificationVersion)) { } + : base(string.Format(CultureInfo.InvariantCulture, messagePattern, specificationVersion)) + { + if (string.IsNullOrWhiteSpace(specificationVersion)) + { + throw new ArgumentException("Value cannot be null or white space.", nameof(specificationVersion)); + } + + this.SpecificationVersion = specificationVersion; + } /// /// Initializes the class with a specification version and @@ -34,15 +36,19 @@ public OpenApiUnsupportedSpecVersionException(string specificationVersion) /// Version that caused this exception to be thrown. /// Inner exception that caused this exception to be thrown. public OpenApiUnsupportedSpecVersionException(string specificationVersion, Exception innerException) - : base(string.Format(CultureInfo.InvariantCulture, messagePattern, specificationVersion), innerException) { } + : base(string.Format(CultureInfo.InvariantCulture, messagePattern, specificationVersion), innerException) + { + if (string.IsNullOrWhiteSpace(specificationVersion)) + { + throw new ArgumentException("Value cannot be null or white space.", nameof(specificationVersion)); + } + + this.SpecificationVersion = specificationVersion; + } /// - /// Initializes the class based on serialization info and - /// context. + /// The unsupported specification version. /// - /// Info needed to serialize or deserialize this exception. - /// Context needed to serialize or deserialize this exception. - protected OpenApiUnsupportedSpecVersionException(SerializationInfo info, StreamingContext context) - : base(info, context) { } + public string SpecificationVersion { get; } } } \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/Samples/unsupported.v1.yaml b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/Samples/unsupported.v1.yaml index ff9d27eac..f205f2d2b 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/Samples/unsupported.v1.yaml +++ b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/Samples/unsupported.v1.yaml @@ -1,4 +1,4 @@ -swagger: 1.0 +swagger: 1.0.0 info: title: This is a simple example version: 1.0.0 diff --git a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/UnsupportedSpecVersionTests.cs b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/UnsupportedSpecVersionTests.cs index d1dde76b4..f20529115 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/UnsupportedSpecVersionTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/UnsupportedSpecVersionTests.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. -using System; using FluentAssertions; using Microsoft.OpenApi.Readers.Exceptions; using Xunit; @@ -16,8 +15,14 @@ public void ThrowOpenApiUnsupportedSpecVersionException() { using (var stream = Resources.GetStream("OpenApiReaderTests/Samples/unsupported.v1.yaml")) { - Action act = () => new OpenApiStreamReader().Read(stream, out var diagnostic); - act.ShouldThrow(); + try + { + new OpenApiStreamReader().Read(stream, out var diagnostic); + } + catch (OpenApiUnsupportedSpecVersionException exception) + { + exception.SpecificationVersion.Should().Be("1.0.0"); + } } } } From a4b28928c499c2d0299ee1c7d4eb23b1a050dfeb Mon Sep 17 00:00:00 2001 From: Scott Lin Date: Tue, 16 Jan 2018 11:59:39 -0800 Subject: [PATCH 3/3] Bumping nuget version --- src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj | 2 +- src/Microsoft.OpenApi/Microsoft.OpenApi.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj b/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj index 687ea1b5b..84edc097a 100644 --- a/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj +++ b/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj @@ -10,7 +10,7 @@ Microsoft Microsoft.OpenApi.Readers Microsoft.OpenApi.Readers - 1.0.0-beta010 + 1.0.0-beta011 OpenAPI.NET Readers for JSON and YAML documents © Microsoft Corporation. All rights reserved. OpenAPI .NET diff --git a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj index e7a5e7f68..e44c3ca0e 100644 --- a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj +++ b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj @@ -10,7 +10,7 @@ Microsoft Microsoft.OpenApi Microsoft.OpenApi - 1.0.0-beta010 + 1.0.0-beta011 .NET models with JSON and YAML writers for OpenAPI specification © Microsoft Corporation. All rights reserved. OpenAPI .NET