Skip to content

Fixes in filtering #752

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
May 15, 2020
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
7 changes: 7 additions & 0 deletions src/JsonApiDotNetCore/QueryParameterServices/FilterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ private FilterQueryContext GetQueryContexts(FilterQuery query, string parameterN
queryContext.Relationship = GetRelationship(parameterName, query.Relationship);
var attribute = GetAttribute(parameterName, query.Attribute, queryContext.Relationship);

if (queryContext.Relationship is HasManyAttribute)
{
throw new InvalidQueryStringParameterException(parameterName,
"Filtering on one-to-many and many-to-many relationships is currently not supported.",
$"Filtering on the relationship '{queryContext.Relationship.PublicRelationshipName}.{attribute.PublicAttributeName}' is currently not supported.");
}

if (!attribute.Capabilities.HasFlag(AttrCapabilities.AllowFilter))
{
throw new InvalidQueryStringParameterException(parameterName, "Filtering on the requested attribute is not allowed.",
Expand Down
2 changes: 2 additions & 0 deletions src/JsonApiDotNetCore/Services/DefaultResourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ public virtual async Task<TResource> GetAsync(TId id)
_hookExecutor?.BeforeRead<TResource>(pipeline, id.ToString());

var entityQuery = _repository.Get(id);
entityQuery = ApplyFilter(entityQuery);
entityQuery = ApplyInclude(entityQuery);
entityQuery = ApplySelect(entityQuery);

var entity = await _repository.FirstOrDefaultAsync(entityQuery);

if (entity == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,52 @@ public async Task Can_Filter_On_Related_Attrs()
list.Owner.FirstName = person.FirstName;
}

[Fact]
public async Task Can_Filter_On_Related_Attrs_From_GetById()
{
// Arrange
var context = _fixture.GetService<AppDbContext>();
var person = _personFaker.Generate();
var todoItem = _todoItemFaker.Generate();
todoItem.Owner = person;
context.TodoItems.Add(todoItem);
await context.SaveChangesAsync();

var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/todoItems/{todoItem.Id}?include=owner&filter[owner.firstName]=SOMETHING-ELSE";
var request = new HttpRequestMessage(httpMethod, route);

// Act
var response = await _fixture.Client.SendAsync(request);
var body = await response.Content.ReadAsStringAsync();

// Assert
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}

[Fact]
public async Task Cannot_Filter_On_Related_ToMany_Attrs()
{
// Arrange
var httpMethod = new HttpMethod("GET");
var route = "/api/v1/todoItems?include=childrenTodos&filter[childrenTodos.ordinal]=1";
var request = new HttpRequestMessage(httpMethod, route);

// Act
var response = await _fixture.Client.SendAsync(request);

// Assert
var body = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);

var errorDocument = JsonConvert.DeserializeObject<ErrorDocument>(body);
Assert.Single(errorDocument.Errors);
Assert.Equal(HttpStatusCode.BadRequest, errorDocument.Errors[0].StatusCode);
Assert.Equal("Filtering on one-to-many and many-to-many relationships is currently not supported.", errorDocument.Errors[0].Title);
Assert.Equal("Filtering on the relationship 'childrenTodos.ordinal' is currently not supported.", errorDocument.Errors[0].Detail);
Assert.Equal("filter[childrenTodos.ordinal]", errorDocument.Errors[0].Source.Parameter);
}

[Fact]
public async Task Cannot_Filter_If_Explicitly_Forbidden()
{
Expand Down