-
-
Notifications
You must be signed in to change notification settings - Fork 158
Add IN filter operation for array searching #288
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,14 +82,23 @@ protected virtual List<FilterQuery> ParseFilterQuery(string key, string value) | |
// expected input = filter[id]=1 | ||
// expected input = filter[id]=eq:1 | ||
var queries = new List<FilterQuery>(); | ||
|
||
var propertyName = key.Split(QueryConstants.OPEN_BRACKET, QueryConstants.CLOSE_BRACKET)[1]; | ||
|
||
var values = value.Split(QueryConstants.COMMA); | ||
foreach (var val in values) | ||
// InArray case | ||
string op = GetFilterOperation(value); | ||
if (string.Equals(op, [email protected](), StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
(var operation, var filterValue) = ParseFilterOperation(value); | ||
queries.Add(new FilterQuery(propertyName, filterValue, op)); | ||
} | ||
else | ||
{ | ||
(var operation, var filterValue) = ParseFilterOperation(val); | ||
queries.Add(new FilterQuery(propertyName, filterValue, operation)); | ||
var values = value.Split(QueryConstants.COMMA); | ||
foreach (var val in values) | ||
{ | ||
(var operation, var filterValue) = ParseFilterOperation(val); | ||
queries.Add(new FilterQuery(propertyName, filterValue, operation)); | ||
} | ||
} | ||
|
||
return queries; | ||
|
@@ -100,19 +109,15 @@ protected virtual (string operation, string value) ParseFilterOperation(string v | |
if (value.Length < 3) | ||
return (string.Empty, value); | ||
|
||
var operation = value.Split(QueryConstants.COLON); | ||
|
||
if (operation.Length == 1) | ||
return (string.Empty, value); | ||
var operation = GetFilterOperation(value); | ||
var values = value.Split(QueryConstants.COLON); | ||
|
||
// remove prefix from value | ||
if (Enum.TryParse(operation[0], out FilterOperations op) == false) | ||
if (string.IsNullOrEmpty(operation)) | ||
return (string.Empty, value); | ||
|
||
var prefix = operation[0]; | ||
value = string.Join(QueryConstants.COLON_STR, operation.Skip(1)); | ||
value = string.Join(QueryConstants.COLON_STR, values.Skip(1)); | ||
|
||
return (prefix, value); | ||
return (operation, value); | ||
} | ||
|
||
protected virtual PageQuery ParsePageQuery(PageQuery pageQuery, string key, string value) | ||
|
@@ -225,6 +230,21 @@ protected virtual AttrAttribute GetAttribute(string propertyName) | |
} | ||
} | ||
|
||
private string GetFilterOperation(string value) | ||
{ | ||
var values = value.Split(QueryConstants.COLON); | ||
|
||
if (values.Length == 1) | ||
return string.Empty; | ||
|
||
var operation = values[0]; | ||
// remove prefix from value | ||
if (Enum.TryParse(operation, out FilterOperations op) == false) | ||
return string.Empty; | ||
|
||
return operation; | ||
} | ||
|
||
private FilterQuery BuildFilterQuery(ReadOnlySpan<char> query, string propertyName) | ||
{ | ||
var (operation, filterValue) = ParseFilterOperation(query.ToString()); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
|
@@ -8,6 +9,7 @@ | |
using JsonApiDotNetCore.Serialization; | ||
using JsonApiDotNetCoreExample.Data; | ||
using JsonApiDotNetCoreExample.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
using Newtonsoft.Json; | ||
using Xunit; | ||
using Person = JsonApiDotNetCoreExample.Models.Person; | ||
|
@@ -131,5 +133,82 @@ public async Task Can_Filter_On_Not_Equal_Values() | |
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
Assert.False(deserializedTodoItems.Any(i => i.Ordinal == todoItem.Ordinal)); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_Filter_On_In_Array_Values() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need a negative test, i.e. instances with property values that are not included in the array get excluded from the results There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I'll add this test case. |
||
{ | ||
// arrange | ||
var context = _fixture.GetService<AppDbContext>(); | ||
var todoItems = _todoItemFaker.Generate(5); | ||
var guids = new List<Guid>(); | ||
var notInGuids = new List<Guid>(); | ||
foreach (var item in todoItems) | ||
{ | ||
context.TodoItems.Add(item); | ||
// Exclude 2 items | ||
if (guids.Count < (todoItems.Count() - 2)) | ||
guids.Add(item.GuidProperty); | ||
else | ||
notInGuids.Add(item.GuidProperty); | ||
} | ||
context.SaveChanges(); | ||
|
||
var totalCount = context.TodoItems.Count(); | ||
var httpMethod = new HttpMethod("GET"); | ||
var route = $"/api/v1/todo-items?filter[guid-property]=in:{string.Join(",", guids)}"; | ||
var request = new HttpRequestMessage(httpMethod, route); | ||
|
||
// act | ||
var response = await _fixture.Client.SendAsync(request); | ||
var body = await response.Content.ReadAsStringAsync(); | ||
var deserializedTodoItems = _fixture | ||
.GetService<IJsonApiDeSerializer>() | ||
.DeserializeList<TodoItem>(body); | ||
|
||
// assert | ||
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
Assert.Equal(guids.Count(), deserializedTodoItems.Count()); | ||
foreach (var item in deserializedTodoItems) | ||
{ | ||
Assert.True(guids.Contains(item.GuidProperty)); | ||
Assert.False(notInGuids.Contains(item.GuidProperty)); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Can_Filter_On_Related_In_Array_Values() | ||
{ | ||
// arrange | ||
var context = _fixture.GetService<AppDbContext>(); | ||
var todoItems = _todoItemFaker.Generate(3); | ||
var ownerFirstNames = new List<string>(); | ||
foreach (var item in todoItems) | ||
{ | ||
var person = _personFaker.Generate(); | ||
ownerFirstNames.Add(person.FirstName); | ||
item.Owner = person; | ||
context.TodoItems.Add(item); | ||
} | ||
context.SaveChanges(); | ||
|
||
var httpMethod = new HttpMethod("GET"); | ||
var route = $"/api/v1/todo-items?include=owner&filter[owner.first-name]=in:{string.Join(",", ownerFirstNames)}"; | ||
var request = new HttpRequestMessage(httpMethod, route); | ||
|
||
// act | ||
var response = await _fixture.Client.SendAsync(request); | ||
var body = await response.Content.ReadAsStringAsync(); | ||
var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync()); | ||
var included = documents.Included; | ||
|
||
// assert | ||
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
Assert.Equal(ownerFirstNames.Count(), documents.Data.Count()); | ||
Assert.NotNull(included); | ||
Assert.NotEmpty(included); | ||
foreach (var item in included) | ||
Assert.True(ownerFirstNames.Contains(item.Attributes["first-name"])); | ||
|
||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 awesome!