Skip to content

Commit e28c438

Browse files
authored
Merge pull request #312 from Microsoft/dm/release1.1
Release 1.1
2 parents d793955 + d0acd74 commit e28c438

File tree

115 files changed

+1373
-341
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+1373
-341
lines changed

build.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@echo off
22
if "%~1"=="" goto :error
33

4-
SET %VERSION%=%~1
4+
SET VERSION=%~1
55

66
Echo Building Microsoft.OpenApi
77

src/Directory.Build.props

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project>
2+
<PropertyGroup>
3+
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-*" PrivateAssets="All"/>
7+
</ItemGroup>
8+
</Project>

src/Microsoft.OpenApi.Readers/Exceptions/OpenApiReaderException.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
// Licensed under the MIT license.
33

44
using System;
5+
using Microsoft.OpenApi.Exceptions;
6+
using SharpYaml.Serialization;
57

68
namespace Microsoft.OpenApi.Readers.Exceptions
79
{
810
/// <summary>
911
/// Defines an exception indicating OpenAPI Reader encountered an issue while reading.
1012
/// </summary>
1113
[Serializable]
12-
public class OpenApiReaderException : Exception
14+
public class OpenApiReaderException : OpenApiException
1315
{
1416
/// <summary>
1517
/// Initializes the <see cref="OpenApiReaderException"/> class.
@@ -22,6 +24,18 @@ public OpenApiReaderException() { }
2224
/// <param name="message">Plain text error message for this exception.</param>
2325
public OpenApiReaderException(string message) : base(message) { }
2426

27+
/// <summary>
28+
/// Initializes the <see cref="OpenApiReaderException"/> class with a message and line, column location of error.
29+
/// </summary>
30+
/// <param name="message">Plain text error message for this exception.</param>
31+
/// <param name="node">Parsing node where error occured</param>
32+
public OpenApiReaderException(string message, YamlNode node) : base(message)
33+
{
34+
// This only includes line because using a char range causes tests to break due to CR/LF & LF differences
35+
// See https://tools.ietf.org/html/rfc5147 for syntax
36+
Pointer = $"#line={node.Start.Line}";
37+
}
38+
2539
/// <summary>
2640
/// Initializes the <see cref="OpenApiReaderException"/> class with a custom message and inner exception.
2741
/// </summary>

src/Microsoft.OpenApi.Readers/Exceptions/OpenApiUnsupportedSpecVersionException.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ namespace Microsoft.OpenApi.Readers.Exceptions
1010
/// Defines an exception indicating OpenAPI Reader encountered an unsupported specification version while reading.
1111
/// </summary>
1212
[Serializable]
13-
public class OpenApiUnsupportedSpecVersionException : OpenApiReaderException
13+
public class OpenApiUnsupportedSpecVersionException : Exception
1414
{
15-
const string messagePattern = "OpenAPI specification version {0} is not supported.";
15+
const string messagePattern = "OpenAPI specification version '{0}' is not supported.";
1616

1717
/// <summary>
1818
/// Initializes the <see cref="OpenApiUnsupportedSpecVersionException"/> class with a specification version.
@@ -21,11 +21,6 @@ public class OpenApiUnsupportedSpecVersionException : OpenApiReaderException
2121
public OpenApiUnsupportedSpecVersionException(string specificationVersion)
2222
: base(string.Format(CultureInfo.InvariantCulture, messagePattern, specificationVersion))
2323
{
24-
if (string.IsNullOrWhiteSpace(specificationVersion))
25-
{
26-
throw new ArgumentException("Value cannot be null or white space.", nameof(specificationVersion));
27-
}
28-
2924
this.SpecificationVersion = specificationVersion;
3025
}
3126

@@ -38,11 +33,6 @@ public OpenApiUnsupportedSpecVersionException(string specificationVersion)
3833
public OpenApiUnsupportedSpecVersionException(string specificationVersion, Exception innerException)
3934
: base(string.Format(CultureInfo.InvariantCulture, messagePattern, specificationVersion), innerException)
4035
{
41-
if (string.IsNullOrWhiteSpace(specificationVersion))
42-
{
43-
throw new ArgumentException("Value cannot be null or white space.", nameof(specificationVersion));
44-
}
45-
4636
this.SpecificationVersion = specificationVersion;
4737
}
4838

src/Microsoft.OpenApi.Readers/Interface/IOpenApiVersionService.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ internal interface IOpenApiVersionService
2323
OpenApiReference ConvertToOpenApiReference(string reference, ReferenceType? type);
2424

2525
/// <summary>
26-
/// Function that converts a MapNode into a Tag object in a version specific way
26+
/// Loads an OpenAPI Element from a document fragment
2727
/// </summary>
28-
Func<MapNode, OpenApiTag> TagLoader { get; }
28+
/// <typeparam name="T">Type of element to load</typeparam>
29+
/// <param name="node">document fragment node</param>
30+
/// <returns>Instance of OpenAPIElement</returns>
31+
T LoadElement<T>(ParseNode node) where T : IOpenApiElement;
2932

3033
/// <summary>
3134
/// Converts a generic RootNode instance into a strongly typed OpenApiDocument

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.1</Version>
13+
<Version>1.1.0</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/OpenApiReaderError.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/Microsoft.OpenApi.Readers/OpenApiReaderSettings.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,16 @@ public class OpenApiReaderSettings
4242
/// <summary>
4343
/// Dictionary of parsers for converting extensions into strongly typed classes
4444
/// </summary>
45-
public Dictionary<string, Func<IOpenApiAny , IOpenApiExtension>> ExtensionParsers { get; set; } = new Dictionary<string, Func<IOpenApiAny, IOpenApiExtension>>();
45+
public Dictionary<string, Func<IOpenApiAny , OpenApiSpecVersion, IOpenApiExtension>> ExtensionParsers { get; set; } = new Dictionary<string, Func<IOpenApiAny, OpenApiSpecVersion, IOpenApiExtension>>();
4646

4747
/// <summary>
4848
/// Rules to use for validating OpenAPI specification. If none are provided a default set of rules are applied.
4949
/// </summary>
5050
public ValidationRuleSet RuleSet { get; set; } = ValidationRuleSet.GetDefaultRuleSet();
5151

52+
/// <summary>
53+
/// URL where relative references should be resolved from if the description does not contain Server definitions
54+
/// </summary>
55+
public Uri BaseUrl { get; set; }
5256
}
5357
}

src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using Microsoft.OpenApi.Exceptions;
88
using Microsoft.OpenApi.Extensions;
9+
using Microsoft.OpenApi.Interfaces;
910
using Microsoft.OpenApi.Models;
1011
using Microsoft.OpenApi.Readers.Interface;
1112
using Microsoft.OpenApi.Readers.Services;
@@ -48,15 +49,16 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
4849
{
4950
yamlDocument = LoadYamlDocument(input);
5051
}
51-
catch (SyntaxErrorException ex)
52+
catch (YamlException ex)
5253
{
53-
diagnostic.Errors.Add(new OpenApiReaderError(ex));
54+
diagnostic.Errors.Add(new OpenApiError($"#char={ex.Start.Line}", ex.Message));
5455
return new OpenApiDocument();
5556
}
5657

5758
context = new ParsingContext
5859
{
59-
ExtensionParsers = _settings.ExtensionParsers
60+
ExtensionParsers = _settings.ExtensionParsers,
61+
BaseUrl = _settings.BaseUrl
6062
};
6163

6264
OpenApiDocument document = null;
@@ -102,6 +104,60 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
102104
return document;
103105
}
104106

