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

Alias optional parameters of OData functions #272

Merged
merged 2 commits into from
Sep 7, 2022
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 @@ -5,6 +5,7 @@

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using Microsoft.OData.Edm;
Expand Down Expand Up @@ -145,7 +146,9 @@ private string FunctionName(IEdmFunction function, OpenApiConvertSettings settin
{
string uniqueName = Utils.GetUniqueName(p.Name, parameters);
var quote = p.Type.Definition.ShouldPathParameterBeQuoted(settings) ? "'" : string.Empty;
return p.Name + $"={quote}{{{uniqueName}}}{quote}";
return p is IEdmOptionalParameter
? p.Name + $"={quote}@{uniqueName}{quote}"
: p.Name + $"={quote}{{{uniqueName}}}{quote}";
})));

functionName.Append(")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public static IList<OpenApiParameter> CreateParameters(this ODataContext context
}
}

OpenApiParameter parameter;
OpenApiParameter parameter;
bool isOptionalParameter = edmParameter is IEdmOptionalParameter;
if (edmParameter.Type.IsStructured() ||
edmParameter.Type.IsCollection())
{
Expand Down Expand Up @@ -121,7 +122,6 @@ public static IList<OpenApiParameter> CreateParameters(this ODataContext context
else
{
// Primitive parameters use the same type mapping as described for primitive properties.
bool isOptionalParameter = edmParameter is IEdmOptionalParameter;
parameter = new OpenApiParameter
{
Name = parameterNameMapping == null ? edmParameter.Name : parameterNameMapping[edmParameter.Name],
Expand All @@ -134,7 +134,9 @@ public static IList<OpenApiParameter> CreateParameters(this ODataContext context
if (parameterNameMapping != null)
{
var quote = edmParameter.Type.Definition.ShouldPathParameterBeQuoted(context.Settings) ? "'" : string.Empty;
parameter.Description = $"Usage: {edmParameter.Name}={quote}{{{parameterNameMapping[edmParameter.Name]}}}{quote}";
parameter.Description = isOptionalParameter
? $"Usage: {edmParameter.Name}={quote}@{parameterNameMapping[edmParameter.Name]}{quote}"
: $"Usage: {edmParameter.Name}={quote}{{{parameterNameMapping[edmParameter.Name]}}}{quote}";
}

parameters.Add(parameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// ------------------------------------------------------------

using System;
using System.Runtime.CompilerServices;
using Microsoft.OData.Edm;
using Xunit;

Expand Down Expand Up @@ -74,17 +73,18 @@ public void GetPathItemNameReturnsCorrectActionLiteral(bool unqualifiedCall, boo
}

[Theory]
[InlineData(true, true, "MyFunction(param={param})")]
[InlineData(true, false, "MyFunction(entity={entity},param={param})")]
[InlineData(false, true, "NS.MyFunction(param={param})")]
[InlineData(false, false, "NS.MyFunction(entity={entity},param={param})")]
[InlineData(true, true, "MyFunction(param={param},param2=@param2)")]
[InlineData(true, false, "MyFunction(entity={entity},param={param},param2=@param2)")]
[InlineData(false, true, "NS.MyFunction(param={param},param2=@param2)")]
[InlineData(false, false, "NS.MyFunction(entity={entity},param={param},param2=@param2)")]
public void GetPathItemNameReturnsCorrectFunctionLiteral(bool unqualifiedCall, bool isBound, string expected)
{
// Arrange & Act
IEdmEntityTypeReference entityTypeReference = new EdmEntityTypeReference(new EdmEntityType("NS", "Entity"), false);
IEdmTypeReference parameterType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Boolean, isNullable: false);
EdmFunction boundFunction = BoundFunction("MyFunction", isBound, entityTypeReference);
boundFunction.AddParameter("param", parameterType);
boundFunction.AddOptionalParameter("param2", parameterType);

var segment = new ODataOperationSegment(boundFunction);
OpenApiConvertSettings settings = new OpenApiConvertSettings
Expand All @@ -97,17 +97,18 @@ public void GetPathItemNameReturnsCorrectFunctionLiteral(bool unqualifiedCall, b
}

[Theory]
[InlineData(true, true, "{param}")]
[InlineData(true, false, "NS.MyFunction(param='{param}')")]
[InlineData(false, true, "NS.MyFunction(param='{param}')")]
[InlineData(false, false, "NS.MyFunction(param='{param}')")]
[InlineData(true, true, "{param2}")]
[InlineData(true, false, "NS.MyFunction(param='{param}',param2='@param2')")]
[InlineData(false, true, "NS.MyFunction(param='{param}',param2='@param2')")]
[InlineData(false, false, "NS.MyFunction(param='{param}',param2='@param2')")]
public void GetPathItemNameReturnsCorrectFunctionLiteralForEscapedFunction(bool isEscapedFunction, bool enableEscapeFunctionCall, string expected)
{
// Arrange & Act
IEdmEntityTypeReference entityTypeReference = new EdmEntityTypeReference(new EdmEntityType("NS", "Entity"), false);
IEdmTypeReference parameterType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false);
EdmFunction boundFunction = BoundFunction("MyFunction", true, entityTypeReference);
boundFunction.AddParameter("param", parameterType);
boundFunction.AddOptionalParameter("param2", parameterType);

var segment = new ODataOperationSegment(boundFunction, isEscapedFunction);
OpenApiConvertSettings settings = new OpenApiConvertSettings
Expand All @@ -121,17 +122,18 @@ public void GetPathItemNameReturnsCorrectFunctionLiteralForEscapedFunction(bool
}

[Theory]
[InlineData(true, true, "{param}:")]
[InlineData(true, false, "NS.MyFunction(param='{param}')")]
[InlineData(false, true, "NS.MyFunction(param='{param}')")]
[InlineData(false, false, "NS.MyFunction(param='{param}')")]
[InlineData(true, true, "{param2}:")]
[InlineData(true, false, "NS.MyFunction(param='{param}',param2='@param2')")]
[InlineData(false, true, "NS.MyFunction(param='{param}',param2='@param2')")]
[InlineData(false, false, "NS.MyFunction(param='{param}',param2='@param2')")]
public void GetPathItemNameReturnsCorrectFunctionLiteralForEscapedComposableFunction(bool isEscapedFunction, bool enableEscapeFunctionCall, string expected)
{
// Arrange & Act
IEdmEntityTypeReference entityTypeReference = new EdmEntityTypeReference(new EdmEntityType("NS", "Entity"), false);
IEdmTypeReference parameterType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false);
EdmFunction boundFunction = BoundFunction("MyFunction", true, entityTypeReference, true);
boundFunction.AddParameter("param", parameterType);
boundFunction.AddOptionalParameter("param2", parameterType);

var segment = new ODataOperationSegment(boundFunction, isEscapedFunction);
OpenApiConvertSettings settings = new OpenApiConvertSettings
Expand Down