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

Fix for Error occurs when you check whether a string or integer literal is in an enum collection #3023

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
7 changes: 7 additions & 0 deletions src/Microsoft.OData.Core/UriParser/Binders/InBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ internal QueryNode BindInOperator(InToken inToken, BindingState state)
inToken.Right, new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetUntyped())), state.Model);
}

// If the left operand is either an integral or a string type and the right operand is a collection of enums,
// Calls the MetadataBindingUtils.ConvertToTypeIfNeeded() method to convert the left operand to the same enum type as the right operand.
if ((right is CollectionPropertyAccessNode && right.ItemType.IsEnum()) && (left.TypeReference.IsString() || left.TypeReference.IsIntegral()))
{
left = MetadataBindingUtils.ConvertToTypeIfNeeded(left, right.ItemType);
}

return new InNode(left, right);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.OData.UriParser;
using Microsoft.OData.Edm;
using Xunit;
using System.Linq;
WanjohiSammy marked this conversation as resolved.
Show resolved Hide resolved

namespace Microsoft.OData.Tests.ScenarioTests.UriParser
{
Expand Down Expand Up @@ -70,6 +71,10 @@ public EnumFilterFunctionalTests()
var enumFlagsTypeReference = new EdmEnumTypeReference(enumFlagsType, true);
this.entityType.AddProperty(new EdmStructuralProperty(this.entityType, "ColorFlags", enumFlagsTypeReference));

// add colors collection
EdmCollectionTypeReference colorCollectionTypeRef = new EdmCollectionTypeReference(new EdmCollectionType(new EdmEnumTypeReference(enumType, true)));
this.entityType.AddProperty(new EdmStructuralProperty(this.entityType, "Colors", colorCollectionTypeRef));

this.userModel.AddElement(this.entityType);

var defaultContainer = new EdmEntityContainer("NS", "DefaultContainer");
Expand Down Expand Up @@ -515,13 +520,205 @@ public void ParseFilterEnumTypesWrongCast3()
parse.Throws<ODataException>(Strings.MetadataBinder_CastOrIsOfFunctionWithoutATypeArgument);
}

[Fact]
public void ParseFilterWithInOperatorWithEnumsFullyQualifiedMemberName()
{
// Arrange
string enumValue = "White";
string filterQuery = $"NS.Color'{enumValue}' in Colors"; // "NS.Color'White' in Colors"
string expectedLiteral = "NS.Color'White'";

IEdmEnumType colorType = this.GetColorType(this.userModel);
IEdmEnumMember enumMember = colorType.Members.Single(m => m.Name == enumValue);

// Act
FilterClause filterQueryNode = ParseFilter(filterQuery, this.userModel, this.entityType, this.entitySet);
SingleValueNode expression = filterQueryNode.Expression;

// Assert
Assert.Equal("NS.Color'White' in Colors", filterQuery);
WanjohiSammy marked this conversation as resolved.
Show resolved Hide resolved
InNode inNode = expression.ShouldBeInNode();
ConstantNode leftNode = Assert.IsType<ConstantNode>(inNode.Left);
leftNode.ShouldBeEnumNode(colorType, enumMember.Value.Value);

Assert.True(leftNode.TypeReference.IsEnum());
Assert.Equal(enumMember.Value.Value.ToString(), leftNode.Value.ToString());
Assert.Equal(expectedLiteral, leftNode.LiteralText);

CollectionPropertyAccessNode rightNode = Assert.IsType<CollectionPropertyAccessNode>(inNode.Right);
rightNode.ShouldBeCollectionPropertyAccessQueryNode(this.GetColorsProperty(this.userModel));
Assert.Equal(colorType, rightNode.ItemType.Definition);
}

