Skip to content

Commit cce2495

Browse files
authored
Merge pull request #2028 from microsoft/dev
dev
2 parents 249ff45 + f5fb254 commit cce2495

File tree

10 files changed

+99
-11
lines changed

10 files changed

+99
-11
lines changed

src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<Nullable>enable</Nullable>
1010
<ToolCommandName>hidi</ToolCommandName>
1111
<PackageOutputPath>./../../artifacts</PackageOutputPath>
12-
<Version>2.0.0-preview3</Version>
12+
<Version>2.0.0-preview4</Version>
1313
<Description>OpenAPI.NET CLI tool for slicing OpenAPI documents</Description>
1414
<SignAssembly>true</SignAssembly>
1515
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->

src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>netstandard2.0</TargetFramework>
3+
<TargetFrameworks>netstandard2.0;net6.0;</TargetFrameworks>
4+
<!-- net6.0 target is present because of the conditional build in OpenApiYamlReader.Read -->
45
<LangVersion>latest</LangVersion>
56
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Version>2.0.0-preview3</Version>
7+
<Version>2.0.0-preview4</Version>
78
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
89
<SignAssembly>true</SignAssembly>
910
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->

src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Linq;
1313
using Microsoft.OpenApi.Models;
1414
using System;
15+
using System.Text;
1516

