diff --git a/src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs b/src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs index c2d5c79d..e2bb4d78 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs @@ -872,16 +872,16 @@ private void RetrieveBoundOperationPaths(OpenApiConvertSettings convertSettings) var functionPaths = _allOperationPaths.Where(x => x.LastSegment is ODataOperationSegment operationSegment && operationSegment.Operation is IEdmFunction edmFunction && edmFunction.IsComposable - && edmFunction.ReturnType != null - && edmFunction.ReturnType.Definition is IEdmEntityType); + && edmFunction.GetReturn()?.Type is { } retType + && retType.Definition is IEdmEntityType); foreach( var functionPath in functionPaths) { if (functionPath.LastSegment is not ODataOperationSegment operationSegment || operationSegment.Operation is not IEdmFunction edmFunction || !edmFunction.IsComposable - || edmFunction.ReturnType == null - || edmFunction.ReturnType.Definition is not IEdmEntityType returnBindingEntityType) + || edmFunction.GetReturn()?.Type is not { } retType + || retType.Definition is not IEdmEntityType returnBindingEntityType) { continue; } @@ -1194,7 +1194,7 @@ private void AppendBoundOperationOnOperationPath(IEdmOperation edmOperation, boo if (path.LastSegment is not ODataOperationSegment operationSegment || (path.Segments.Count > 1 && path.Segments[path.Segments.Count - 2] is ODataOperationSegment) || operationSegment.Operation is not IEdmFunction edmFunction || !edmFunction.IsComposable - || edmFunction.ReturnType == null || !edmFunction.ReturnType.Definition.Equals(bindingEntityType) + || edmFunction.GetReturn()?.Type is not { } retType || !retType.Definition.Equals(bindingEntityType) || isCollection || _model is not null && !EdmModelHelper.IsOperationAllowed(_model, edmOperation, operationSegment.Operation, true)) { diff --git a/src/Microsoft.OpenApi.OData.Reader/Generator/EdmSpatialTypeVisitor.cs b/src/Microsoft.OpenApi.OData.Reader/Generator/EdmSpatialTypeVisitor.cs index ffcec704..ce2065ea 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Generator/EdmSpatialTypeVisitor.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Generator/EdmSpatialTypeVisitor.cs @@ -49,9 +49,9 @@ public void Visit(IEdmModel model) private void VisitOperation(IEdmOperation operation) { - if (operation.ReturnType != null) + if (operation.GetReturn() is {} operationReturn) { - this.VisitTypeReference(operation.ReturnType); + this.VisitTypeReference(operationReturn.Type); } foreach (var parameter in operation.Parameters) diff --git a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiResponseGenerator.cs b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiResponseGenerator.cs index 0d59595c..1bec08b1 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiResponseGenerator.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiResponseGenerator.cs @@ -117,7 +117,7 @@ public static OpenApiResponses CreateResponses(this ODataContext context, IEdmOp OpenApiResponses responses = new(); - if (operation.IsAction() && operation.ReturnType == null && Constants.StatusCode204.GetResponse(document) is {} x204Response) + if (operation.IsAction() && operation.GetReturn()?.Type == null && Constants.StatusCode204.GetResponse(document) is {} x204Response) { responses.Add(Constants.StatusCode204, x204Response); } @@ -150,11 +150,11 @@ public static OpenApiResponses CreateResponses(this ODataContext context, IEdmOp public static OpenApiResponse? CreateOperationResponse(this ODataContext context, IEdmOperation operation, OpenApiDocument document) { - if (operation.ReturnType == null) + if (operation.GetReturn()?.Type is not {} returnType) return null; IOpenApiSchema schema; - if (operation.ReturnType.IsCollection()) + if (returnType.IsCollection()) { OpenApiSchema baseSchema = new() { @@ -162,7 +162,7 @@ public static OpenApiResponses CreateResponses(this ODataContext context, IEdmOp Properties = new Dictionary { { - "value", context.CreateEdmTypeSchema(operation.ReturnType, document) + "value", context.CreateEdmTypeSchema(returnType, document) } } }; @@ -202,12 +202,12 @@ public static OpenApiResponses CreateResponses(this ODataContext context, IEdmOp if (schema is OpenApiSchema openApiSchema) { - openApiSchema.Title = operation.ReturnType.Definition.AsElementType() is not IEdmEntityType entityType + openApiSchema.Title = returnType.Definition.AsElementType() is not IEdmEntityType entityType ? null : $"Collection of {entityType.Name}"; openApiSchema.Type = JsonSchemaType.Object; } } - else if (operation.ReturnType.IsPrimitive()) + else if (returnType.IsPrimitive()) { // A property or operation response that is of a primitive type is represented as an object with a single name/value pair, // whose name is value and whose value is a primitive value. @@ -217,18 +217,18 @@ public static OpenApiResponses CreateResponses(this ODataContext context, IEdmOp Properties = new Dictionary { { - "value", context.CreateEdmTypeSchema(operation.ReturnType, document) + "value", context.CreateEdmTypeSchema(returnType, document) } } }; } else { - schema = context.CreateEdmTypeSchema(operation.ReturnType, document); + schema = context.CreateEdmTypeSchema(returnType, document); } string? mediaType = Constants.ApplicationJsonMediaType; - if (operation.ReturnType.AsPrimitive()?.PrimitiveKind() == EdmPrimitiveTypeKind.Stream) + if (returnType.AsPrimitive()?.PrimitiveKind() == EdmPrimitiveTypeKind.Stream) { mediaType = context.Model.GetString(operation, CoreConstants.MediaType); diff --git a/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs b/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs index 5135d5c1..3725e282 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs @@ -254,7 +254,7 @@ protected override void AppendCustomParameters(OpenApiOperation operation) private void AppendSystemQueryOptions(IEdmFunction function, OpenApiOperation operation) { - if (function.ReturnType.IsCollection() && Context is not null) + if (function.GetReturn()?.Type is {} functionReturnType && functionReturnType.IsCollection() && Context is not null) { operation.Parameters ??= []; // $top @@ -287,7 +287,7 @@ private void AppendSystemQueryOptions(IEdmFunction function, OpenApiOperation op operation.Parameters.AppendParameter(countParameter); } - if (function.ReturnType?.Definition?.AsElementType() is IEdmEntityType entityType) + if (functionReturnType?.Definition?.AsElementType() is IEdmEntityType entityType) { // $select if (Context.CreateSelect(function, entityType) is {} selectParameter) @@ -347,7 +347,7 @@ protected override void SetExternalDocs(OpenApiOperation operation) // protected override void SetExtensions(OpenApiOperation operation) { - if (Context is { Settings.EnablePagination: true } && EdmOperation?.ReturnType?.TypeKind() == EdmTypeKind.Collection) + if (Context is { Settings.EnablePagination: true } && EdmOperation?.GetReturn()?.Type?.TypeKind() == EdmTypeKind.Collection) { var extension = new JsonObject { diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmActionOperationHandlerTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmActionOperationHandlerTests.cs index 88b6f29b..569b6225 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmActionOperationHandlerTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmActionOperationHandlerTests.cs @@ -342,7 +342,7 @@ public void CreateOperationForEdmActionWithCollectionReturnTypeContainsXMsPageab var operation = _operationHandler.CreateOperation(context, path); // Assert - if (enablePagination && action.ReturnType.IsCollection()) + if (enablePagination && action.GetReturn()?.Type?.IsCollection() == true) { Assert.True(operation.Extensions.ContainsKey(Common.Constants.xMsPageable)); } diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmFunctionOperationHandlerTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmFunctionOperationHandlerTests.cs index e8732853..636108ff 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmFunctionOperationHandlerTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmFunctionOperationHandlerTests.cs @@ -546,7 +546,7 @@ public void CreateOperationForEdmFunctionWithCollectionReturnTypeContainsXMsPage var operation = _operationHandler.CreateOperation(context, path); // Assert - if (enablePagination && function.ReturnType.IsCollection()) + if (enablePagination && function.GetReturn()?.Type?.IsCollection() == true) { Assert.True(operation.Extensions.ContainsKey(Common.Constants.xMsPageable)); } diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/CustomParameterTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/CustomParameterTests.cs index b37f6941..4e0b12e8 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/CustomParameterTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/CustomParameterTests.cs @@ -3,6 +3,7 @@ // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. // ------------------------------------------------------------ +using System; using System.IO; using System.Xml; using Microsoft.OData.Edm; @@ -29,7 +30,7 @@ public void InitializeCustomParameterWithRecordSuccess() new EdmPropertyConstructor("ExampleValues", new EdmCollectionExpression( new EdmRecordExpression(new EdmPropertyConstructor("Value", new EdmStringConstant("html"))), - new EdmRecordExpression(new EdmPropertyConstructor("Value", new EdmTimeOfDayConstant(new TimeOfDay(3, 4, 5, 6))))))); + new EdmRecordExpression(new EdmPropertyConstructor("Value", new EdmTimeOfDayConstant(new TimeOnly(3, 4, 5, 6))))))); // Act CustomParameter parameter = new CustomParameter(); @@ -102,7 +103,7 @@ private static void VerifyCustomParameter(CustomParameter parameter) value = parameter.ExampleValues[1]; Assert.Null(value.Description); Assert.NotNull(value.Value); - Assert.Equal(new TimeOfDay(3, 4, 5, 6), value.Value.Value); + Assert.Equal(new TimeOnly(3, 4, 5, 6).ToString("HH:mm:ss.fffffff"), value.Value.Value.ToString()); } private IEdmModel GetEdmModel(string annotation) diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/RevisionRecordTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/RevisionRecordTests.cs index 3b155d23..c8d550c4 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/RevisionRecordTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/RevisionRecordTests.cs @@ -43,8 +43,8 @@ public void InitializeWithDeprecatedRevisionsTypeRecordSuccess() { // Arrange IEdmRecordExpression record = new EdmRecordExpression( - new EdmPropertyConstructor("Date", new EdmDateConstant(new Date(2021, 8, 24))), - new EdmPropertyConstructor("RemovalDate", new EdmDateConstant(new Date(2021, 10, 24))), + new EdmPropertyConstructor("Date", new EdmDateConstant(new DateOnly(2021, 8, 24))), + new EdmPropertyConstructor("RemovalDate", new EdmDateConstant(new DateOnly(2021, 10, 24))), new EdmPropertyConstructor("Kind", new EdmEnumMemberExpression(deprecatedValue)), new EdmPropertyConstructor("Description", new EdmStringConstant("The identityProvider API is deprecated and will stop returning data on March 2023. Please use the new identityProviderBase API.")), new EdmPropertyConstructor("Version", new EdmStringConstant("2021-05/test"))); @@ -71,15 +71,15 @@ public void WorksForAllKinds() { // Arrange IEdmRecordExpression record1 = new EdmRecordExpression( - new EdmPropertyConstructor("Date", new EdmDateConstant(new Date(2021, 8, 24))), - new EdmPropertyConstructor("RemovalDate", new EdmDateConstant(new Date(2021, 10, 24))), + new EdmPropertyConstructor("Date", new EdmDateConstant(new DateOnly(2021, 8, 24))), + new EdmPropertyConstructor("RemovalDate", new EdmDateConstant(new DateOnly(2021, 10, 24))), new EdmPropertyConstructor("Kind", new EdmEnumMemberExpression(addedValue)), new EdmPropertyConstructor("Description", new EdmStringConstant("The identityProvider API is deprecated and will stop returning data on March 2023. Please use the new identityProviderBase API.")), new EdmPropertyConstructor("Version", new EdmStringConstant("2021-05/test"))); IEdmRecordExpression record2 = new EdmRecordExpression( - new EdmPropertyConstructor("Date", new EdmDateConstant(new Date(2023, 3, 2))), - new EdmPropertyConstructor("RemovalDate", new EdmDateConstant(new Date(2023, 05, 30))), + new EdmPropertyConstructor("Date", new EdmDateConstant(new DateOnly(2023, 3, 2))), + new EdmPropertyConstructor("RemovalDate", new EdmDateConstant(new DateOnly(2023, 05, 30))), new EdmPropertyConstructor("Kind", new EdmEnumMemberExpression(deprecatedValue)), new EdmPropertyConstructor("Description", new EdmStringConstant("Private preview test.")), new EdmPropertyConstructor("Version", new EdmStringConstant("2023-03/test"))); diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Core/PrimitiveExampleValueTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Core/PrimitiveExampleValueTests.cs index 12b98e8f..00f92cc3 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Core/PrimitiveExampleValueTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Core/PrimitiveExampleValueTests.cs @@ -69,8 +69,8 @@ public void InitializeWithPrimitiveValueRecordSuccess() new object[] { @"Int=""42""", (long)42 }, new object[] { @"Bool=""true""", true }, new object[] { @"Bool=""false""", false }, - new object[] { @"TimeOfDay=""15:38:25.1090000""", new TimeOfDay(15, 38, 25, 109) }, - new object[] { @"Date=""2014-10-13""", new Date(2014, 10, 13) }, + new object[] { @"TimeOfDay=""15:38:25.1090000""", new TimeOnly(15, 38, 25, 109) }, + new object[] { @"Date=""2014-10-13""", new DateOnly(2014, 10, 13) }, new object[] { @"Duration=""PT0S""", new TimeSpan() }, // new object[] { @"Binary=""AQ==""", new byte[] { 1 }, }, has problem in ODL? new object[] { @"Float=""3.14""", 3.14 }, @@ -106,7 +106,18 @@ public void PrimitiveExamplevalueInitializeWorksForPrimitiveData(string data, ob Assert.NotNull(value); Assert.Equal("Primitive example value", value.Description); Assert.NotNull(value.Value); - Assert.Equal(except, value.Value.Value); + switch (except) + { + case DateOnly dateOnly: + Assert.Equal(dateOnly.ToString("yyyy-MM-dd"), value.Value.Value.ToString()); + break; + case TimeOnly timeOnly: + Assert.Equal(timeOnly.ToString("HH:mm:ss.fffffff"), value.Value.Value.ToString()); + break; + default: + Assert.Equal(except, value.Value.Value); + break; + } } private IEdmModel GetEdmModel(string annotation)