Skip to content

Feat(#406): Allow filter[id] and filter[related.id] #458

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 3 commits into from
Dec 11, 2018
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
12 changes: 12 additions & 0 deletions src/JsonApiDotNetCore/Builders/ContextGraphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ protected virtual List<AttrAttribute> GetAttributes(Type entityType)

foreach (var prop in properties)
{
if (prop.Name == nameof(Identifiable.Id))
{
var idAttr = new AttrAttribute()
{
PublicAttributeName = JsonApiOptions.ResourceNameFormatter.FormatPropertyName(prop),
PropertyInfo = prop,
InternalAttributeName = prop.Name
};
attributes.Add(idAttr);
Copy link
Contributor

Choose a reason for hiding this comment

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

should there be a continue here, after the id property is added?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you're right. Now it's already there.

continue;
}

var attribute = (AttrAttribute)prop.GetCustomAttribute(typeof(AttrAttribute));
if (attribute == null)
continue;
Expand Down
8 changes: 6 additions & 2 deletions src/JsonApiDotNetCore/Builders/DocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public ResourceObject GetData(ContextEntity contextEntity, IIdentifiable entity,
private bool ShouldIncludeAttribute(AttrAttribute attr, object attributeValue)
{
return OmitNullValuedAttribute(attr, attributeValue) == false
&& attr.InternalAttributeName != nameof(Identifiable.Id)
&& ((_jsonApiContext.QuerySet == null
|| _jsonApiContext.QuerySet.Fields.Count == 0)
|| _jsonApiContext.QuerySet.Fields.Contains(attr.InternalAttributeName));
Expand Down Expand Up @@ -280,10 +281,13 @@ private ResourceObject GetIncludedEntity(IIdentifiable entity)

data.Attributes = new Dictionary<string, object>();

contextEntity.Attributes.ForEach(attr =>
foreach(var attr in contextEntity.Attributes)
{
if (attr.InternalAttributeName == nameof(Identifiable.Id))
continue;

data.Attributes.Add(attr.PublicAttributeName, attr.GetValue(entity));
});
}

return data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,54 @@ public async Task Can_Get_TodoItems()
Assert.True(deserializedBody.Count <= expectedEntitiesPerPage);
}

[Fact]
public async Task Can_Filter_By_Resource_Id()
{
// Arrange
var todoItem = _todoItemFaker.Generate();
_context.TodoItems.Add(todoItem);
_context.SaveChanges();

var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/todo-items?filter[id]={todoItem.Id}";
var request = new HttpRequestMessage(httpMethod, route);

// Act
var response = await _fixture.Client.SendAsync(request);
var body = await response.Content.ReadAsStringAsync();
var deserializedBody = _fixture.GetService<IJsonApiDeSerializer>().DeserializeList<TodoItem>(body);

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotEmpty(deserializedBody);
Assert.Contains(deserializedBody, (i) => i.Id == todoItem.Id);
}

[Fact]
public async Task Can_Filter_By_Relationship_Id()
{
// Arrange
var person = new Person();
var todoItem = _todoItemFaker.Generate();
todoItem.Owner = person;
_context.TodoItems.Add(todoItem);
_context.SaveChanges();

var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/todo-items?filter[owner.id]={person.Id}";
var request = new HttpRequestMessage(httpMethod, route);

// Act
var response = await _fixture.Client.SendAsync(request);
var body = await response.Content.ReadAsStringAsync();
var deserializedBody = _fixture.GetService<IJsonApiDeSerializer>().DeserializeList<TodoItem>(body);

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotEmpty(deserializedBody);
Assert.Contains(deserializedBody, (i) => i.OwnerId == person.Id);
}

[Fact]
public async Task Can_Filter_TodoItems()
{
Expand Down
4 changes: 2 additions & 2 deletions test/UnitTests/Builders/ContextGraphBuilder_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void Attrs_Without_Names_Specified_Will_Use_Default_Formatter()

// assert
var resource = graph.GetContextEntity(typeof(TestResource));
Assert.Equal("compound-attribute", resource.Attributes.Single().PublicAttributeName);
Assert.Contains(resource.Attributes, (i) => i.PublicAttributeName == "compound-attribute");
}

[Fact]
Expand All @@ -110,7 +110,7 @@ public void Attrs_Without_Names_Specified_Will_Use_Configured_Formatter()

// assert
var resource = graph.GetContextEntity(typeof(TestResource));
Assert.Equal("compoundAttribute", resource.Attributes.Single().PublicAttributeName);
Assert.Contains(resource.Attributes, (i) => i.PublicAttributeName == "compoundAttribute");
}

[Fact]
Expand Down