Skip to content

Allow at least one constant instead of two in any() filter function #1234

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 1 commit into from
Dec 21, 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
2 changes: 1 addition & 1 deletion docs/usage/reading/filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ matchTextExpression:
( 'contains' | 'startsWith' | 'endsWith' ) LPAREN fieldChain COMMA literalConstant RPAREN;

anyExpression:
'any' LPAREN fieldChain COMMA literalConstant ( COMMA literalConstant )+ RPAREN;
'any' LPAREN fieldChain ( COMMA literalConstant )+ RPAREN;

hasExpression:
'has' LPAREN fieldChain ( COMMA filterExpression )? RPAREN;
Expand Down
7 changes: 1 addition & 6 deletions src/JsonApiDotNetCore/Queries/Expressions/AnyExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ public class AnyExpression : FilterExpression
public AnyExpression(ResourceFieldChainExpression targetAttribute, IImmutableSet<LiteralConstantExpression> constants)
{
ArgumentGuard.NotNull(targetAttribute);
ArgumentGuard.NotNull(constants);

if (constants.Count < 2)
{
throw new ArgumentException("At least two constants are required.", nameof(constants));
}
ArgumentGuard.NotNullNorEmpty(constants);

TargetAttribute = targetAttribute;
Constants = constants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,6 @@ protected AnyExpression ParseAny()
LiteralConstantExpression constant = ParseConstant();
constantsBuilder.Add(constant);

EatSingleCharacterToken(TokenKind.Comma);

constant = ParseConstant();
constantsBuilder.Add(constant);

while (TokenStack.TryPeek(out Token? nextToken) && nextToken.Kind == TokenKind.Comma)
{
EatSingleCharacterToken(TokenKind.Comma);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
}

[Theory]
[InlineData("yes", "no", "'yes'")]
[InlineData("two", "one two", "'one','two','three'")]
[InlineData("two", "nine", "'one','two','three','four','five'")]
public async Task Can_filter_in_set(string matchingText, string nonMatchingText, string filterText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void Reader_Is_Enabled(JsonApiQueryStringParameters parametersDisabled, b
[InlineData("filter", "any(null,'a','b')", "Attribute 'null' does not exist on resource type 'blogs'.")]
[InlineData("filter", "any('a','b','c')", "Field name expected.")]
[InlineData("filter", "any(title,'b','c',)", "Value between quotes expected.")]
[InlineData("filter", "any(title,'b')", ", expected.")]
[InlineData("filter", "any(title)", ", expected.")]
[InlineData("filter[posts]", "any(author,'a','b')", "Attribute 'author' does not exist on resource type 'blogPosts'.")]
[InlineData("filter", "and(", "Filter function expected.")]
[InlineData("filter", "or(equals(title,'some'),equals(title,'other')", ") expected.")]
Expand Down Expand Up @@ -146,6 +146,7 @@ public void Reader_Read_Fails(string parameterName, string parameterValue, strin
[InlineData("filter", "contains(title,'this')", null, "contains(title,'this')")]
[InlineData("filter", "startsWith(title,'this')", null, "startsWith(title,'this')")]
[InlineData("filter", "endsWith(title,'this')", null, "endsWith(title,'this')")]
[InlineData("filter", "any(title,'this')", null, "any(title,'this')")]
[InlineData("filter", "any(title,'this','that','there')", null, "any(title,'that','there','this')")]
[InlineData("filter", "and(contains(title,'sales'),contains(title,'marketing'),contains(title,'advertising'))", null,
"and(contains(title,'sales'),contains(title,'marketing'),contains(title,'advertising'))")]
Expand Down