Skip to content

Commit 90d32df

Browse files
authored
Merge pull request #442 from milosloub/fix/#433
Fix(#433): Nullable parent in nested inclusion
2 parents 89c38dc + f55b5b1 commit 90d32df

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ private List<ResourceObject> IncludeRelationshipChain(
218218
throw new JsonApiException(400, $"{parentEntity.EntityName} does not contain relationship {requestedRelationship}");
219219

220220
var navigationEntity = _jsonApiContext.ResourceGraph.GetRelationshipValue(parentResource, relationship);
221+
if(navigationEntity == null)
222+
return included;
221223
if (navigationEntity is IEnumerable hasManyNavigationEntity)
222224
{
223225
foreach (IIdentifiable includedEntity in hasManyNavigationEntity)

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/DocumentTests/Included.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Linq;
23
using System.Net;
34
using System.Net.Http;
@@ -358,5 +359,56 @@ public async Task Request_ToIncludeRelationshipMarkedCanIncludeFalse_Returns_400
358359
// assert
359360
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
360361
}
362+
363+
[Fact]
364+
public async Task Can_Ignore_Null_Parent_In_Nested_Include()
365+
{
366+
// arrange
367+
var todoItem = _todoItemFaker.Generate();
368+
todoItem.Owner = _personFaker.Generate();
369+
todoItem.CreatedDate = DateTime.Now;
370+
_context.TodoItems.Add(todoItem);
371+
_context.SaveChanges();
372+
373+
var todoItemWithNullOwner = _todoItemFaker.Generate();
374+
todoItemWithNullOwner.Owner = null;
375+
todoItemWithNullOwner.CreatedDate = DateTime.Now;
376+
_context.TodoItems.Add(todoItemWithNullOwner);
377+
_context.SaveChanges();
378+
379+
var builder = new WebHostBuilder()
380+
.UseStartup<Startup>();
381+
382+
var httpMethod = new HttpMethod("GET");
383+
384+
var route = $"/api/v1/todo-items?sort=-created-date&page[size]=2&include=owner.role"; // last two todo-items
385+
386+
var server = new TestServer(builder);
387+
var client = server.CreateClient();
388+
var request = new HttpRequestMessage(httpMethod, route);
389+
390+
// act
391+
var response = await client.SendAsync(request);
392+
var responseString = await response.Content.ReadAsStringAsync();
393+
var documents = JsonConvert.DeserializeObject<Documents>(responseString);
394+
395+
// assert
396+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
397+
Assert.Single(documents.Included);
398+
399+
var ownerValueNull = documents.Data
400+
.First(i => i.Id == todoItemWithNullOwner.StringId)
401+
.Relationships.First(i => i.Key == "owner")
402+
.Value.SingleData;
403+
404+
Assert.Null(ownerValueNull);
405+
406+
var ownerValue = documents.Data
407+
.First(i => i.Id == todoItem.StringId)
408+
.Relationships.First(i => i.Key == "owner")
409+
.Value.SingleData;
410+
411+
Assert.NotNull(ownerValue);
412+
}
361413
}
362414
}

0 commit comments

Comments
 (0)