Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaces instances of anyOf to oneOf #264

Merged
merged 27 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e720c12
Change from anyOf to oneOf
irvinesunday Aug 15, 2022
6077fa4
Update tests
irvinesunday Aug 15, 2022
42fd966
Remove whitespace
irvinesunday Aug 17, 2022
b526ebe
Make components for floating point numbers
irvinesunday Aug 18, 2022
cbd4387
Update integration tests
irvinesunday Aug 18, 2022
8f1bba2
Remove Format values from root schema
irvinesunday Aug 24, 2022
5a86db0
Update integration files
irvinesunday Aug 24, 2022
140ed48
Fix integration files
irvinesunday Aug 24, 2022
54a3289
Fix retrieval of type name
irvinesunday Aug 24, 2022
5e64822
Update tests
irvinesunday Aug 24, 2022
c909313
Update src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGene…
irvinesunday Aug 24, 2022
82e3ad0
Fix PR review suggestion
irvinesunday Aug 25, 2022
fba3701
Revert integration file to original
irvinesunday Aug 25, 2022
864fd9e
Resolve integration file
irvinesunday Aug 25, 2022
5c68646
Fix integration file
irvinesunday Aug 25, 2022
71b6f12
Update integration test files
irvinesunday Aug 30, 2022
e12b30e
Update test
irvinesunday Aug 30, 2022
e161b64
Update schema generator to account for v2 serializations
irvinesunday Aug 30, 2022
f443ec4
Update lib. version and tests and remove format from root schema
irvinesunday Sep 2, 2022
7afe3cd
Merge remote-tracking branch 'origin/master' into fix/is/anyOf-oneOf-…
irvinesunday Sep 2, 2022
f849733
Update integration test files
irvinesunday Sep 5, 2022
4841a21
Remove unnecessary variable assignment
irvinesunday Sep 5, 2022
4c8cbad
Add and use constants
irvinesunday Sep 7, 2022
b5277b8
Update lib version
irvinesunday Sep 8, 2022
8b0a4ea
Update integration test files
irvinesunday Sep 8, 2022
31cc58e
Merge remote-tracking branch 'origin/master' into fix/is/anyOf-oneOf-…
irvinesunday Sep 8, 2022
e4f6489
Remove collectionFormat from integration test files
irvinesunday Sep 8, 2022
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
25 changes: 25 additions & 0 deletions src/Microsoft.OpenApi.OData.Reader/Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ internal static class Constants
/// Name used for reference request PUT body.
/// </summary>
public static string ReferencePutRequestBodyName = "refPutBody";

/// <summary>
/// Name used to reference INF, -INF and NaN
/// </summary>
public static string ReferenceNumericName = "ReferenceNumeric";

/// <summary>
/// The odata type name.
Expand All @@ -149,5 +154,25 @@ internal static class Constants
/// string type
/// </summary>
public static string StringType = "string";

/// <summary>
/// integer type
/// </summary>
public static string IntegerType = "integer";

/// <summary>
/// number type
/// </summary>
public static string NumberType = "number";

/// <summary>
/// int64 format
/// </summary>
public static string Int64Format = "int64";

/// <summary>
/// decimal format
/// </summary>
public static string DecimalFormat = "decimal";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,132 +147,130 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
OneOf = null,
AnyOf = null
};

