Skip to content

Commit 728d061

Browse files
committed
Merge from master
2 parents 77586ac + b9ba6bb commit 728d061

File tree

9 files changed

+152
-29
lines changed

9 files changed

+152
-29
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
![Category overview screenshot](docs/images/oainet.png "Microsoft + OpenAPI = Love")
22

33
# OpenAPI.NET [Preview]
4-
[ Disclaimer: This repository is in a preview state. Expect to see some iterating as we work towards the final release candidate slated for early 2018. Feedback is welcome! ]
4+
[Disclaimer: This repository is in a preview state. Expect to see some iterating as we work towards the final release candidate slated for early 2018. Feedback is welcome!]
55

66
The **OpenAPI.NET** SDK contains a useful object model for OpenAPI documents in .NET along with common serializers to extract raw OpenAPI JSON and YAML documents from the model.
77

8-
**See more information on the OpenAPI spec and its history here: <a href="https://www.openapis.org">Open API Initiative</a>**
8+
**See more information on the OpenAPI specification and its history here: <a href="https://www.openapis.org">Open API Initiative</a>**
99

1010
Project Objectives
1111

@@ -73,7 +73,6 @@ var stream = await httpClient.GetStreamAsync("master/examples/v3.0/petstore.yaml
7373
// Read V3 as YAML
7474
var openApiDocument = new OpenApiStreamReader().Read(stream, out var diagnostic);
7575

76-
7776
// Write V2 as JSON
7877
var outputString = openApiDocument.Serialize(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Json);
7978

@@ -99,4 +98,4 @@ This project has adopted the [Microsoft Open Source Code of Conduct](https://ope
9998
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
10099
contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
101100

102-
To provide feedback and ask questions you can use StackOverflow with the [OpenApi.net](https://stackoverflow.com/questions/tagged/openapi.net) tag or use the OpenApi.net Slack channel which you can join by registering for the httpapis team at http://slack.httpapis.com
101+
To provide feedback and ask questions you can use Stack Overflow with the [OpenAPI.NET](https://stackoverflow.com/questions/tagged/openapi.net) tag or use the OpenAPI.NET Slack channel which you can join by registering for the HTTP APIs team at http://slack.httpapis.com.
Lines changed: 32 additions & 0 deletions
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+
}
Lines changed: 54 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 18 additions & 19 deletions
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;
@@ -43,26 +44,24 @@ internal OpenApiDocument Parse(YamlDocument yamlDocument, OpenApiDiagnostic diag
4344

4445
OpenApiDocument doc;
4546

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

@@ -83,7 +82,7 @@ private static string GetVersion(RootNode rootNode)
8382
return versionNode?.GetScalarValue();
8483
}
8584

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

src/Microsoft.OpenApi/Microsoft.OpenApi.csproj

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
<AssemblyOriginatorKeyFile>..\..\src\Microsoft.OpenApi.snk</AssemblyOriginatorKeyFile>
1414
</PropertyGroup>
1515
<ItemGroup>
16-
<None Remove="V3Tests\Samples\OpenApiSchema\selfReferencingSchema.yaml" />
17-
</ItemGroup>
18-
<ItemGroup>
16+
<EmbeddedResource Include="OpenApiReaderTests\Samples\unsupported.v1.yaml">
17+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
18+
</EmbeddedResource>
1919
<EmbeddedResource Include="ReferenceService\Samples\multipleReferences.v2.yaml">
2020
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
2121
</EmbeddedResource>
@@ -109,7 +109,9 @@
109109
<EmbeddedResource Include="V3Tests\Samples\OpenApiSchema\primitiveSchema.yaml">
110110
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
111111
</EmbeddedResource>
112-
<EmbeddedResource Include="V3Tests\Samples\OpenApiSchema\selfReferencingSchema.yaml" />
112+
<EmbeddedResource Include="V3Tests\Samples\OpenApiSchema\selfReferencingSchema.yaml">
113+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
114+
</EmbeddedResource>
113115
<EmbeddedResource Include="V3Tests\Samples\OpenApiSchema\simpleSchema.yaml">
114116
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
115117
</EmbeddedResource>
Lines changed: 8 additions & 0 deletions
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: {}
Lines changed: 29 additions & 0 deletions
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)