[Fact]
public void ParseFilterWithInOperatorWithEnumsMemberNameWithoutFullyQualifiedName()
{
// Arrange
string enumValue = "Green";
string filterQuery = $"'{enumValue}' in Colors"; // "'Green' in Colors"
string expectedLiteral = "'Green'";

IEdmEnumType colorType = this.GetColorType(this.userModel);

// Act
FilterClause filterQueryNode = ParseFilter(filterQuery, this.userModel, this.entityType, this.entitySet);
SingleValueNode expression = filterQueryNode.Expression;

// Assert
Assert.Equal("'Green' in Colors", filterQuery);
WanjohiSammy marked this conversation as resolved.
Show resolved Hide resolved
InNode inNode = expression.ShouldBeInNode();
ConstantNode leftNode = Assert.IsType<ConstantNode>(inNode.Left);
leftNode.ShouldBeEnumNode(colorType, enumValue);

Assert.True(leftNode.TypeReference.IsEnum());
Assert.Equal(enumValue, leftNode.Value.ToString());
Assert.Equal(expectedLiteral, leftNode.LiteralText);

CollectionPropertyAccessNode rightNode = Assert.IsType<CollectionPropertyAccessNode>(inNode.Right);
rightNode.ShouldBeCollectionPropertyAccessQueryNode(this.GetColorsProperty(this.userModel));
Assert.Equal(colorType, rightNode.ItemType.Definition);
}

[Fact]
public void ParseFilterWithInOperatorWithEnumsMemberIntegralValueWithSingleQuotes()
{
// Arrange
int enumValue = 1;
string filterQuery = $"'{enumValue}' in Colors"; // "'1' in Colors"
string expectedLiteral = "'Red'";

IEdmEnumType colorType = this.GetColorType(this.userModel);
bool success = colorType.TryParse(enumValue, out IEdmEnumMember enumMember);

// Act
FilterClause filterQueryNode = ParseFilter(filterQuery, this.userModel, this.entityType, this.entitySet);
SingleValueNode expression = filterQueryNode.Expression;

// Assert
Assert.Equal("'1' in Colors", filterQuery);
WanjohiSammy marked this conversation as resolved.
Show resolved Hide resolved
Assert.True(success);
InNode inNode = expression.ShouldBeInNode();
ConstantNode leftNode = Assert.IsType<ConstantNode>(inNode.Left);
leftNode.ShouldBeEnumNode(colorType, enumMember.Name);

Assert.True(leftNode.TypeReference.IsEnum());
Assert.Equal(enumMember.Name, leftNode.Value.ToString());
Assert.Equal(expectedLiteral, leftNode.LiteralText);

CollectionPropertyAccessNode rightNode = Assert.IsType<CollectionPropertyAccessNode>(inNode.Right);
rightNode.ShouldBeCollectionPropertyAccessQueryNode(this.GetColorsProperty(this.userModel));
Assert.Equal(colorType, rightNode.ItemType.Definition);
}

[Fact]
public void ParseFilterWithInOperatorWithEnumsMemberIntegralValueWithoutSingleQuotes()
{
// Arrange
int enumValue = 3;
string filterQuery = $"{enumValue} in Colors"; // "3 in Colors"
string expectedLiteral = "'Blue'";

IEdmEnumType colorType = this.GetColorType(this.userModel);
bool success = colorType.TryParse(enumValue, out IEdmEnumMember enumMember);

// Act
FilterClause filterQueryNode = ParseFilter(filterQuery, this.userModel, this.entityType, this.entitySet);
SingleValueNode expression = filterQueryNode.Expression;

// Assert
Assert.Equal("3 in Colors", filterQuery);
WanjohiSammy marked this conversation as resolved.
Show resolved Hide resolved
Assert.True(success);
InNode inNode = expression.ShouldBeInNode();
ConstantNode leftNode = Assert.IsType<ConstantNode>(inNode.Left);
leftNode.ShouldBeEnumNode(colorType, enumMember.Name);

Assert.True(leftNode.TypeReference.IsEnum());
Assert.Equal(enumMember.Name, leftNode.Value.ToString());
Assert.Equal(expectedLiteral, leftNode.LiteralText);

CollectionPropertyAccessNode rightNode = Assert.IsType<CollectionPropertyAccessNode>(inNode.Right);
rightNode.ShouldBeCollectionPropertyAccessQueryNode(this.GetColorsProperty(this.userModel));
Assert.Equal(colorType, rightNode.ItemType.Definition);
}

