Skip to content

Commit 6377257

Browse files
authored
[Fix 177]: Throw exception when spec version is not recognized by reader (#181)
Throwing OpenApiUnsupportedSpecVersionException when spec version is not recognized in Reader
1 parent ba575c4 commit 6377257

File tree

8 files changed

+146
-21
lines changed

8 files changed

+146
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
6+
namespace Microsoft.OpenApi.Readers.Exceptions
7+
{
8+
/// <summary>
9+
/// Defines an exception indicating OpenAPI Reader encountered an issue while reading.
10+
/// </summary>
11+
[Serializable]
12+
public class OpenApiReaderException : Exception
13+
{
14+
/// <summary>
15+
/// Initializes the <see cref="OpenApiReaderException"/> class.
16+
/// </summary>
17+
public OpenApiReaderException() { }
18+
19+
/// <summary>
20+
/// Initializes the <see cref="OpenApiReaderException"/> class with a custom message.
21+
/// </summary>
22+
/// <param name="message">Plain text error message for this exception.</param>
23+
public OpenApiReaderException(string message) : base(message) { }
24+
25+
/// <summary>
26+
/// Initializes the <see cref="OpenApiReaderException"/> class with a custom message and inner exception.
27+
/// </summary>
28+
/// <param name="message">Plain text error message for this exception.</param>
29+
/// <param name="innerException">Inner exception that caused this exception to be thrown.</param>
30+
public OpenApiReaderException(string message, Exception innerException) : base(message, innerException) { }
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.Globalization;
6+
7+
namespace Microsoft.OpenApi.Readers.Exceptions
8+
{
9+
/// <summary>
10+
/// Defines an exception indicating OpenAPI Reader encountered an unsupported specification version while reading.
11+
/// </summary>
12+
[Serializable]
13+
public class OpenApiUnsupportedSpecVersionException : OpenApiReaderException
14+
{
15+
const string messagePattern = "OpenAPI specification version {0} is not supported.";
16+
17+
/// <summary>
18+
/// Initializes the <see cref="OpenApiUnsupportedSpecVersionException"/> class with a specification version.
19+
/// </summary>
20+
/// <param name="specificationVersion">Version that caused this exception to be thrown.</param>
21+
public OpenApiUnsupportedSpecVersionException(string specificationVersion)
22+
: base(string.Format(CultureInfo.InvariantCulture, messagePattern, specificationVersion))
23+
{
24+
if (string.IsNullOrWhiteSpace(specificationVersion))
25+
{
26+
throw new ArgumentException("Value cannot be null or white space.", nameof(specificationVersion));
27+
}
28+
29+
this.SpecificationVersion = specificationVersion;
30+
}
31+
32+
/// <summary>
33+
/// Initializes the <see cref="OpenApiUnsupportedSpecVersionException"/> class with a specification version and
34+
/// inner exception.
35+
/// </summary>
36+
/// <param name="specificationVersion">Version that caused this exception to be thrown.</param>
37+
/// <param name="innerException">Inner exception that caused this exception to be thrown.</param>
38+
public OpenApiUnsupportedSpecVersionException(string specificationVersion, Exception innerException)
39+
: base(string.Format(CultureInfo.InvariantCulture, messagePattern, specificationVersion), innerException)
40+
{
41+
if (string.IsNullOrWhiteSpace(specificationVersion))
42+
{
43+
throw new ArgumentException("Value cannot be null or white space.", nameof(specificationVersion));
44+
}
45+
46+
this.SpecificationVersion = specificationVersion;
47+
}
48+
49+
/// <summary>
50+
/// The unsupported specification version.
51+
/// </summary>
52+
public string SpecificationVersion { get; }
53+
}
54+
}

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-beta010</Version>
13+
<Version>1.0.0-beta011</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/ParsingContext.cs

+18-19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using Microsoft.OpenApi.Interfaces;
88
using Microsoft.OpenApi.Models;
9+
using Microsoft.OpenApi.Readers.Exceptions;
910
using Microsoft.OpenApi.Readers.Interface;
1011
using Microsoft.OpenApi.Readers.ParseNodes;
1112
using Microsoft.OpenApi.Readers.V2;
@@ -41,26 +42,24 @@ internal OpenApiDocument Parse(YamlDocument yamlDocument, OpenApiDiagnostic diag
4142

4243
OpenApiDocument doc;
4344

44-
if (inputVersion == "2.0")
45+
switch (inputVersion)
4546
{
46-
VersionService = new OpenApiV2VersionService();
47-
doc = this.VersionService.LoadDocument(this.RootNode);
48-
diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi2_0;
49-
}
50-
else if (inputVersion.StartsWith("3.0."))
51-
{
52-
this.VersionService = new OpenApiV3VersionService();
53-
doc = this.VersionService.LoadDocument(this.RootNode);
54-
diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi3_0;
55-
}
56-
else
57-
{
58-
// If version number is not recognizable,
59-
// our best effort will try to deserialize the document to V3.
60-
this.VersionService = new OpenApiV3VersionService();
61-
doc = this.VersionService.LoadDocument(this.RootNode);
62-
diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi3_0;
47+
case string version when version == "2.0":
48+
VersionService = new OpenApiV2VersionService();
49+
doc = this.VersionService.LoadDocument(this.RootNode);
50+
diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi2_0;
51+
break;
52+
53+
case string version when version.StartsWith("3.0"):
54+
this.VersionService = new OpenApiV3VersionService();
55+
doc = this.VersionService.LoadDocument(this.RootNode);
56+
diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi3_0;
57+
break;
58+
59+
default:
60+
throw new OpenApiUnsupportedSpecVersionException(inputVersion);
6361
}
62+
6463
return doc;
6564
}
6665

@@ -81,7 +80,7 @@ private static string GetVersion(RootNode rootNode)
8180
return versionNode?.GetScalarValue();
8281
}
8382

84-
private void ComputeTags(List<OpenApiTag> tags, Func<MapNode,OpenApiTag> loadTag )
83+
private void ComputeTags(List<OpenApiTag> tags, Func<MapNode, OpenApiTag> loadTag)
8584
{
8685
// Precompute the tags array so that each tag reference does not require a new deserialization.
8786
var tagListPointer = new JsonPointer("#/tags");

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-beta010</Version>
13+
<Version>1.0.0-beta011</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>

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

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<AssemblyOriginatorKeyFile>..\..\src\Microsoft.OpenApi.snk</AssemblyOriginatorKeyFile>
1414
</PropertyGroup>
1515
<ItemGroup>
16+
<EmbeddedResource Include="OpenApiReaderTests\Samples\unsupported.v1.yaml">
17+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
18+
</EmbeddedResource>
1619
<EmbeddedResource Include="ReferenceService\Samples\multipleReferences.v2.yaml">
1720
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
1821
</EmbeddedResource>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
swagger: 1.0.0
2+
info:
3+
title: This is a simple example
4+
version: 1.0.0
5+
host: example.org
6+
basePath: /api
7+
schemes: ["http", "https"]
8+
paths: {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using FluentAssertions;
5+
using Microsoft.OpenApi.Readers.Exceptions;
6+
using Xunit;
7+
8+
namespace Microsoft.OpenApi.Readers.Tests.OpenApiReaderTests
9+
{
10+
[Collection("DefaultSettings")]
11+
public class UnsupportedSpecVersionTests
12+
{
13+
[Fact]
14+
public void ThrowOpenApiUnsupportedSpecVersionException()
15+
{
16+
using (var stream = Resources.GetStream("OpenApiReaderTests/Samples/unsupported.v1.yaml"))
17+
{
18+
try
19+
{
20+
new OpenApiStreamReader().Read(stream, out var diagnostic);
21+
}
22+
catch (OpenApiUnsupportedSpecVersionException exception)
23+
{
24+
exception.SpecificationVersion.Should().Be("1.0.0");
25+
}
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)