-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix 177]: Throw exception when spec version is not recognized by rea…
…der (#181) Throwing OpenApiUnsupportedSpecVersionException when spec version is not recognized in Reader
- Loading branch information
Showing
8 changed files
with
146 additions
and
21 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/Microsoft.OpenApi.Readers/Exceptions/OpenApiReaderException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
|
||
namespace Microsoft.OpenApi.Readers.Exceptions | ||
{ | ||
/// <summary> | ||
/// Defines an exception indicating OpenAPI Reader encountered an issue while reading. | ||
/// </summary> | ||
[Serializable] | ||
public class OpenApiReaderException : Exception | ||
{ | ||
/// <summary> | ||
/// Initializes the <see cref="OpenApiReaderException"/> class. | ||
/// </summary> | ||
public OpenApiReaderException() { } | ||
|
||
/// <summary> | ||
/// Initializes the <see cref="OpenApiReaderException"/> class with a custom message. | ||
/// </summary> | ||
/// <param name="message">Plain text error message for this exception.</param> | ||
public OpenApiReaderException(string message) : base(message) { } | ||
|
||
/// <summary> | ||
/// Initializes the <see cref="OpenApiReaderException"/> class with a custom message and inner exception. | ||
/// </summary> | ||
/// <param name="message">Plain text error message for this exception.</param> | ||
/// <param name="innerException">Inner exception that caused this exception to be thrown.</param> | ||
public OpenApiReaderException(string message, Exception innerException) : base(message, innerException) { } | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/Microsoft.OpenApi.Readers/Exceptions/OpenApiUnsupportedSpecVersionException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
using System.Globalization; | ||
|
||
namespace Microsoft.OpenApi.Readers.Exceptions | ||
{ | ||
/// <summary> | ||
/// Defines an exception indicating OpenAPI Reader encountered an unsupported specification version while reading. | ||
/// </summary> | ||
[Serializable] | ||
public class OpenApiUnsupportedSpecVersionException : OpenApiReaderException | ||
{ | ||
const string messagePattern = "OpenAPI specification version {0} is not supported."; | ||
|
||
/// <summary> | ||
/// Initializes the <see cref="OpenApiUnsupportedSpecVersionException"/> class with a specification version. | ||
/// </summary> | ||
/// <param name="specificationVersion">Version that caused this exception to be thrown.</param> | ||
public OpenApiUnsupportedSpecVersionException(string 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; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes the <see cref="OpenApiUnsupportedSpecVersionException"/> class with a specification version and | ||
/// inner exception. | ||
/// </summary> | ||
/// <param name="specificationVersion">Version that caused this exception to be thrown.</param> | ||
/// <param name="innerException">Inner exception that caused this exception to be thrown.</param> | ||
public OpenApiUnsupportedSpecVersionException(string specificationVersion, Exception 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; | ||
} | ||
|
||
/// <summary> | ||
/// The unsupported specification version. | ||
/// </summary> | ||
public string SpecificationVersion { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/Samples/unsupported.v1.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
swagger: 1.0.0 | ||
info: | ||
title: This is a simple example | ||
version: 1.0.0 | ||
host: example.org | ||
basePath: /api | ||
schemes: ["http", "https"] | ||
paths: {} |
29 changes: 29 additions & 0 deletions
29
test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/UnsupportedSpecVersionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
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")) | ||
{ | ||
try | ||
{ | ||
new OpenApiStreamReader().Read(stream, out var diagnostic); | ||
} | ||
catch (OpenApiUnsupportedSpecVersionException exception) | ||
{ | ||
exception.SpecificationVersion.Should().Be("1.0.0"); | ||
} | ||
} | ||
} | ||
} | ||
} |