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
10 changes: 5 additions & 5 deletions src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
/// <param name="model">The Edm model.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>The collection of built <see cref="ODataPath"/>.</returns>
public virtual IEnumerable<ODataPath> GetPaths(IEdmModel model, OpenApiConvertSettings settings)

Check warning on line 51 in src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
if (model == null || model.EntityContainer == null)
{
Expand All @@ -60,7 +60,7 @@
if (_model is not null)
{
// entity set
foreach (var entitySet in _model.EntityContainer.EntitySets())

Check warning on line 63 in src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs

View workflow job for this annotation

GitHub Actions / Build

Loops should be simplified using the "Where" LINQ method (https://rules.sonarsource.com/csharp/RSPEC-3267)
{
if (CanFilter(entitySet))
{
Expand All @@ -69,7 +69,7 @@
}

// singleton
foreach (var singleton in _model.EntityContainer.Singletons())

Check warning on line 72 in src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs

View workflow job for this annotation

GitHub Actions / Build

Loops should be simplified using the "Where" LINQ method (https://rules.sonarsource.com/csharp/RSPEC-3267)
{
if (CanFilter(singleton))
{
Expand All @@ -84,7 +84,7 @@
if (_model is not null)
{
// unbound operations
foreach (IEdmOperationImport import in _model.EntityContainer.OperationImports())

Check warning on line 87 in src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs

View workflow job for this annotation

GitHub Actions / Build

Loops should be simplified using the "Where" LINQ method (https://rules.sonarsource.com/csharp/RSPEC-3267)
{
if (CanFilter(import))
{
Expand Down Expand Up @@ -132,7 +132,7 @@
return allODataPaths;
}

private void AppendPath(ODataPath path)

Check warning on line 135 in src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 38 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
Utils.CheckArgumentNull(path, nameof(path));

Expand Down Expand Up @@ -290,7 +290,7 @@
RetrieveComplexPropertyPaths(entityType, path, convertSettings);

// navigation property
foreach (IEdmNavigationProperty np in entityType.NavigationProperties())

Check warning on line 293 in src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs

View workflow job for this annotation

GitHub Actions / Build

Loops should be simplified using the "Where" LINQ method (https://rules.sonarsource.com/csharp/RSPEC-3267)
{
if (CanFilter(np))
{
Expand Down Expand Up @@ -499,7 +499,7 @@
/// <param name="currentPath">The current OData path.</param>
/// <param name="convertSettings">The settings for the current conversion.</param>
/// <param name="visitedNavigationProperties">A stack that holds the visited navigation properties in the <paramref name="currentPath"/>.</param>
private void RetrieveNavigationPropertyPaths(

Check warning on line 502 in src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 47 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
IEdmNavigationProperty navigationProperty,
CountRestrictionsType? count,
ODataPath currentPath,
Expand Down Expand Up @@ -639,7 +639,7 @@
if (shouldExpand)
{
// expand to sub navigation properties
foreach (IEdmNavigationProperty subNavProperty in navEntityType.NavigationProperties())

Check warning on line 642 in src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs

View workflow job for this annotation

GitHub Actions / Build

Loops should be simplified using the "Where" LINQ method (https://rules.sonarsource.com/csharp/RSPEC-3267)
{
if (CanFilter(subNavProperty))
{
Expand Down Expand Up @@ -722,7 +722,7 @@
/// <param name="structuredType">The type that is being inherited from to which this method will add downcast path segments.</param>
/// <param name="annotable">The annotable navigation source to read cast annotations from.</param>
/// <param name="targetsMany">Whether the annotable navigation source targets many entities.</param>
private void CreateTypeCastPaths(ODataPath currentPath, OpenApiConvertSettings convertSettings, IEdmStructuredType structuredType, IEdmVocabularyAnnotatable annotable, bool targetsMany)

Check warning on line 725 in src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 33 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
Utils.CheckArgumentNull(currentPath, nameof(currentPath));
Utils.CheckArgumentNull(convertSettings, nameof(convertSettings));
Expand Down Expand Up @@ -750,7 +750,7 @@
.OfType<IEdmStructuredType>()
.ToArray();

if (targetTypes is not { Length: > 0} || _model is null) return;

Check warning on line 753 in src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'False'. (https://rules.sonarsource.com/csharp/RSPEC-2589)

foreach (var targetType in targetTypes)
{
Expand Down Expand Up @@ -872,16 +872,16 @@
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;
}
Expand Down Expand Up @@ -1194,7 +1194,7 @@
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))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -150,19 +150,19 @@ 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()
{
Type = JsonSchemaType.Object,
Properties = new Dictionary<string, IOpenApiSchema>
{
{
"value", context.CreateEdmTypeSchema(operation.ReturnType, document)
"value", context.CreateEdmTypeSchema(returnType, document)
}
}
};
Expand Down Expand Up @@ -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.
Expand All @@ -217,18 +217,18 @@ public static OpenApiResponses CreateResponses(this ODataContext context, IEdmOp
Properties = new Dictionary<string, IOpenApiSchema>
{
{
"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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -347,7 +347,7 @@ protected override void SetExternalDocs(OpenApiOperation operation)
// <inheritdoc/>
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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
Expand All @@ -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")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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)
Expand Down
Loading