1617
namespace Microsoft.OpenApi.Readers
1718
{
@@ -53,7 +54,13 @@ public ReadResult Read(MemoryStream input,
5354
// Parse the YAML text in the stream into a sequence of JsonNodes
5455
try
5556
{
57+
#if NET
58+
// this represents net core, net5 and up
5659
using var stream = new StreamReader(input, default, true, -1, settings.LeaveStreamOpen);
60+
#else
61+
// the implementation differs and results in a null reference exception in NETFX
62+
using var stream = new StreamReader(input, Encoding.UTF8, true, 4096, settings.LeaveStreamOpen);
63+
#endif
5764
jsonNode = LoadJsonNodesFromYamlDocument(stream);
5865
}
5966
catch (JsonException ex)

src/Microsoft.OpenApi/Microsoft.OpenApi.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFramework>netstandard2.0</TargetFramework>
44
<LangVersion>Latest</LangVersion>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Version>2.0.0-preview3</Version>
6+
<Version>2.0.0-preview4</Version>
77
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
88
<SignAssembly>true</SignAssembly>
99
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->

src/Microsoft.OpenApi/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtension.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Microsoft.OpenApi.Interfaces;
1212
using Microsoft.OpenApi.Writers;
1313
using System.Text.Json.Nodes;
14+
using System.Text.Json;
15+
using System.Globalization;
1416

1517
namespace Microsoft.OpenApi.MicrosoftExtensions;
1618

@@ -97,7 +99,10 @@ public EnumDescription(JsonObject source)
9799
{
98100
if (source is null) throw new ArgumentNullException(nameof(source));
99101
if (source.TryGetPropertyValue(nameof(Value).ToFirstCharacterLowerCase(), out var rawValue) && rawValue is JsonNode value)
100-
Value = value.GetValue<string>();
102+
if (value.GetValueKind() == JsonValueKind.Number)
103+
Value = value.GetValue<decimal>().ToString(CultureInfo.InvariantCulture);
104+
else
105+
Value = value.GetValue<string>();
101106
if (source.TryGetPropertyValue(nameof(Description).ToFirstCharacterLowerCase(), out var rawDescription) && rawDescription is JsonNode description)
102107
Description = description.GetValue<string>();
103108
if (source.TryGetPropertyValue(nameof(Name).ToFirstCharacterLowerCase(), out var rawName) && rawName is JsonNode name)

src/Microsoft.OpenApi/Reader/ReadResult.cs

+8
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,13 @@ public class ReadResult
1818
/// OpenApiDiagnostic contains the Errors reported while parsing
1919
/// </summary>
2020
public OpenApiDiagnostic Diagnostic { get; set; }
21+
/// <summary>
22+
/// Deconstructs the result for easier assignment on the client application.
23+
/// </summary>
24+
public void Deconstruct(out OpenApiDocument document, out OpenApiDiagnostic diagnostic)
25+
{
26+
document = Document;
27+
diagnostic = Diagnostic;
28+
}
2129
}
2230
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
1515
<PackageReference Include="Moq" Version="4.20.72" />
1616
<PackageReference Include="SharpYaml" Version="2.1.1" />
17-
<PackageReference Include="Verify.Xunit" Version="28.6.1" />
17+
<PackageReference Include="Verify.Xunit" Version="28.7.0" />
1818
<PackageReference Include="xunit" Version="2.9.2" />
1919
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0" PrivateAssets="all" />
2020
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />

test/Microsoft.OpenApi.Tests/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtensionTests.cs

+50-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.IO;
2+
using System.Text.Json.Nodes;
23
using Microsoft.OpenApi.MicrosoftExtensions;
34
using Microsoft.OpenApi.Writers;
45
using Xunit;
@@ -22,7 +23,7 @@ public void WritesNothingWhenNoValues()
2223
{
2324
// Arrange
2425
OpenApiEnumValuesDescriptionExtension extension = new();
25-
using TextWriter sWriter = new StringWriter();
26+
using var sWriter = new StringWriter();
2627
OpenApiJsonWriter writer = new(sWriter);
2728

2829
// Act
@@ -41,16 +42,16 @@ public void WritesEnumDescription()
4142
OpenApiEnumValuesDescriptionExtension extension = new()
4243
{
4344
EnumName = "TestEnum",
44-
ValuesDescriptions = new()
45-
{
45+
ValuesDescriptions =
46+
[
4647
new() {
4748
Description = "TestDescription",
4849
Value = "TestValue",
4950
Name = "TestName"
5051
}
51-
}
52+
]
5253
};
53-
using TextWriter sWriter = new StringWriter();
54+
using var sWriter = new StringWriter();
5455
OpenApiJsonWriter writer = new(sWriter);
5556

5657
// Act
@@ -65,5 +66,49 @@ public void WritesEnumDescription()
6566
Assert.Contains("value\": \"TestValue", result);
6667
Assert.Contains("name\": \"TestName", result);
6768
}
69+
[Fact]
70+
public void ParsesEnumDescription()
71+
{
72+
var extensionValue =
73+
"""
74+
{
75+
"value": "Standard_LRS",
76+
"description": "Locally redundant storage.",
77+
"name": "StandardLocalRedundancy"
78+
}
79+
""";
80+
var source = JsonNode.Parse(extensionValue);
81+
Assert.NotNull(source);
82+
var sourceAsObject = source.AsObject();
83+
Assert.NotNull(sourceAsObject);
84+
85+
var descriptionItem = new EnumDescription(sourceAsObject);
86+
Assert.NotNull(descriptionItem);
87+
Assert.Equal("Standard_LRS", descriptionItem.Value);
88+
Assert.Equal("Locally redundant storage.", descriptionItem.Description);
89+
Assert.Equal("StandardLocalRedundancy", descriptionItem.Name);
90+
}
91+
[Fact]
92+
public void ParsesEnumDescriptionWithDecimalValue()
93+
{
94+
var extensionValue =
95+
"""
96+
{
97+
"value": -1,
98+
"description": "Locally redundant storage.",
99+
"name": "StandardLocalRedundancy"
100+
}
101+
""";
102+
var source = JsonNode.Parse(extensionValue);
103+
Assert.NotNull(source);
104+
var sourceAsObject = source.AsObject();
105+
Assert.NotNull(sourceAsObject);
106+
107+
var descriptionItem = new EnumDescription(sourceAsObject);
108+
Assert.NotNull(descriptionItem);
109+
Assert.Equal("-1", descriptionItem.Value);
110+
Assert.Equal("Locally redundant storage.", descriptionItem.Description);
111+
Assert.Equal("StandardLocalRedundancy", descriptionItem.Name);
112+
}
68113
}
69114

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,7 @@ namespace Microsoft.OpenApi.Reader
13801380
public ReadResult() { }
13811381
public Microsoft.OpenApi.Reader.OpenApiDiagnostic Diagnostic { get; set; }
13821382
public Microsoft.OpenApi.Models.OpenApiDocument Document { get; set; }
1383+
public void Deconstruct(out Microsoft.OpenApi.Models.OpenApiDocument document, out Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnostic) { }
13831384
}
13841385
public enum ReferenceResolutionSetting
13851386
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Xunit;
2+
using Microsoft.OpenApi.Reader;
3+
using Microsoft.OpenApi.Models;
4+
5+
namespace Microsoft.OpenApi.Tests.Reader;
6+
7+
public class ReadResultTests
8+
{
9+
[Fact]
10+
public void Deconstructs()
11+
{
12+
var readResult = new ReadResult()
13+
{
14+
Document = new OpenApiDocument(),
15+
Diagnostic = new OpenApiDiagnostic()
16+
};
17+
var (document, diagnostic) = readResult;
18+
Assert.Equal(readResult.Document, document);
19+
Assert.Equal(readResult.Diagnostic, diagnostic);
20+
}
21+
}

0 commit comments

Comments
 (0)