diff --git a/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj b/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj index 04c42ee47..52672405e 100644 --- a/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj +++ b/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj @@ -9,7 +9,7 @@ enable hidi ./../../artifacts - 2.0.0-preview3 + 2.0.0-preview4 OpenAPI.NET CLI tool for slicing OpenAPI documents true diff --git a/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj b/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj index 05e3e52e6..94f633208 100644 --- a/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj +++ b/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj @@ -1,9 +1,10 @@ - netstandard2.0 + netstandard2.0;net6.0; + latest true - 2.0.0-preview3 + 2.0.0-preview4 OpenAPI.NET Readers for JSON and YAML documents true diff --git a/src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs b/src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs index 95e58c52f..217db91b3 100644 --- a/src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs +++ b/src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs @@ -12,6 +12,7 @@ using System.Linq; using Microsoft.OpenApi.Models; using System; +using System.Text; namespace Microsoft.OpenApi.Readers { @@ -53,7 +54,13 @@ public ReadResult Read(MemoryStream input, // Parse the YAML text in the stream into a sequence of JsonNodes try { +#if NET +// this represents net core, net5 and up using var stream = new StreamReader(input, default, true, -1, settings.LeaveStreamOpen); +#else +// the implementation differs and results in a null reference exception in NETFX + using var stream = new StreamReader(input, Encoding.UTF8, true, 4096, settings.LeaveStreamOpen); +#endif jsonNode = LoadJsonNodesFromYamlDocument(stream); } catch (JsonException ex) diff --git a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj index 5c4e18a29..e0b815f5d 100644 --- a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj +++ b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj @@ -3,7 +3,7 @@ netstandard2.0 Latest true - 2.0.0-preview3 + 2.0.0-preview4 .NET models with JSON and YAML writers for OpenAPI specification true diff --git a/src/Microsoft.OpenApi/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtension.cs b/src/Microsoft.OpenApi/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtension.cs index 1235e68b0..a6df2444b 100644 --- a/src/Microsoft.OpenApi/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtension.cs +++ b/src/Microsoft.OpenApi/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtension.cs @@ -11,6 +11,8 @@ using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Writers; using System.Text.Json.Nodes; +using System.Text.Json; +using System.Globalization; namespace Microsoft.OpenApi.MicrosoftExtensions; @@ -97,7 +99,10 @@ public EnumDescription(JsonObject source) { if (source is null) throw new ArgumentNullException(nameof(source)); if (source.TryGetPropertyValue(nameof(Value).ToFirstCharacterLowerCase(), out var rawValue) && rawValue is JsonNode value) - Value = value.GetValue(); + if (value.GetValueKind() == JsonValueKind.Number) + Value = value.GetValue().ToString(CultureInfo.InvariantCulture); + else + Value = value.GetValue(); if (source.TryGetPropertyValue(nameof(Description).ToFirstCharacterLowerCase(), out var rawDescription) && rawDescription is JsonNode description) Description = description.GetValue(); if (source.TryGetPropertyValue(nameof(Name).ToFirstCharacterLowerCase(), out var rawName) && rawName is JsonNode name) diff --git a/src/Microsoft.OpenApi/Reader/ReadResult.cs b/src/Microsoft.OpenApi/Reader/ReadResult.cs index a0013b249..aa835478a 100644 --- a/src/Microsoft.OpenApi/Reader/ReadResult.cs +++ b/src/Microsoft.OpenApi/Reader/ReadResult.cs @@ -18,5 +18,13 @@ public class ReadResult /// OpenApiDiagnostic contains the Errors reported while parsing /// public OpenApiDiagnostic Diagnostic { get; set; } + /// + /// Deconstructs the result for easier assignment on the client application. + /// + public void Deconstruct(out OpenApiDocument document, out OpenApiDiagnostic diagnostic) + { + document = Document; + diagnostic = Diagnostic; + } } } diff --git a/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj b/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj index 59f05da32..634c70257 100644 --- a/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj +++ b/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj @@ -14,7 +14,7 @@ - + diff --git a/test/Microsoft.OpenApi.Tests/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtensionTests.cs b/test/Microsoft.OpenApi.Tests/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtensionTests.cs index 3b9f9964a..f09e07657 100644 --- a/test/Microsoft.OpenApi.Tests/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtensionTests.cs +++ b/test/Microsoft.OpenApi.Tests/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtensionTests.cs @@ -1,4 +1,5 @@ using System.IO; +using System.Text.Json.Nodes; using Microsoft.OpenApi.MicrosoftExtensions; using Microsoft.OpenApi.Writers; using Xunit; @@ -22,7 +23,7 @@ public void WritesNothingWhenNoValues() { // Arrange OpenApiEnumValuesDescriptionExtension extension = new(); - using TextWriter sWriter = new StringWriter(); + using var sWriter = new StringWriter(); OpenApiJsonWriter writer = new(sWriter); // Act @@ -41,16 +42,16 @@ public void WritesEnumDescription() OpenApiEnumValuesDescriptionExtension extension = new() { EnumName = "TestEnum", - ValuesDescriptions = new() - { + ValuesDescriptions = + [ new() { Description = "TestDescription", Value = "TestValue", Name = "TestName" } - } + ] }; - using TextWriter sWriter = new StringWriter(); + using var sWriter = new StringWriter(); OpenApiJsonWriter writer = new(sWriter); // Act @@ -65,5 +66,49 @@ public void WritesEnumDescription() Assert.Contains("value\": \"TestValue", result); Assert.Contains("name\": \"TestName", result); } + [Fact] + public void ParsesEnumDescription() + { + var extensionValue = +""" +{ + "value": "Standard_LRS", + "description": "Locally redundant storage.", + "name": "StandardLocalRedundancy" +} +"""; + var source = JsonNode.Parse(extensionValue); + Assert.NotNull(source); + var sourceAsObject = source.AsObject(); + Assert.NotNull(sourceAsObject); + + var descriptionItem = new EnumDescription(sourceAsObject); + Assert.NotNull(descriptionItem); + Assert.Equal("Standard_LRS", descriptionItem.Value); + Assert.Equal("Locally redundant storage.", descriptionItem.Description); + Assert.Equal("StandardLocalRedundancy", descriptionItem.Name); + } + [Fact] + public void ParsesEnumDescriptionWithDecimalValue() + { + var extensionValue = +""" +{ + "value": -1, + "description": "Locally redundant storage.", + "name": "StandardLocalRedundancy" +} +"""; + var source = JsonNode.Parse(extensionValue); + Assert.NotNull(source); + var sourceAsObject = source.AsObject(); + Assert.NotNull(sourceAsObject); + + var descriptionItem = new EnumDescription(sourceAsObject); + Assert.NotNull(descriptionItem); + Assert.Equal("-1", descriptionItem.Value); + Assert.Equal("Locally redundant storage.", descriptionItem.Description); + Assert.Equal("StandardLocalRedundancy", descriptionItem.Name); + } } diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index a3681e9d5..642dd0b82 100644 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -1380,6 +1380,7 @@ namespace Microsoft.OpenApi.Reader public ReadResult() { } public Microsoft.OpenApi.Reader.OpenApiDiagnostic Diagnostic { get; set; } public Microsoft.OpenApi.Models.OpenApiDocument Document { get; set; } + public void Deconstruct(out Microsoft.OpenApi.Models.OpenApiDocument document, out Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnostic) { } } public enum ReferenceResolutionSetting { diff --git a/test/Microsoft.OpenApi.Tests/Reader/ReadResultTests.cs b/test/Microsoft.OpenApi.Tests/Reader/ReadResultTests.cs new file mode 100644 index 000000000..c3a87c7a3 --- /dev/null +++ b/test/Microsoft.OpenApi.Tests/Reader/ReadResultTests.cs @@ -0,0 +1,21 @@ +using Xunit; +using Microsoft.OpenApi.Reader; +using Microsoft.OpenApi.Models; + +namespace Microsoft.OpenApi.Tests.Reader; + +public class ReadResultTests +{ + [Fact] + public void Deconstructs() + { + var readResult = new ReadResult() + { + Document = new OpenApiDocument(), + Diagnostic = new OpenApiDiagnostic() + }; + var (document, diagnostic) = readResult; + Assert.Equal(readResult.Document, document); + Assert.Equal(readResult.Diagnostic, diagnostic); + } +}