Skip to content

Fix $filter in (collectionofdynamicprimitivevalues) #3190

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

Merged
merged 6 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
73 changes: 72 additions & 1 deletion src/Microsoft.OData.Core/UriParser/Binders/InBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private CollectionNode GetCollectionOperandFromToken(QueryToken queryToken, IEdm
Debug.Assert(expectedType.IsCollection());
string expectedTypeFullName = expectedType.Definition.AsElementType().FullTypeName();

if (expectedTypeFullName.Equals("Edm.String", StringComparison.Ordinal) || expectedTypeFullName.Equals("Edm.Untyped", StringComparison.Ordinal))
if (expectedTypeFullName.Equals("Edm.String", StringComparison.Ordinal) || (expectedTypeFullName.Equals("Edm.Untyped", StringComparison.Ordinal) && IsCollectionContentEmptyOrSpaces(bracketLiteralText)))
{
// For collection of strings, need to convert single-quoted string to double-quoted string,
// and also, per ABNF, a single quote within a string literal is "encoded" as two consecutive single quotes in either
Expand Down Expand Up @@ -423,5 +423,76 @@ private static string NormalizeDateTimeCollectionItems(string bracketLiteralText

return "[" + String.Join(",", items) + "]";
}

private static bool IsCollectionContentEmptyOrSpaces(string bracketLiteralText)
{
if (string.IsNullOrWhiteSpace(bracketLiteralText) || bracketLiteralText.Length < 2)
{
return true;
}

string content = bracketLiteralText[1..^1].Trim();

if (string.IsNullOrWhiteSpace(content))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on the answer to this https://github.com/OData/odata.net/pull/3190/files#r1978721142, is this scenario possible?

I think that the first thing we should check is string.IsNullOrWhiteSpace at the beginning of this method

Copy link
Contributor Author

@ElizabethOkerio ElizabethOkerio Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is after removing the first and last brackets and trimming the spaces before and after content. I also don't think at this point we can have null or empty whitespaces without the brackets. So the check for isNullOrWhiteSpace will always return false.

{
return true;
}

bool isEmptyOrHasOnlySpaces = true;
bool isCharinsideQuotes = false;
char quoteChar = '\0';
Span<char> buffer = stackalloc char[content.Length];
int bufferIndex = 0;

for (int i = 0; i < content.Length; i++)
{
char c = content[i];

if (isCharinsideQuotes)
{
if (c == quoteChar)
{
isCharinsideQuotes = false;
}
buffer[bufferIndex++] = c;
}
else
{
if (c == '"' || c == '\'')
{
isCharinsideQuotes = true;
quoteChar = c;
buffer[bufferIndex++] = c;
}
else if (c == ',')
{
string item = new string(buffer[..bufferIndex]).Trim().Trim('\'', '"');

if (!string.IsNullOrWhiteSpace(item))
{
isEmptyOrHasOnlySpaces = false;
break;
}
bufferIndex = 0;
}
else
{
buffer[bufferIndex++] = c;
}
}
}

if (bufferIndex > 0)
{
string lastItem = new string(buffer[..bufferIndex]).Trim().Trim('\'', '"');

if (!string.IsNullOrWhiteSpace(lastItem))
{
isEmptyOrHasOnlySpaces = false;
}
}

return isEmptyOrHasOnlySpaces;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2130,6 +2130,22 @@ public void FilterWithInOperationWithParensCollection()
Assert.Equal("(1,2,3)", Assert.IsType<CollectionConstantNode>(inNode.Right).LiteralText);
}

[Theory]
[InlineData("TestInt", "(1,2,3)")]
[InlineData("TestDouble", "(1.1,2.2,3.3)")]
[InlineData("TestBool", "(true,false)")]
[InlineData("TestString", "('a','b','c')")]
[InlineData("TestDecimal", "(1.1,2.2,3.3)")]
public void FilterWithInOperationWithParensCollectionConsistingOfDynamicPrimitiveProperties(string propertyName, string values)
{
string filterExpression = $"{propertyName} in {values}";
FilterClause filter = ParseFilter(filterExpression, HardCodedTestModel.TestModel, HardCodedTestModel.GetOpenEmployeeType());

var inNode = Assert.IsType<InNode>(filter.Expression);
Assert.IsType<SingleValueOpenPropertyAccessNode>(inNode.Left);
Assert.Equal(values, Assert.IsType<CollectionConstantNode>(inNode.Right).LiteralText);
}

[Fact]
public void FilterWithInOperationWithParensCollectionAndLogicalNotOperator()
{
Expand Down