switch (primitiveType.PrimitiveKind)
{
case EdmPrimitiveTypeKind.Binary: // binary
schema.Type = "string";
schema.Type = Constants.StringType;
schema.Format = "base64url";
break;
case EdmPrimitiveTypeKind.Boolean: // boolean
schema.Type = "boolean";
schema.Default = new OpenApiBoolean(false);
break;
case EdmPrimitiveTypeKind.Byte: // byte
schema.Type = "integer";
schema.Type = Constants.IntegerType;
schema.Format = "uint8";
break;
case EdmPrimitiveTypeKind.DateTimeOffset: // datetime offset
schema.Type = "string";
schema.Type = Constants.StringType;
schema.Format = "date-time";
schema.Pattern = "^[0-9]{4,}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]([.][0-9]{1,12})?(Z|[+-][0-9][0-9]:[0-9][0-9])$";
break;
case EdmPrimitiveTypeKind.Decimal: // decimal
if (context.Settings.IEEE754Compatible)
{
schema.AnyOf = new List<OpenApiSchema>
schema.OneOf = new List<OpenApiSchema>
{
new OpenApiSchema { Type = "number" },
new OpenApiSchema { Type = "string" },
new OpenApiSchema { Type = Constants.NumberType, Format = Constants.DecimalFormat },
new OpenApiSchema { Type = Constants.StringType },
};
}
else
{
schema.Type = "number";
schema.Type = Constants.NumberType;
schema.Format = Constants.DecimalFormat;
}
schema.Format = "decimal";
break;
case EdmPrimitiveTypeKind.Double: // double
schema.AnyOf = new List<OpenApiSchema>
schema.OneOf = new List<OpenApiSchema>
{
new OpenApiSchema { Type = "number" },
new OpenApiSchema { Type = "string" },
new OpenApiSchema { Type = Constants.NumberType, Format = "double" },
new OpenApiSchema { Type = Constants.StringType },
new OpenApiSchema
{
Enum = new List<IOpenApiAny>
UnresolvedReference = true,
Reference = new OpenApiReference
{
new OpenApiString("-INF"),
new OpenApiString("INF"),
new OpenApiString("NaN")
Type = ReferenceType.Schema,
Id = Constants.ReferenceNumericName
}
}
};
schema.Format = "double";
break;
case EdmPrimitiveTypeKind.Single: // single
schema.AnyOf = new List<OpenApiSchema>
schema.OneOf = new List<OpenApiSchema>
{
new OpenApiSchema { Type = "number" },
new OpenApiSchema { Type = "string" },
new OpenApiSchema { Type = Constants.NumberType, Format = "float" },
new OpenApiSchema { Type = Constants.StringType },
new OpenApiSchema
{
Enum = new List<IOpenApiAny>
UnresolvedReference = true,
Reference = new OpenApiReference
{
new OpenApiString("-INF"),
new OpenApiString("INF"),
new OpenApiString("NaN")
Type = ReferenceType.Schema,
Id = Constants.ReferenceNumericName
}
}
};
schema.Format = "float";
break;
case EdmPrimitiveTypeKind.Guid: // guid
schema.Type = "string";
schema.Type = Constants.StringType;
schema.Format = "uuid";
schema.Pattern = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$";
break;
case EdmPrimitiveTypeKind.Int16:
schema.Type = "integer";
schema.Type = Constants.IntegerType;
schema.Format = "int16";
schema.Minimum = Int16.MinValue; // -32768
schema.Maximum = Int16.MaxValue; // 32767
break;
case EdmPrimitiveTypeKind.Int32:
schema.Type = "integer";
schema.Type = Constants.IntegerType;
schema.Format = "int32";
schema.Minimum = Int32.MinValue; // -2147483648
schema.Maximum = Int32.MaxValue; // 2147483647
break;
case EdmPrimitiveTypeKind.Int64:
if (context.Settings.IEEE754Compatible)
{
schema.AnyOf = new List<OpenApiSchema>
schema.OneOf = new List<OpenApiSchema>
{
new OpenApiSchema { Type = "integer" },
new OpenApiSchema { Type = "string" }
new OpenApiSchema { Type = Constants.IntegerType, Format = Constants.Int64Format },
new OpenApiSchema { Type = Constants.StringType }
};
}
else
{
schema.Type = "integer";
schema.Type = Constants.IntegerType;
schema.Format = Constants.Int64Format;
}

schema.Format = "int64";
break;
case EdmPrimitiveTypeKind.SByte:
schema.Type = "integer";
schema.Type = Constants.IntegerType;
schema.Format = "int8";
schema.Minimum = SByte.MinValue; // -128
schema.Maximum = SByte.MaxValue; // 127
break;
case EdmPrimitiveTypeKind.String: // string
schema.Type = "string";
schema.Type = Constants.StringType;
break;
case EdmPrimitiveTypeKind.Stream: // stream
schema.Type = "string";
schema.Type = Constants.StringType;
schema.Format = "base64url";
break;
case EdmPrimitiveTypeKind.Duration: // duration
schema.Type = "string";
schema.Type = Constants.StringType;
schema.Format = "duration";
schema.Pattern = "^-?P([0-9]+D)?(T([0-9]+H)?([0-9]+M)?([0-9]+([.][0-9]+)?S)?)?$";
break;
case EdmPrimitiveTypeKind.Date:
schema.Type = "string";
schema.Type = Constants.StringType;
schema.Format = "date";
schema.Pattern = "^[0-9]{4,}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$";
break;
case EdmPrimitiveTypeKind.TimeOfDay:
schema.Type = "string";
schema.Type = Constants.StringType;
schema.Format = "time";
schema.Pattern = "^([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]([.][0-9]{1,12})?$";
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ public static IDictionary<string, OpenApiSchema> CreateSchemas(this ODataContext
AdditionalProperties = new OpenApiSchema { Type = Constants.ObjectType }
};

schemas[Constants.ReferenceNumericName] = new()
{
Enum = new List<IOpenApiAny>
{
new OpenApiString("-INF"),
new OpenApiString("INF"),
new OpenApiString("NaN")
}
};

// @odata.nextLink + @odata.count
if (context.Settings.EnablePagination || context.Settings.EnableCount)
{
Expand Down Expand Up @@ -611,7 +621,11 @@ private static IOpenApiAny GetTypeNameForExample(ODataContext context, IEdmTypeR
}
else
{
return new OpenApiString(schema.Type ?? schema.Format);
return new OpenApiString(schema.Type ??
(schema.AnyOf ?? Enumerable.Empty<OpenApiSchema>())
baywet marked this conversation as resolved.
Show resolved Hide resolved
.Union(schema.AllOf ?? Enumerable.Empty<OpenApiSchema>())
.Union(schema.OneOf ?? Enumerable.Empty<OpenApiSchema>())
.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ?? schema.Format);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public static OpenApiSchema CreateEdmGeometrySchema()
return new OpenApiSchema
{
Type = "object",
AnyOf = new List<OpenApiSchema>
OneOf = new List<OpenApiSchema>
{
new OpenApiSchema { UnresolvedReference = true, Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = "Edm.GeometryPoint" } },
new OpenApiSchema { UnresolvedReference = true, Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = "Edm.GeometryLineString" } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.OData.Edm" Version="7.10.0" />
<PackageReference Include="Microsoft.OpenApi" Version="1.2.3" />
<PackageReference Include="Microsoft.OpenApi" Version="1.4.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
4 changes: 2 additions & 2 deletions src/OoasGui/OoasGui.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<OutputType>WinExe</OutputType>
Expand All @@ -17,7 +17,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.12.2" />
<PackageReference Include="Microsoft.OpenApi" Version="1.4.0" />
<PackageReference Include="Microsoft.OpenApi" Version="1.4.1" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ------------------------------------------------------------
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
Expand Down Expand Up @@ -407,14 +407,14 @@ public void CreateEdmTypeSchemaReturnSchemaForDecimal(bool isNullable, bool IEEE
if (IEEE754Compatible)
{
Assert.Null(schema.Type);
Assert.NotNull(schema.AnyOf);
Assert.Equal(2, schema.AnyOf.Count);
Assert.Equal(new[] { "number", "string" }, schema.AnyOf.Select(a => a.Type));
Assert.NotNull(schema.OneOf);
Assert.Equal(2, schema.OneOf.Count);
Assert.Equal(new[] { "number", "string" }, schema.OneOf.Select(a => a.Type));
}
else
{
Assert.Equal("number", schema.Type);
Assert.Null(schema.AnyOf);
Assert.Null(schema.OneOf);
}

Assert.Equal(isNullable, schema.Nullable);
Expand Down Expand Up @@ -445,9 +445,9 @@ public void CreateEdmTypeSchemaReturnSchemaForInt64(bool isNullable, bool IEEE75
if (IEEE754Compatible)
{
Assert.Null(schema.Type);
Assert.NotNull(schema.AnyOf);
Assert.Equal(2, schema.AnyOf.Count);
Assert.Equal(new[] { "integer", "string" }, schema.AnyOf.Select(a => a.Type));
Assert.NotNull(schema.OneOf);
Assert.Equal(2, schema.OneOf.Count);
Assert.Equal(new[] { "integer", "string" }, schema.OneOf.Select(a => a.Type));
}
else
{
Expand Down Expand Up @@ -510,13 +510,13 @@ public void CreateEdmTypeSchemaReturnSchemaForDouble(bool isNullable)
// & Assert
Assert.Null(schema.Type);

Assert.Equal("double", schema.Format);
Assert.Equal("double", schema.OneOf.FirstOrDefault(x => !string.IsNullOrEmpty(x.Format))?.Format);
Assert.Equal(isNullable, schema.Nullable);

Assert.Null(schema.OneOf);
Assert.Null(schema.AnyOf);

Assert.NotNull(schema.AnyOf);
Assert.Equal(3, schema.AnyOf.Count);
Assert.NotNull(schema.OneOf);
Assert.Equal(3, schema.OneOf.Count);
}

[Theory]
Expand All @@ -536,13 +536,13 @@ public void CreateEdmTypeSchemaReturnSchemaForSingle(bool isNullable)
// & Assert
Assert.Null(schema.Type);

Assert.Equal("float", schema.Format);
Assert.Equal("float", schema.OneOf.FirstOrDefault(x => !string.IsNullOrEmpty(x.Format))?.Format);
Assert.Equal(isNullable, schema.Nullable);

Assert.Null(schema.OneOf);
Assert.Null(schema.AnyOf);

Assert.NotNull(schema.AnyOf);
Assert.Equal(3, schema.AnyOf.Count);
Assert.NotNull(schema.OneOf);
Assert.Equal(3, schema.OneOf.Count);
}
#endregion
}
Expand Down
Loading