Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 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
5 changes: 5 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
OneOf = null,
AnyOf = null
};

string format;
switch (primitiveType.PrimitiveKind)
{
case EdmPrimitiveTypeKind.Binary: // binary
Expand All @@ -167,53 +169,54 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
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
format = "decimal";
if (context.Settings.IEEE754Compatible)
{
schema.AnyOf = new List<OpenApiSchema>
schema.OneOf = new List<OpenApiSchema>
{
new OpenApiSchema { Type = "number" },
new OpenApiSchema { Type = "number", Format = format },
new OpenApiSchema { Type = "string" },
};
}
else
{
schema.Type = "number";
schema.Format = format;
}
schema.Format = "decimal";
break;
case EdmPrimitiveTypeKind.Double: // double
schema.AnyOf = new List<OpenApiSchema>
format = "double";
schema.OneOf = new List<OpenApiSchema>
{
new OpenApiSchema { Type = "number" },
new OpenApiSchema { Type = "number", Format = format },
new OpenApiSchema { Type = "string" },
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>
format = "float";
schema.OneOf = new List<OpenApiSchema>
{
new OpenApiSchema { Type = "number" },
new OpenApiSchema { Type = "number", Format = format },
new OpenApiSchema { Type = "string" },
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";
Expand All @@ -233,20 +236,20 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
schema.Maximum = Int32.MaxValue; // 2147483647
break;
case EdmPrimitiveTypeKind.Int64:
format = "int64";
if (context.Settings.IEEE754Compatible)
{
schema.AnyOf = new List<OpenApiSchema>
schema.OneOf = new List<OpenApiSchema>
{
new OpenApiSchema { Type = "integer" },
new OpenApiSchema { Type = "integer", Format = format },
new OpenApiSchema { Type = "string" }
};
}
else
{
schema.Type = "integer";
schema.Format = format;
}

schema.Format = "int64";
break;
case EdmPrimitiveTypeKind.SByte:
schema.Type = "integer";
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>())
.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.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
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
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 @@ -428,9 +428,9 @@ public void CreateComplexTypeWithBaseSchemaReturnCorrectSchema()
Assert.Equal(1, declaredSchema.Properties.Count);
var property = Assert.Single(declaredSchema.Properties);
Assert.Equal("Price", property.Key);
Assert.Equal("decimal", property.Value.Format);
Assert.NotNull(property.Value.AnyOf);
Assert.Equal(new string[] { "number", "string" }, property.Value.AnyOf.Select(e => e.Type));
Assert.Equal("decimal", property.Value.OneOf.FirstOrDefault(x => !string.IsNullOrEmpty(x.Format))?.Format);
Assert.NotNull(property.Value.OneOf);
Assert.Equal(new string[] { "number", "string" }, property.Value.OneOf.Select(e => e.Type));

Assert.Equal("Complex type 'Tree' description.", declaredSchema.Description);
Assert.Equal("Tree", declaredSchema.Title);
Expand All @@ -451,15 +451,15 @@ public void CreateComplexTypeWithBaseSchemaReturnCorrectSchema()
""properties"": {
""Price"": {
""multipleOf"": 1,
""anyOf"": [
""oneOf"": [
{
""type"": ""number""
""type"": ""number"",
""format"": ""decimal""
},
{
""type"": ""string""
}
],
""format"": ""decimal""
]
}
},
""description"": ""Complex type 'Tree' description.""
Expand Down Expand Up @@ -936,22 +936,18 @@ public void NonNullableDoublePropertyWithDefaultStringWorks()
string json = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);

Assert.Equal(@"{
""anyOf"": [
""oneOf"": [
{
""type"": ""number""
""type"": ""number"",
""format"": ""double""
},
{
""type"": ""string""
},
{
""enum"": [
""-INF"",
""INF"",
""NaN""
]
""$ref"": ""#/components/schemas/ReferenceNumeric""
}
],
""format"": ""double"",
""default"": ""3.1415926535897931""
}".ChangeLineBreaks(), json);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void CreateEdmGeometrySchemaSerializeAsJsonWorks()
// Assert
Assert.Equal(@"{
""type"": ""object"",
""anyOf"": [
""oneOf"": [
{
""$ref"": ""#/components/schemas/Edm.GeometryPoint""
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Microsoft.OpenApi" Version="1.2.3" />
<PackageReference Include="Microsoft.OpenApi" Version="1.4.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,13 @@
"additionalProperties": {
"type": "object"
}
},
"ReferenceNumeric": {
"enum": [
"-INF",
"INF",
"NaN"
]
}
},
"parameters": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,11 @@ definitions:
type: string
additionalProperties:
type: object
ReferenceNumeric:
enum:
- '-INF'
- INF
- NaN
parameters:
top:
in: query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,13 @@
"additionalProperties": {
"type": "object"
}
},
"ReferenceNumeric": {
"enum": [
"-INF",
"INF",
"NaN"
]
}
},
"responses": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,11 @@ components:
type: string
additionalProperties:
type: object
ReferenceNumeric:
enum:
- '-INF'
- INF
- NaN
responses:
error:
description: error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@
"additionalProperties": {
"type": "object"
}
},
"ReferenceNumeric": {
"enum": [
"-INF",
"INF",
"NaN"
]
}
},
"parameters": {
Expand Down
Loading