[Fact]
public void ParseFilterWithInOperatorWithEnumsMemberDoubleIntegralValue()
WanjohiSammy marked this conversation as resolved.
Show resolved Hide resolved
{
// Arrange
long enumValue = 3;
string filterQuery = $"{enumValue} in Colors"; // "3 in Colors"
string expectedLiteral = "'Blue'";

IEdmEnumType colorType = this.GetColorType(this.userModel);
bool success = colorType.TryParse(enumValue, out IEdmEnumMember enumMember);

// Act
FilterClause filterQueryNode = ParseFilter(filterQuery, this.userModel, this.entityType, this.entitySet);
SingleValueNode expression = filterQueryNode.Expression;

// Assert
Assert.Equal("3 in Colors", filterQuery);
WanjohiSammy marked this conversation as resolved.
Show resolved Hide resolved
Assert.True(success);
InNode inNode = expression.ShouldBeInNode();
ConstantNode leftNode = Assert.IsType<ConstantNode>(inNode.Left);
leftNode.ShouldBeEnumNode(colorType, enumMember.Name);

Assert.True(leftNode.TypeReference.IsEnum());
Assert.Equal(enumMember.Name, leftNode.Value.ToString());
Assert.Equal(expectedLiteral, leftNode.LiteralText);

CollectionPropertyAccessNode rightNode = Assert.IsType<CollectionPropertyAccessNode>(inNode.Right);
rightNode.ShouldBeCollectionPropertyAccessQueryNode(this.GetColorsProperty(this.userModel));
Assert.Equal(colorType, rightNode.ItemType.Definition);
}

[Theory]
[InlineData("3.0")]
[InlineData("3.1")]
[InlineData("4.0")]
[InlineData("5.0")]
WanjohiSammy marked this conversation as resolved.
Show resolved Hide resolved
public void ParseFilterWithInOperatorWithEnumsMemberFloatIntegralValue(string floatString)
{
// Arrange
string filterQuery = $"{floatString} in Colors";

// Act
Action test = () => ParseFilter(filterQuery, this.userModel, this.entityType, this.entitySet);

// Assert
test.Throws<ArgumentException>(Strings.Nodes_InNode_CollectionItemTypeMustBeSameAsSingleItemType("NS.Color", "Edm.Single")); // Float are of Type Edm.Single
}

[Theory]
[InlineData(-20)]
[InlineData("-20")]
WanjohiSammy marked this conversation as resolved.
Show resolved Hide resolved
[InlineData("'-20'")]
public void ParseFilterWithInOperatorWithEnumsMemberInvalidIntegralValue(object integralValue)
WanjohiSammy marked this conversation as resolved.
Show resolved Hide resolved
{
// Arrange
string filterQuery = $"{integralValue} in Colors";

// Act
Action action = () => ParseFilter(filterQuery, this.userModel, this.entityType, this.entitySet);

// Assert
action.Throws<ODataException>(Strings.Binder_IsNotValidEnumConstant("-20"));
}

private IEdmStructuralProperty GetColorProp(IEdmModel model)
{
return (IEdmStructuralProperty)((IEdmStructuredType)model
.FindType("NS.MyEntityType"))
.FindProperty("Color");
}

private IEdmStructuralProperty GetColorsProperty(IEdmModel model)
{
return (IEdmStructuralProperty)((IEdmStructuredType)model
.FindType("NS.MyEntityType"))
.FindProperty("Colors");
}

private IEdmEnumType GetColorType(IEdmModel model)
{
return (IEdmEnumType)model.FindType("NS.Color");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2041,6 +2041,91 @@ public void FilterWithInOperationWithEnums()
Assert.Equal("FavoriteColors", Assert.IsType<CollectionPropertyAccessNode>(inNode.Right).Property.Name);
}

