Skip to content

Commit

Permalink
Merge pull request #2028 from microsoft/dev
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
baywet authored Dec 24, 2024
2 parents 249ff45 + f5fb254 commit cce2495
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Nullable>enable</Nullable>
<ToolCommandName>hidi</ToolCommandName>
<PackageOutputPath>./../../artifacts</PackageOutputPath>
<Version>2.0.0-preview3</Version>
<Version>2.0.0-preview4</Version>
<Description>OpenAPI.NET CLI tool for slicing OpenAPI documents</Description>
<SignAssembly>true</SignAssembly>
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net6.0;</TargetFrameworks>
<!-- net6.0 target is present because of the conditional build in OpenApiYamlReader.Read -->
<LangVersion>latest</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.0.0-preview3</Version>
<Version>2.0.0-preview4</Version>
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
<SignAssembly>true</SignAssembly>
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->
Expand Down
7 changes: 7 additions & 0 deletions src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Linq;
using Microsoft.OpenApi.Models;
using System;
using System.Text;

namespace Microsoft.OpenApi.Readers
{
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>Latest</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.0.0-preview3</Version>
<Version>2.0.0-preview4</Version>
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
<SignAssembly>true</SignAssembly>
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<string>();
if (value.GetValueKind() == JsonValueKind.Number)
Value = value.GetValue<decimal>().ToString(CultureInfo.InvariantCulture);
else
Value = value.GetValue<string>();
if (source.TryGetPropertyValue(nameof(Description).ToFirstCharacterLowerCase(), out var rawDescription) && rawDescription is JsonNode description)
Description = description.GetValue<string>();
if (source.TryGetPropertyValue(nameof(Name).ToFirstCharacterLowerCase(), out var rawName) && rawName is JsonNode name)
Expand Down
8 changes: 8 additions & 0 deletions src/Microsoft.OpenApi/Reader/ReadResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,13 @@ public class ReadResult
/// OpenApiDiagnostic contains the Errors reported while parsing
/// </summary>
public OpenApiDiagnostic Diagnostic { get; set; }
/// <summary>
/// Deconstructs the result for easier assignment on the client application.
/// </summary>
public void Deconstruct(out OpenApiDocument document, out OpenApiDiagnostic diagnostic)
{
document = Document;
diagnostic = Diagnostic;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="SharpYaml" Version="2.1.1" />
<PackageReference Include="Verify.Xunit" Version="28.6.1" />
<PackageReference Include="Verify.Xunit" Version="28.7.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.MicrosoftExtensions;
using Microsoft.OpenApi.Writers;
using Xunit;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
21 changes: 21 additions & 0 deletions test/Microsoft.OpenApi.Tests/Reader/ReadResultTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit cce2495

Please sign in to comment.