Skip to content

fix(#425): fetching HasManyThrough returns no includes #427

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 7 commits into from
Oct 14, 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
1 change: 1 addition & 0 deletions src/Examples/JsonApiDotNetCoreExample/Models/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace JsonApiDotNetCoreExample.Models
{
public class Tag : Identifiable
{
[Attr]
public string Name { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/Builders/DocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private List<ResourceObject> IncludeRelationshipChain(
if(relationship == null)
throw new JsonApiException(400, $"{parentEntity.EntityName} does not contain relationship {requestedRelationship}");

var navigationEntity = _jsonApiContext.ContextGraph.GetRelationship(parentResource, relationship.InternalRelationshipName);
var navigationEntity = _jsonApiContext.ContextGraph.GetRelationshipValue(parentResource, relationship);
if (navigationEntity is IEnumerable hasManyNavigationEntity)
{
foreach (IIdentifiable includedEntity in hasManyNavigationEntity)
Expand Down
Empty file modified src/JsonApiDotNetCore/JsonApiDotNetCore.csproj
100755 → 100644
Empty file.
58 changes: 54 additions & 4 deletions test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Bogus;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCore.Serialization;
using JsonApiDotNetCoreExample.Data;
using JsonApiDotNetCoreExample.Models;
Expand All @@ -24,25 +26,69 @@ public class ManyToManyTests
private static readonly Faker<Tag> _tagFaker = new Faker<Tag>().RuleFor(a => a.Name, f => f.Random.AlphaNumeric(10));

private TestFixture<TestStartup> _fixture;
public ManyToManyTests(TestFixture<TestStartup> fixture)
public ManyToManyTests(TestFixture<TestStartup> fixture)
{
_fixture = fixture;
}

[Fact]
public async Task Can_Fetch_Many_To_Many_Through()
public async Task Can_Fetch_Many_To_Many_Through_All()
{
// arrange
var context = _fixture.GetService<AppDbContext>();
var article = _articleFaker.Generate();
var tag = _tagFaker.Generate();
var articleTag = new ArticleTag {

context.Articles.RemoveRange(context.Articles);
await context.SaveChangesAsync();

var articleTag = new ArticleTag
{
Article = article,
Tag = tag
};
context.ArticleTags.Add(articleTag);
await context.SaveChangesAsync();

var route = $"/api/v1/articles?include=tags";

// act
var response = await _fixture.Client.GetAsync(route);

// assert
var body = await response.Content.ReadAsStringAsync();
Assert.True(HttpStatusCode.OK == response.StatusCode, $"{route} returned {response.StatusCode} status code with payload: {body}");

var document = JsonConvert.DeserializeObject<Documents>(body);
Assert.NotEmpty(document.Included);

var articleResponseList = _fixture.GetService<IJsonApiDeSerializer>().DeserializeList<Article>(body);
Assert.NotNull(articleResponseList);

var articleResponse = articleResponseList.FirstOrDefault(a => a.Id == article.Id);
Assert.NotNull(articleResponse);
Assert.Equal(article.Name, articleResponse.Name);

var tagResponse = Assert.Single(articleResponse.Tags);
Assert.Equal(tag.Id, tagResponse.Id);
Assert.Equal(tag.Name, tagResponse.Name);
}

[Fact]
public async Task Can_Fetch_Many_To_Many_Through_GetById()
{
// arrange
var context = _fixture.GetService<AppDbContext>();
var article = _articleFaker.Generate();
var tag = _tagFaker.Generate();
var articleTag = new ArticleTag
{
Article = article,
Tag = tag
};
context.ArticleTags.Add(articleTag);
await context.SaveChangesAsync();

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

// act
Expand All @@ -52,12 +98,16 @@ public async Task Can_Fetch_Many_To_Many_Through()
var body = await response.Content.ReadAsStringAsync();
Assert.True(HttpStatusCode.OK == response.StatusCode, $"{route} returned {response.StatusCode} status code with payload: {body}");

var document = JsonConvert.DeserializeObject<Document>(body);
Assert.NotEmpty(document.Included);

var articleResponse = _fixture.GetService<IJsonApiDeSerializer>().Deserialize<Article>(body);
Assert.NotNull(articleResponse);
Assert.Equal(article.Id, articleResponse.Id);

var tagResponse = Assert.Single(articleResponse.Tags);
Assert.Equal(tag.Id, tagResponse.Id);
Assert.Equal(tag.Name, tagResponse.Name);
}

[Fact]
Expand Down