diff --git a/src/Examples/JsonApiDotNetCoreExample/Models/Tag.cs b/src/Examples/JsonApiDotNetCoreExample/Models/Tag.cs index ebd3bd5a1c..5ccb57a119 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Models/Tag.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Models/Tag.cs @@ -4,6 +4,7 @@ namespace JsonApiDotNetCoreExample.Models { public class Tag : Identifiable { + [Attr] public string Name { get; set; } } } \ No newline at end of file diff --git a/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs b/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs index b377dc94f5..c6abed49a7 100644 --- a/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs +++ b/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs @@ -217,7 +217,7 @@ private List 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) diff --git a/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj b/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj old mode 100755 new mode 100644 diff --git a/test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs b/test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs index 77c601373b..b5d80cdee8 100644 --- a/test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs +++ b/test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Net; @@ -5,6 +6,7 @@ using System.Net.Http.Headers; using System.Threading.Tasks; using Bogus; +using JsonApiDotNetCore.Models; using JsonApiDotNetCore.Serialization; using JsonApiDotNetCoreExample.Data; using JsonApiDotNetCoreExample.Models; @@ -24,25 +26,69 @@ public class ManyToManyTests private static readonly Faker _tagFaker = new Faker().RuleFor(a => a.Name, f => f.Random.AlphaNumeric(10)); private TestFixture _fixture; - public ManyToManyTests(TestFixture fixture) + public ManyToManyTests(TestFixture 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(); 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(body); + Assert.NotEmpty(document.Included); + + var articleResponseList = _fixture.GetService().DeserializeList
(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(); + 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 @@ -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(body); + Assert.NotEmpty(document.Included); + var articleResponse = _fixture.GetService().Deserialize
(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]