Skip to content

Fix(#433): Nullable parent in nested inclusion #442

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 2 commits into from
Nov 13, 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
2 changes: 2 additions & 0 deletions src/JsonApiDotNetCore/Builders/DocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ private List<ResourceObject> IncludeRelationshipChain(
throw new JsonApiException(400, $"{parentEntity.EntityName} does not contain relationship {requestedRelationship}");

var navigationEntity = _jsonApiContext.ResourceGraph.GetRelationshipValue(parentResource, relationship);
if(navigationEntity == null)
return included;
if (navigationEntity is IEnumerable hasManyNavigationEntity)
{
foreach (IIdentifiable includedEntity in hasManyNavigationEntity)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -358,5 +359,56 @@ public async Task Request_ToIncludeRelationshipMarkedCanIncludeFalse_Returns_400
// assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}

[Fact]
public async Task Can_Ignore_Null_Parent_In_Nested_Include()
{
// arrange
var todoItem = _todoItemFaker.Generate();
todoItem.Owner = _personFaker.Generate();
todoItem.CreatedDate = DateTime.Now;
_context.TodoItems.Add(todoItem);
_context.SaveChanges();

var todoItemWithNullOwner = _todoItemFaker.Generate();
todoItemWithNullOwner.Owner = null;
todoItemWithNullOwner.CreatedDate = DateTime.Now;
_context.TodoItems.Add(todoItemWithNullOwner);
_context.SaveChanges();

var builder = new WebHostBuilder()
.UseStartup<Startup>();

var httpMethod = new HttpMethod("GET");

var route = $"/api/v1/todo-items?sort=-created-date&page[size]=2&include=owner.role"; // last two todo-items

var server = new TestServer(builder);
var client = server.CreateClient();
var request = new HttpRequestMessage(httpMethod, route);

// act
var response = await client.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
var documents = JsonConvert.DeserializeObject<Documents>(responseString);

// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Single(documents.Included);

var ownerValueNull = documents.Data
.First(i => i.Id == todoItemWithNullOwner.StringId)
.Relationships.First(i => i.Key == "owner")
.Value.SingleData;

Assert.Null(ownerValueNull);

var ownerValue = documents.Data
.First(i => i.Id == todoItem.StringId)
.Relationships.First(i => i.Key == "owner")
.Value.SingleData;

Assert.NotNull(ownerValue);
}
}
}