Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ public async Task Get_related_to_many_included()
}
}


[TestMethod]
[DeploymentItem(@"Data\Comment.csv", @"Data")]
[DeploymentItem(@"Data\Post.csv", @"Data")]
[DeploymentItem(@"Data\PostTagLink.csv", @"Data")]
[DeploymentItem(@"Data\Tag.csv", @"Data")]
[DeploymentItem(@"Data\User.csv", @"Data")]
public async Task Get_related_to_many_included_external()
{
using (var effortConnection = GetEffortConnection())
{
var response = await SubmitGet(effortConnection, "users/401/posts?include=tags");

await AssertResponseContent(response, @"Fixtures\FetchingResources\Get_related_to_many_include_external_response.json", HttpStatusCode.OK);
}
}

[TestMethod]
[DeploymentItem(@"Data\Comment.csv", @"Data")]
[DeploymentItem(@"Data\Post.csv", @"Data")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
{
"data": [
{
"type": "posts",
"id": "201",
"attributes": {
"content": "Post 1 content",
"created": "2015-01-31T14:00:00.0000000+00:00",
"title": "Post 1"
},
"relationships": {
"author": {
"links": {
"self": "https://www.example.com/posts/201/relationships/author",
"related": "https://www.example.com/posts/201/author"
}
},
"comments": {
"links": {
"self": "https://www.example.com/posts/201/relationships/comments",
"related": "https://www.example.com/posts/201/comments"
}
},
"tags": {
"links": {
"self": "https://www.example.com/posts/201/relationships/tags",
"related": "https://www.example.com/posts/201/tags"
},
"data": [
{
"type": "tags",
"id": "301"
},
{
"type": "tags",
"id": "302"
}
]
}
}
},
{
"type": "posts",
"id": "202",
"attributes": {
"content": "Post 2 content",
"created": "2015-02-05T08:10:00.0000000+00:00",
"title": "Post 2"
},
"relationships": {
"author": {
"links": {
"self": "https://www.example.com/posts/202/relationships/author",
"related": "https://www.example.com/posts/202/author"
}
},
"comments": {
"links": {
"self": "https://www.example.com/posts/202/relationships/comments",
"related": "https://www.example.com/posts/202/comments"
}
},
"tags": {
"links": {
"self": "https://www.example.com/posts/202/relationships/tags",
"related": "https://www.example.com/posts/202/tags"
},
"data": [
{
"type": "tags",
"id": "302"
},
{
"type": "tags",
"id": "303"
}
]
}
}
},
{
"type": "posts",
"id": "203",
"attributes": {
"content": "Post 3 content",
"created": "2015-02-07T11:11:00.0000000+00:00",
"title": "Post 3"
},
"relationships": {
"author": {
"links": {
"self": "https://www.example.com/posts/203/relationships/author",
"related": "https://www.example.com/posts/203/author"
}
},
"comments": {
"links": {
"self": "https://www.example.com/posts/203/relationships/comments",
"related": "https://www.example.com/posts/203/comments"
}
},
"tags": {
"links": {
"self": "https://www.example.com/posts/203/relationships/tags",
"related": "https://www.example.com/posts/203/tags"
},
"data": [
{
"type": "tags",
"id": "303"
}
]
}
}
}
],
"included": [
{
"type": "tags",
"id": "301",
"attributes": {
"name": "Tag A"
},
"relationships": {
"posts": {
"links": {
"self": "https://www.example.com/tags/301/relationships/posts",
"related": "https://www.example.com/tags/301/posts"
}
}
}
},
{
"type": "tags",
"id": "302",
"attributes": {
"name": "Tag B"
},
"relationships": {
"posts": {
"links": {
"self": "https://www.example.com/tags/302/relationships/posts",
"related": "https://www.example.com/tags/302/posts"
}
}
}
},
{
"type": "tags",
"id": "303",
"attributes": {
"name": "Tag C"
},
"relationships": {
"posts": {
"links": {
"self": "https://www.example.com/tags/303/relationships/posts",
"related": "https://www.example.com/tags/303/posts"
}
}
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@
<EmbeddedResource Include="Fixtures\Pagination\GetFilterPaged-2-1.json" />
<EmbeddedResource Include="Fixtures\Pagination\GetFilterPaged-1-2-sorted.json" />
<EmbeddedResource Include="Fixtures\Pagination\GetFilterPaged-1-2-sorted-desc.json" />
<EmbeddedResource Include="Fixtures\FetchingResources\Get_related_to_many_include_external_response.json" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,62 +45,54 @@ protected override async Task<IQueryable<TRelated>> GetRelatedQuery(string prima
var param = Expression.Parameter(typeof (TPrimaryResource));
var accessorExpr = Expression.Property(param, _relationship.Property);
var lambda = Expression.Lambda<Func<TPrimaryResource, IEnumerable<TRelated>>>(accessorExpr, param);
var primaryEntityQuery = FilterById(primaryResourceId, _primaryTypeRegistration, GetNavigationPropertiesIncludes<TPrimaryResource>(Includes));

var primaryEntityQuery = FilterById<TPrimaryResource>(primaryResourceId, _primaryTypeRegistration);

// We have to see if the resource even exists, so we can throw a 404 if it doesn't
var relatedResource = await primaryEntityQuery.FirstOrDefaultAsync(cancellationToken);
if (relatedResource == null)
throw JsonApiException.CreateForNotFound(string.Format(
"No resource of type `{0}` exists with id `{1}`.",
_primaryTypeRegistration.ResourceTypeName, primaryResourceId));
var includes = GetNavigationPropertiesIncludes(Includes);
var query = primaryEntityQuery.SelectMany(lambda);

return primaryEntityQuery.SelectMany(lambda);
if (includes != null && includes.Any())
query = includes.Aggregate(query, (current, include) => current.Include(include));
return query;
}


/// <summary>
/// This method allows to include <see cref="QueryableExtensions.Include{T}"/> into query.
/// This can reduce the number of queries (eager loading)
/// </summary>
/// <typeparam name="TResource"></typeparam>
/// <param name="includes"></param>
/// <returns></returns>
protected virtual Expression<Func<TResource, object>>[] GetNavigationPropertiesIncludes<TResource>(string[] includes)
protected virtual Expression<Func<TRelated, object>>[] GetNavigationPropertiesIncludes(string[] includes)
{
List<Expression<Func<TResource, object>>> list = new List<Expression<Func<TResource, object>>>();
List<Expression<Func<TRelated, object>>> list = new List<Expression<Func<TRelated, object>>>();
foreach (var include in includes)
{
var incl = include.Pascalize();
var param = Expression.Parameter(typeof(TResource));
var param = Expression.Parameter(typeof(TRelated));
var lambda =
Expression.Lambda<Func<TResource, object>>(
Expression.Lambda<Func<TRelated, object>>(
Expression.PropertyOrField(param, incl), param);
list.Add(lambda);
}
return list.ToArray();
}

private IQueryable<TResource> FilterById<TResource>(string id,
IResourceTypeRegistration resourceTypeRegistration,
params Expression<Func<TResource, object>>[] includes) where TResource : class
IResourceTypeRegistration resourceTypeRegistration) where TResource : class
{
var param = Expression.Parameter(typeof (TResource));
var filterByIdExpression = resourceTypeRegistration.GetFilterByIdExpression(param, id);
var predicate = Expression.Lambda<Func<TResource, bool>>(filterByIdExpression, param);
return QueryIncludeNavigationProperties(predicate, includes);
}

private IQueryable<TResource> QueryIncludeNavigationProperties<TResource>(Expression<Func<TResource, bool>> predicate,
params Expression<Func<TResource, object>>[] includes) where TResource : class
{
IQueryable<TResource> query = _dbContext.Set<TResource>();
if (includes != null && includes.Any())
query = includes.Aggregate(query, (current, include) => current.Include(include));

if (predicate != null)
query = query.Where(predicate);

return query.AsQueryable();
return query.Where(predicate);
}

}
}