Skip to content
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
25 changes: 11 additions & 14 deletions src/JsonApiDotNetCore/Builders/DocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,17 @@ private void AddRelationships(DocumentData data, ContextEntity contextEntity, II
if (r.DocumentLinks.HasFlag(Link.Related))
relationshipData.Links.Related = linkBuilder.GetRelatedRelationLink(contextEntity.EntityName, entity.StringId, r.PublicRelationshipName);
}

if (RelationshipIsIncluded(r.PublicRelationshipName))
{
var navigationEntity = _jsonApiContext.ContextGraph
.GetRelationship(entity, r.InternalRelationshipName);

if (navigationEntity == null)
relationshipData.SingleData = null;
else if (navigationEntity is IEnumerable)
relationshipData.ManyData = GetRelationships((IEnumerable<object>)navigationEntity);
else
relationshipData.SingleData = GetRelationship(navigationEntity);
}


var navigationEntity = _jsonApiContext.ContextGraph
.GetRelationship(entity, r.InternalRelationshipName);

if (navigationEntity == null)
relationshipData.SingleData = null;
else if (navigationEntity is IEnumerable)
relationshipData.ManyData = GetRelationships((IEnumerable<object>)navigationEntity);
else
relationshipData.SingleData = GetRelationship(navigationEntity);

data.Relationships.Add(r.PublicRelationshipName, relationshipData);
});
}
Expand Down
49 changes: 41 additions & 8 deletions test/UnitTests/Builders/DocumentBuilder_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public DocumentBuilder_Tests()
_options.BuildContextGraph(builder =>
{
builder.AddResource<Model>("models");
builder.AddResource<RelatedModel>("related-models");
});

_jsonApiContextMock
Expand Down Expand Up @@ -59,7 +60,7 @@ public DocumentBuilder_Tests()
[Fact]
public void Includes_Paging_Links_By_Default()
{
// arrange
// arrange
_pageManager.PageSize = 1;
_pageManager.TotalRecords = 1;
_pageManager.CurrentPage = 1;
Expand Down Expand Up @@ -121,6 +122,38 @@ public void Related_Links_Can_Be_Disabled()
Assert.Null(document.Data.Relationships["related-model"].Links);
}

[Fact]
public void Related_Data_Included_In_Relationships_By_Default()
{
// arrange
const string relatedTypeName = "related-models";
const string relationshipName = "related-model";
const int relatedId = 1;
_jsonApiContextMock
.Setup(m => m.ContextGraph)
.Returns(_options.ContextGraph);

var documentBuilder = new DocumentBuilder(_jsonApiContextMock.Object);
var entity = new Model
{
RelatedModel = new RelatedModel
{
Id = relatedId
}
};

// act
var document = documentBuilder.Build(entity);

// assert
var relationshipData = document.Data.Relationships[relationshipName];
Assert.NotNull(relationshipData);
Assert.NotNull(relationshipData.SingleData);
Assert.NotNull(relationshipData.SingleData);
Assert.Equal(relatedId.ToString(), relationshipData.SingleData.Id);
Assert.Equal(relatedTypeName, relationshipData.SingleData.Type);
}

[Fact]
public void Build_Can_Build_Arrays()
{
Expand All @@ -145,12 +178,12 @@ public void Build_Can_Build_CustomIEnumerables()


[Theory]
[InlineData(null,null,true)]
[InlineData(false,null,true)]
[InlineData(true,null,false)]
[InlineData(null,"foo",true)]
[InlineData(false,"foo",true)]
[InlineData(true,"foo",true)]
[InlineData(null, null, true)]
[InlineData(false, null, true)]
[InlineData(true, null, false)]
[InlineData(null, "foo", true)]
[InlineData(false, "foo", true)]
[InlineData(true, "foo", true)]
public void DocumentBuilderOptions(bool? omitNullValuedAttributes,
string attributeValue,
bool resultContainsAttribute)
Expand All @@ -162,7 +195,7 @@ public void DocumentBuilderOptions(bool? omitNullValuedAttributes,
.Returns(new DocumentBuilderOptions(omitNullValuedAttributes.Value));
}
var documentBuilder = new DocumentBuilder(_jsonApiContextMock.Object, null, omitNullValuedAttributes.HasValue ? documentBuilderBehaviourMock.Object : null);
var document = documentBuilder.Build(new Model(){StringProperty = attributeValue});
var document = documentBuilder.Build(new Model() { StringProperty = attributeValue });

Assert.Equal(resultContainsAttribute, document.Data.Attributes.ContainsKey("StringProperty"));
}
Expand Down