Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,21 @@
}
else
{
if (type == "integer" && format == "int32")
if (type is "integer" or "number" && format == "int32")
{
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
{
return new OpenApiInteger(intValue);
}
}

Check notice

Code scanning / CodeQL

Nested 'if' statements can be combined Note

These 'if' statements can be combined.

if (type == "integer" && format == "int64")
if (type is "integer" or "number" && format == "int64")
{
if (long.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var longValue))
{
return new OpenApiLong(longValue);
}
}

Check notice

Code scanning / CodeQL

Nested 'if' statements can be combined Note

These 'if' statements can be combined.

if (type == "integer")
{
Expand Down
25 changes: 13 additions & 12 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,8 +98,9 @@ public static Type MapOpenApiPrimitiveTypeToSimpleType(this OpenApiSchema schema
var type = (schema.Type?.ToLowerInvariant(), schema.Format?.ToLowerInvariant(), schema.Nullable) switch
{
("boolean", null, false) => typeof(bool),
("integer", "int32", false) => typeof(int),
("integer", "int64", false) => typeof(long),
// 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(int),
("number", "float", false) => typeof(float),
("number", "double", false) => typeof(double),
Expand All @@ -113,8 +114,8 @@ 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" or "number", "int32", true) => typeof(int?),
("integer" or "number", "int64", true) => typeof(long?),
("integer", null, true) => typeof(int?),
("number", "float", true) => typeof(float?),
("number", "double", true) => typeof(double?),
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static void ValidateDataTypeMismatch(
return;
}

if (type == "integer" && format == "int32")
if (type is "integer" or "number" && format == "int32")
{
if (value is not OpenApiInteger)
{
Expand All @@ -146,7 +146,7 @@ public static void ValidateDataTypeMismatch(
return;
}

if (type == "integer" && format == "int64")
if (type is "integer" or "number" && format == "int64")
{
if (value is not OpenApiLong)
{
Expand All @@ -158,7 +158,7 @@ public static void ValidateDataTypeMismatch(
return;
}

if (type == "integer" && value is not OpenApiInteger)
if (type is "integer" && value is not OpenApiInteger)
{
if (value is not OpenApiInteger)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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(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 } },
Expand All @@ -24,6 +24,7 @@ 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"}, typeof(int) },
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 = "string" }, typeof(string) },
Expand Down
Loading