Skip to content

Fix for building LINQ Select clause on many-to-many includes #869

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
Oct 30, 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/Examples/JsonApiDotNetCoreExample/Models/Tag.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Resources.Annotations;

Expand All @@ -10,6 +12,11 @@ public class Tag : Identifiable

[Attr]
public TagColor Color { get; set; }

[NotMapped]
[HasManyThrough(nameof(ArticleTags))]
public ISet<Article> Articles { get; set; }
public ISet<ArticleTag> ArticleTags { get; set; }
}

public enum TagColor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Expression ApplySelect(IDictionary<ResourceFieldAttribute, QueryLayer> se
private Expression CreateLambdaBodyInitializer(IDictionary<ResourceFieldAttribute, QueryLayer> selectors, ResourceContext resourceContext,
LambdaScope lambdaScope, bool lambdaAccessorRequiresTestForNull)
{
var propertySelectors = ToPropertySelectors(selectors, resourceContext, lambdaScope.Parameter.Type);
var propertySelectors = ToPropertySelectors(selectors, resourceContext, lambdaScope.Accessor.Type);
MemberBinding[] propertyAssignments = propertySelectors.Select(selector => CreatePropertyAssignment(selector, lambdaScope)).Cast<MemberBinding>().ToArray();

NewExpression newExpression = _resourceFactory.CreateNewExpression(lambdaScope.Accessor.Type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,51 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
responseDocument.Included[0].Attributes["name"].Should().Be(article.ArticleTags.Single().Tag.Name);
}

[Fact]
public async Task Can_include_HasManyThrough_relationship_in_secondary_resource()
{
// Arrange
var article = new Article
{
Caption = "One",
ArticleTags = new HashSet<ArticleTag>
{
new ArticleTag
{
Tag = new Tag
{
Name = "Hot"
}
}
}
};

await _testContext.RunOnDatabaseAsync(async dbContext =>
{
dbContext.Articles.Add(article);

await dbContext.SaveChangesAsync();
});

var route = $"/api/v1/articles/{article.StringId}/tags?include=articles";

// Act
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);

// Assert
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);

responseDocument.ManyData.Should().HaveCount(1);
responseDocument.ManyData[0].Type.Should().Be("tags");
responseDocument.ManyData[0].Id.Should().Be(article.ArticleTags.ElementAt(0).Tag.StringId);
responseDocument.ManyData[0].Attributes["name"].Should().Be(article.ArticleTags.Single().Tag.Name);

responseDocument.Included.Should().HaveCount(1);
responseDocument.Included[0].Type.Should().Be("articles");
responseDocument.Included[0].Id.Should().Be(article.StringId);
responseDocument.Included[0].Attributes["caption"].Should().Be(article.Caption);
}

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