107+
/// <summary>
108+
/// Reads the stream input and parses the fragment of an OpenAPI description into an Open API Element.
109+
/// </summary>
110+
/// <param name="input">Stream containing OpenAPI description to parse.</param>
111+
/// <param name="version">Version of the OpenAPI specification that the fragment conforms to.</param>
112+
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing</param>
113+
/// <returns>Instance of newly created OpenApiDocument</returns>
114+
public T ReadFragment<T>(Stream input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic) where T : IOpenApiElement
115+
{
116+
ParsingContext context;
117+
YamlDocument yamlDocument;
118+
diagnostic = new OpenApiDiagnostic();
119+
120+
// Parse the YAML/JSON
121+
try
122+
{
123+
yamlDocument = LoadYamlDocument(input);
124+
}
125+
catch (YamlException ex)
126+
{
127+
diagnostic.Errors.Add(new OpenApiError($"#line={ex.Start.Line}", ex.Message));
128+
return default(T);
129+
}
130+
131+
context = new ParsingContext
132+
{
133+
ExtensionParsers = _settings.ExtensionParsers
134+
};
135+
136+
IOpenApiElement element = null;
137+
138+
try
139+
{
140+
// Parse the OpenAPI element
141+
element = context.ParseFragment<T>(yamlDocument, version, diagnostic);
142+
}
143+
catch (OpenApiException ex)
144+
{
145+
diagnostic.Errors.Add(new OpenApiError(ex));
146+
}
147+
148+
// Validate the element
149+
if (_settings.RuleSet != null && _settings.RuleSet.Rules.Count > 0)
150+
{
151+
var errors = element.Validate(_settings.RuleSet);
152+
foreach (var item in errors)
153+
{
154+
diagnostic.Errors.Add(item);
155+
}
156+
}
157+
158+
return (T)element;
159+
}
160+
105161
/// <summary>
106162
/// Helper method to turn streams into YamlDocument
107163
/// </summary>

src/Microsoft.OpenApi.Readers/OpenApiStringReader.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
using System.IO;
5+
using Microsoft.OpenApi.Interfaces;
56
using Microsoft.OpenApi.Models;
67
using Microsoft.OpenApi.Readers.Interface;
78

@@ -20,7 +21,7 @@ public class OpenApiStringReader : IOpenApiReader<string, OpenApiDiagnostic>
2021
/// <param name="settings"></param>
2122
public OpenApiStringReader(OpenApiReaderSettings settings = null)
2223
{
23-
_settings = settings ?? new OpenApiReaderSettings();
24+
_settings = settings ?? new OpenApiReaderSettings();
2425
}
2526

2627
/// <summary>
@@ -38,5 +39,21 @@ public OpenApiDocument Read(string input, out OpenApiDiagnostic diagnostic)
3839
return new OpenApiStreamReader(_settings).Read(memoryStream, out diagnostic);
3940
}
4041
}
42+
43+
/// <summary>
44+
/// Reads the string input and parses it into an Open API element.
45+
/// </summary>
46+
public T ReadFragment<T>(string input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic) where T : IOpenApiElement
47+
{
48+
using (var memoryStream = new MemoryStream())
49+
{
50+
var writer = new StreamWriter(memoryStream);
51+
writer.Write(input);
52+
writer.Flush();
53+
memoryStream.Position = 0;
54+
55+
return new OpenApiStreamReader(_settings).ReadFragment<T>(memoryStream, version, out diagnostic);
56+
}
57+
}
4158
}
4259
}

0 commit comments

Comments
 (0)