[Fact]
public void FilterWithInOperationWithEnumsMemberName_WithoutQualifiedNamespace()
{
// Arrange
string filterQuery = "'SolidYellow' in FavoriteColors";

string expectedLiteralText = "'SolidYellow'";
string expectedfullyQualifiedName = "Fully.Qualified.Namespace.ColorPattern'SolidYellow'";
string expectedCollectionPropertyName = "FavoriteColors";

// Act
FilterClause filter = ParseFilter(filterQuery, HardCodedTestModel.TestModel, HardCodedTestModel.GetPersonType());

// Assert
InNode inNode = Assert.IsType<InNode>(filter.Expression);
ConstantNode left = Assert.IsType<ConstantNode>(inNode.Left);
ODataEnumValue enumValue = Assert.IsType<ODataEnumValue>(left.Value);

Assert.Equal(expectedLiteralText, left.LiteralText);
Assert.Equal(expectedfullyQualifiedName, (enumValue.TypeName + left.LiteralText));
Assert.Equal(expectedCollectionPropertyName, Assert.IsType<CollectionPropertyAccessNode>(inNode.Right).Property.Name);
}

[Fact]
public void FilterWithInOperationWithEnumsMemberIntegralValue_WithSingleQuotes()
{
// Arrange
string filterQuery = "'12' in FavoriteColors";

string expectedLiteralText = "'SolidYellow'";
string expectedfullyQualifiedName = "Fully.Qualified.Namespace.ColorPattern'SolidYellow'";
string expectedCollectionPropertyName = "FavoriteColors";

// Act
FilterClause filter = ParseFilter(filterQuery, HardCodedTestModel.TestModel, HardCodedTestModel.GetPersonType());

// Assert
InNode inNode = Assert.IsType<InNode>(filter.Expression);
ConstantNode left = Assert.IsType<ConstantNode>(inNode.Left);
ODataEnumValue enumValue = Assert.IsType<ODataEnumValue>(left.Value);

Assert.Equal(expectedLiteralText, left.LiteralText);
Assert.Equal(expectedfullyQualifiedName, (enumValue.TypeName + left.LiteralText));
Assert.Equal(expectedCollectionPropertyName, Assert.IsType<CollectionPropertyAccessNode>(inNode.Right).Property.Name);
}
WanjohiSammy marked this conversation as resolved.
Show resolved Hide resolved

[Fact]
public void FilterWithInOperationWithEnumsMemberIntegralValue_WithoutSingleQuotes()
{
// Arrange
string filterQuery = "12 in FavoriteColors";

string expectedLiteralText = "'SolidYellow'";
string expectedfullyQualifiedName = "Fully.Qualified.Namespace.ColorPattern'SolidYellow'";
string expectedCollectionPropertyName = "FavoriteColors";

// Act
FilterClause filter = ParseFilter(filterQuery, HardCodedTestModel.TestModel, HardCodedTestModel.GetPersonType());

// Assert
InNode inNode = Assert.IsType<InNode>(filter.Expression);
ConstantNode left = Assert.IsType<ConstantNode>(inNode.Left);
ODataEnumValue enumValue = Assert.IsType<ODataEnumValue>(left.Value);

Assert.Equal(expectedLiteralText, left.LiteralText);
Assert.Equal(expectedfullyQualifiedName, (enumValue.TypeName + left.LiteralText));
Assert.Equal(expectedCollectionPropertyName, Assert.IsType<CollectionPropertyAccessNode>(inNode.Right).Property.Name);
}

[Theory]
[InlineData(53)]
[InlineData("53")]
WanjohiSammy marked this conversation as resolved.
Show resolved Hide resolved
[InlineData("'53'")]
public void FilterWithInOperationWithEnumsInValidMemberIntegralValue_ThrowsIsNotValidEnumConstantException(object integralValue)
{
// Arrange
string filterQuery = $"{integralValue} in FavoriteColors";

// Act
Action action = () => ParseFilter(filterQuery, HardCodedTestModel.TestModel, HardCodedTestModel.GetPersonType());

// Assert
action.Throws<ODataException>(ODataErrorStrings.Binder_IsNotValidEnumConstant("53"));
}

[Fact]
public void FilterWithInOperationWithAny()
{
Expand Down
Loading