Skip to content

Commit

Permalink
Merge pull request #1893 from microsoft/merge/vnext-to-release2
Browse files Browse the repository at this point in the history
merge/vnext to release2
  • Loading branch information
baywet authored Oct 24, 2024
2 parents c3373af + fe13c56 commit ab0ecf9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 42 deletions.
10 changes: 5 additions & 5 deletions 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>1.4.11</Version>
<Version>1.4.13</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 All @@ -31,15 +31,15 @@
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.11.20">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="Microsoft.OData.Edm" Version="8.0.2" />
<PackageReference Include="Microsoft.OpenApi.OData" Version="2.0.0-preview.3" />
<PackageReference Include="Microsoft.OData.Edm" Version="8.1.0" />
<PackageReference Include="Microsoft.OpenApi.OData" Version="2.0.0-preview.6" />
<PackageReference Include="Microsoft.OpenApi.ApiManifest" Version="0.5.0-preview" />
<PackageReference Include="System.CommandLine.Hosting" Version="0.4.0-alpha.22272.1" />
<!--STJ
Expand Down
29 changes: 15 additions & 14 deletions src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public static class OpenApiTypeMapper
{
[typeof(bool)] = () => new() { Type = "boolean" },
[typeof(byte)] = () => new() { Type = "string", Format = "byte" },
[typeof(int)] = () => new() { Type = "integer", Format = "int32" },
[typeof(uint)] = () => new() { Type = "integer", Format = "int32" },
[typeof(long)] = () => new() { Type = "integer", Format = "int64" },
[typeof(ulong)] = () => new() { Type = "integer", Format = "int64" },
[typeof(int)] = () => new() { Type = "number", Format = "int32" },
[typeof(uint)] = () => new() { Type = "number", Format = "int32" },
[typeof(long)] = () => new() { Type = "number", Format = "int64" },
[typeof(ulong)] = () => new() { Type = "number", Format = "int64" },
[typeof(float)] = () => new() { Type = "number", Format = "float" },
[typeof(double)] = () => new() { Type = "number", Format = "double" },
[typeof(decimal)] = () => new() { Type = "number", Format = "double" },
Expand All @@ -31,10 +31,10 @@ public static class OpenApiTypeMapper
// Nullable types
[typeof(bool?)] = () => new() { Type = "boolean", Nullable = true },
[typeof(byte?)] = () => new() { Type = "string", Format = "byte", Nullable = true },
[typeof(int?)] = () => new() { Type = "integer", Format = "int32", Nullable = true },
[typeof(uint?)] = () => new() { Type = "integer", Format = "int32", Nullable = true },
[typeof(long?)] = () => new() { Type = "integer", Format = "int64", Nullable = true },
[typeof(ulong?)] = () => new() { Type = "integer", Format = "int64", Nullable = true },
[typeof(int?)] = () => new() { Type = "number", Format = "int32", Nullable = true },
[typeof(uint?)] = () => new() { Type = "number", Format = "int32", Nullable = true },
[typeof(long?)] = () => new() { Type = "number", Format = "int64", Nullable = true },
[typeof(ulong?)] = () => new() { Type = "number", Format = "int64", Nullable = true },
[typeof(float?)] = () => new() { Type = "number", Format = "float", Nullable = true },
[typeof(double?)] = () => new() { Type = "number", Format = "double", Nullable = true },
[typeof(decimal?)] = () => new() { Type = "number", Format = "double", Nullable = true },
Expand Down Expand Up @@ -98,9 +98,10 @@ public static Type MapOpenApiPrimitiveTypeToSimpleType(this OpenApiSchema schema
var type = (schema.Type?.ToString().ToLowerInvariant(), schema.Format?.ToLowerInvariant(), schema.Nullable) switch
{
("boolean", null, false) => typeof(bool),
("integer", "int32", false) => typeof(int),
("integer", "int64", false) => typeof(long),
("integer", null, false) => typeof(int),
// integer is technically not valid with format, but we must provide some compatibility
("integer" or "number", "int32", false) => typeof(int),
("integer" or "number", "int64", false) => typeof(long),
("integer", null, false) => typeof(long),
("number", "float", false) => typeof(float),
("number", "double", false) => typeof(double),
("number", "decimal", false) => typeof(decimal),
Expand All @@ -113,9 +114,9 @@ public static Type MapOpenApiPrimitiveTypeToSimpleType(this OpenApiSchema schema
("string", null, false) => typeof(string),
("object", null, false) => typeof(object),
("string", "uri", false) => typeof(Uri),
("integer", "int32", true) => typeof(int?),
("integer", "int64", true) => typeof(long?),
("integer", null, true) => typeof(int?),
("integer" or "number", "int32", true) => typeof(int?),
("integer" or "number", "int64", true) => typeof(long?),
("integer", null, true) => typeof(long?),
("number", "float", true) => typeof(float?),
("number", "double", true) => typeof(double?),
("number", null, true) => typeof(double?),
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static class Utils
/// <returns>The input value.</returns>
internal static T CheckArgumentNull<T>(
T value,
[CallerArgumentExpression("value")] string parameterName = "")
[CallerArgumentExpression(nameof(value))] string parameterName = "")
{
return value ?? throw new ArgumentNullException(parameterName, $"Value cannot be null: {parameterName}");
}
Expand Down
24 changes: 12 additions & 12 deletions src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static void ValidateDataTypeMismatch(
return;
}

if (type == "integer" && format == "int32")
if (type is "integer" or "number" && format is "int32")
{
if (jsonElement.ValueKind is not JsonValueKind.Number)
{
Expand All @@ -149,7 +149,7 @@ public static void ValidateDataTypeMismatch(
return;
}

if (type == "integer" && format == "int64")
if (type is "integer" or "number" && format is "int64")
{
if (jsonElement.ValueKind is not JsonValueKind.Number)
{
Expand All @@ -161,7 +161,7 @@ public static void ValidateDataTypeMismatch(
return;
}

if (type == "integer" && jsonElement.ValueKind is not JsonValueKind.Number)
if (type is "integer")
{
if (jsonElement.ValueKind is not JsonValueKind.Number)
{
Expand All @@ -173,7 +173,7 @@ public static void ValidateDataTypeMismatch(
return;
}

if (type == "number" && format == "float")
if (type is "number" && format is "float")
{
if (jsonElement.ValueKind is not JsonValueKind.Number)
{
Expand All @@ -185,7 +185,7 @@ public static void ValidateDataTypeMismatch(
return;
}

if (type == "number" && format == "double")
if (type is "number" && format is "double")
{
if (jsonElement.ValueKind is not JsonValueKind.Number)
{
Expand All @@ -197,7 +197,7 @@ public static void ValidateDataTypeMismatch(
return;
}

if (type == "number")
if (type is "number")
{
if (jsonElement.ValueKind is not JsonValueKind.Number)
{
Expand All @@ -209,7 +209,7 @@ public static void ValidateDataTypeMismatch(
return;
}

if (type == "string" && format == "byte")
if (type is "string" && format is "byte")
{
if (jsonElement.ValueKind is not JsonValueKind.String)
{
Expand All @@ -221,7 +221,7 @@ public static void ValidateDataTypeMismatch(
return;
}

if (type == "string" && format == "date")
if (type is "string" && format is "date")
{
if (jsonElement.ValueKind is not JsonValueKind.String)
{
Expand All @@ -233,7 +233,7 @@ public static void ValidateDataTypeMismatch(
return;
}

if (type == "string" && format == "date-time")
if (type is "string" && format is "date-time")
{
if (jsonElement.ValueKind is not JsonValueKind.String)
{
Expand All @@ -245,7 +245,7 @@ public static void ValidateDataTypeMismatch(
return;
}

if (type == "string" && format == "password")
if (type is "string" && format is "password")
{
if (jsonElement.ValueKind is not JsonValueKind.String)
{
Expand All @@ -257,7 +257,7 @@ public static void ValidateDataTypeMismatch(
return;
}

if (type == "string")
if (type is "string")
{
if (jsonElement.ValueKind is not JsonValueKind.String)
{
Expand All @@ -269,7 +269,7 @@ public static void ValidateDataTypeMismatch(
return;
}

if (type == "boolean")
if (type is "boolean")
{
if (jsonElement.ValueKind is not JsonValueKind.True && jsonElement.ValueKind is not JsonValueKind.False)
{
Expand Down
25 changes: 16 additions & 9 deletions test/Microsoft.OpenApi.Tests/Extensions/OpenApiTypeMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ public class OpenApiTypeMapperTests
{
public static IEnumerable<object[]> PrimitiveTypeData => new List<object[]>
{
new object[] { typeof(int), new OpenApiSchema { Type = "integer", Format = "int32" } },
new object[] { typeof(int), new OpenApiSchema { Type = "number", Format = "int32" } },
new object[] { typeof(decimal), new OpenApiSchema { Type = "number", Format = "double" } },
new object[] { typeof(decimal?), new OpenApiSchema { Type = "number", Format = "double", Nullable = true } },
new object[] { typeof(bool?), new OpenApiSchema { Type = "boolean", Nullable = true } },
new object[] { typeof(Guid), new OpenApiSchema { Type = "string", Format = "uuid" } },
new object[] { typeof(uint), new OpenApiSchema { Type = "integer", Format = "int32" } },
new object[] { typeof(long), new OpenApiSchema { Type = "integer", Format = "int64" } },
new object[] { typeof(ulong), new OpenApiSchema { Type = "integer", Format = "int64" } },
new object[] { typeof(Guid?), new OpenApiSchema { Type = "string", Format = "uuid", Nullable = true } },
new object[] { typeof(uint), new OpenApiSchema { Type = "number", Format = "int32" } },
new object[] { typeof(long), new OpenApiSchema { Type = "number", Format = "int64" } },
new object[] { typeof(long?), new OpenApiSchema { Type = "number", Format = "int64", Nullable = true } },
new object[] { typeof(ulong), new OpenApiSchema { Type = "number", Format = "int64" } },
new object[] { typeof(string), new OpenApiSchema { Type = "string" } },
new object[] { typeof(double), new OpenApiSchema { Type = "number", Format = "double" } },
new object[] { typeof(float?), new OpenApiSchema { Type = "number", Format = "float", Nullable = true } },
new object[] { typeof(byte?), new OpenApiSchema { Type = "string", Format = "byte", Nullable = true } },
new object[] { typeof(int?), new OpenApiSchema { Type = "integer", Format = "int32", Nullable = true } },
new object[] { typeof(uint?), new OpenApiSchema { Type = "integer", Format = "int32", Nullable = true } },
new object[] { typeof(int?), new OpenApiSchema { Type = "number", Format = "int32", Nullable = true } },
new object[] { typeof(uint?), new OpenApiSchema { Type = "number", Format = "int32", Nullable = true } },
new object[] { typeof(DateTimeOffset?), new OpenApiSchema { Type = "string", Format = "date-time", Nullable = true } },
new object[] { typeof(double?), new OpenApiSchema { Type = "number", Format = "double", Nullable = true } },
new object[] { typeof(char?), new OpenApiSchema { Type = "string", Nullable = true } },
Expand All @@ -35,11 +38,15 @@ public class OpenApiTypeMapperTests

public static IEnumerable<object[]> OpenApiDataTypes => new List<object[]>
{
new object[] { new OpenApiSchema { Type = "integer", Format = "int32"}, typeof(int) },
new object[] { new OpenApiSchema { Type = "number", Format = "int32", Nullable = false}, typeof(int) },
new object[] { new OpenApiSchema { Type = "number", Format = "int32", Nullable = true}, typeof(int?) },
new object[] { new OpenApiSchema { Type = "number", Format = "int64", Nullable = false}, typeof(long) },
new object[] { new OpenApiSchema { Type = "number", Format = "int64", Nullable = true}, typeof(long?) },
new object[] { new OpenApiSchema { Type = "number", Format = "decimal"}, typeof(decimal) },
new object[] { new OpenApiSchema { Type = "integer", Format = null, Nullable = false}, typeof(long) },
new object[] { new OpenApiSchema { Type = "integer", Format = null, Nullable = true}, typeof(long?) },
new object[] { new OpenApiSchema { Type = "number", Format = null, Nullable = false}, typeof(double) },
new object[] { new OpenApiSchema { Type = "integer", Format = null, Nullable = false}, typeof(int) },
new object[] { new OpenApiSchema { Type = "integer", Format = null, Nullable = true}, typeof(int?) },
new object[] { new OpenApiSchema { Type = "number", Format = null, Nullable = true}, typeof(double?) },
new object[] { new OpenApiSchema { Type = "number", Format = "decimal", Nullable = true}, typeof(decimal?) },
new object[] { new OpenApiSchema { Type = "number", Format = "double", Nullable = true}, typeof(double?) },
new object[] { new OpenApiSchema { Type = "string", Format = "date-time", Nullable = true}, typeof(DateTimeOffset?) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SharpYaml" Version="2.1.1" />
<PackageReference Include="Verify.Xunit" Version="26.6.0" />
<PackageReference Include="Verify.Xunit" Version="27.0.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
Expand Down

0 comments on commit ab0ecf9

Please sign in to comment.