Skip to content

Commit

Permalink
Merge pull request #1234 from json-api-dotnet/allow-single-constant-i…
Browse files Browse the repository at this point in the history
…n-any-function

Allow at least one constant instead of two in any() filter function
  • Loading branch information
maurei authored Dec 21, 2022
2 parents 84d01c3 + faaba0b commit 85bdd01
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 13 deletions.
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

0 comments on commit 85bdd01

Please sign in to comment.