Skip to content

Commit 89662a8

Browse files
authored
Merge pull request #280 from crfloyd/feature/#218
#218 Defaults data to be included in relationships rather than only w…
2 parents a793460 + 3e46dee commit 89662a8

File tree

2 files changed

+52
-22
lines changed

2 files changed

+52
-22
lines changed

Diff for: src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

+11-14
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,17 @@ private void AddRelationships(DocumentData data, ContextEntity contextEntity, II
158158
if (r.DocumentLinks.HasFlag(Link.Related))
159159
relationshipData.Links.Related = linkBuilder.GetRelatedRelationLink(contextEntity.EntityName, entity.StringId, r.PublicRelationshipName);
160160
}
161-
162-
if (RelationshipIsIncluded(r.PublicRelationshipName))
163-
{
164-
var navigationEntity = _jsonApiContext.ContextGraph
165-
.GetRelationship(entity, r.InternalRelationshipName);
166-
167-
if (navigationEntity == null)
168-
relationshipData.SingleData = null;
169-
else if (navigationEntity is IEnumerable)
170-
relationshipData.ManyData = GetRelationships((IEnumerable<object>)navigationEntity);
171-
else
172-
relationshipData.SingleData = GetRelationship(navigationEntity);
173-
}
174-
161+
162+
var navigationEntity = _jsonApiContext.ContextGraph
163+
.GetRelationship(entity, r.InternalRelationshipName);
164+
165+
if (navigationEntity == null)
166+
relationshipData.SingleData = null;
167+
else if (navigationEntity is IEnumerable)
168+
relationshipData.ManyData = GetRelationships((IEnumerable<object>)navigationEntity);
169+
else
170+
relationshipData.SingleData = GetRelationship(navigationEntity);
171+
175172
data.Relationships.Add(r.PublicRelationshipName, relationshipData);
176173
});
177174
}

Diff for: test/UnitTests/Builders/DocumentBuilder_Tests.cs

+41-8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public DocumentBuilder_Tests()
2828
_options.BuildContextGraph(builder =>
2929
{
3030
builder.AddResource<Model>("models");
31+
builder.AddResource<RelatedModel>("related-models");
3132
});
3233

3334
_jsonApiContextMock
@@ -59,7 +60,7 @@ public DocumentBuilder_Tests()
5960
[Fact]
6061
public void Includes_Paging_Links_By_Default()
6162
{
62-
// arrange
63+
// arrange
6364
_pageManager.PageSize = 1;
6465
_pageManager.TotalRecords = 1;
6566
_pageManager.CurrentPage = 1;
@@ -121,6 +122,38 @@ public void Related_Links_Can_Be_Disabled()
121122
Assert.Null(document.Data.Relationships["related-model"].Links);
122123
}
123124

125+
[Fact]
126+
public void Related_Data_Included_In_Relationships_By_Default()
127+
{
128+
// arrange
129+
const string relatedTypeName = "related-models";
130+
const string relationshipName = "related-model";
131+
const int relatedId = 1;
132+
_jsonApiContextMock
133+
.Setup(m => m.ContextGraph)
134+
.Returns(_options.ContextGraph);
135+
136+
var documentBuilder = new DocumentBuilder(_jsonApiContextMock.Object);
137+
var entity = new Model
138+
{
139+
RelatedModel = new RelatedModel
140+
{
141+
Id = relatedId
142+
}
143+
};
144+
145+
// act
146+
var document = documentBuilder.Build(entity);
147+
148+
// assert
149+
var relationshipData = document.Data.Relationships[relationshipName];
150+
Assert.NotNull(relationshipData);
151+
Assert.NotNull(relationshipData.SingleData);
152+
Assert.NotNull(relationshipData.SingleData);
153+
Assert.Equal(relatedId.ToString(), relationshipData.SingleData.Id);
154+
Assert.Equal(relatedTypeName, relationshipData.SingleData.Type);
155+
}
156+
124157
[Fact]
125158
public void Build_Can_Build_Arrays()
126159
{
@@ -145,12 +178,12 @@ public void Build_Can_Build_CustomIEnumerables()
145178

146179

147180
[Theory]
148-
[InlineData(null,null,true)]
149-
[InlineData(false,null,true)]
150-
[InlineData(true,null,false)]
151-
[InlineData(null,"foo",true)]
152-
[InlineData(false,"foo",true)]
153-
[InlineData(true,"foo",true)]
181+
[InlineData(null, null, true)]
182+
[InlineData(false, null, true)]
183+
[InlineData(true, null, false)]
184+
[InlineData(null, "foo", true)]
185+
[InlineData(false, "foo", true)]
186+
[InlineData(true, "foo", true)]
154187
public void DocumentBuilderOptions(bool? omitNullValuedAttributes,
155188
string attributeValue,
156189
bool resultContainsAttribute)
@@ -162,7 +195,7 @@ public void DocumentBuilderOptions(bool? omitNullValuedAttributes,
162195
.Returns(new DocumentBuilderOptions(omitNullValuedAttributes.Value));
163196
}
164197
var documentBuilder = new DocumentBuilder(_jsonApiContextMock.Object, null, omitNullValuedAttributes.HasValue ? documentBuilderBehaviourMock.Object : null);
165-
var document = documentBuilder.Build(new Model(){StringProperty = attributeValue});
198+
var document = documentBuilder.Build(new Model() { StringProperty = attributeValue });
166199

167200
Assert.Equal(resultContainsAttribute, document.Data.Attributes.ContainsKey("StringProperty"));
168201
}

0 commit comments

Comments
 (0)