Skip to content

Commit 5c55973

Browse files
committed
fix(DocumentBuilder): duplicate included entities
use string.Equals over reference equality check
1 parent 2a3cdc4 commit 5c55973

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public Document Build(IIdentifiable entity)
3636
Meta = GetMeta(entity)
3737
};
3838

39-
if(ShouldIncludePageLinks(contextEntity))
39+
if (ShouldIncludePageLinks(contextEntity))
4040
document.Links = _jsonApiContext.PageManager.GetPageLinks(new LinkBuilder(_jsonApiContext));
4141

4242
document.Included = AppendIncludedObject(document.Included, contextEntity, entity);
@@ -59,7 +59,7 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
5959
Meta = GetMeta(enumeratedEntities.FirstOrDefault())
6060
};
6161

62-
if(ShouldIncludePageLinks(contextEntity))
62+
if (ShouldIncludePageLinks(contextEntity))
6363
documents.Links = _jsonApiContext.PageManager.GetPageLinks(new LinkBuilder(_jsonApiContext));
6464

6565
foreach (var entity in enumeratedEntities)
@@ -74,20 +74,20 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
7474
private Dictionary<string, object> GetMeta(IIdentifiable entity)
7575
{
7676
if (entity == null) return null;
77-
77+
7878
var builder = _jsonApiContext.MetaBuilder;
7979

80-
if(entity is IHasMeta metaEntity)
80+
if (entity is IHasMeta metaEntity)
8181
builder.Add(metaEntity.GetMeta(_jsonApiContext));
8282

83-
if(_jsonApiContext.Options.IncludeTotalRecordCount)
83+
if (_jsonApiContext.Options.IncludeTotalRecordCount)
8484
builder.Add("total-records", _jsonApiContext.PageManager.TotalRecords);
85-
86-
if(_requestMeta != null)
85+
86+
if (_requestMeta != null)
8787
builder.Add(_requestMeta.GetMeta());
8888

8989
var meta = builder.Build();
90-
if(meta.Count > 0) return meta;
90+
if (meta.Count > 0) return meta;
9191
return null;
9292
}
9393

@@ -119,7 +119,7 @@ public DocumentData GetData(ContextEntity contextEntity, IIdentifiable entity)
119119

120120
contextEntity.Attributes.ForEach(attr =>
121121
{
122-
if(ShouldIncludeAttribute(attr))
122+
if (ShouldIncludeAttribute(attr))
123123
data.Attributes.Add(attr.PublicAttributeName, attr.GetValue(entity));
124124
});
125125

@@ -131,8 +131,8 @@ public DocumentData GetData(ContextEntity contextEntity, IIdentifiable entity)
131131

132132
private bool ShouldIncludeAttribute(AttrAttribute attr)
133133
{
134-
return (_jsonApiContext.QuerySet == null
135-
|| _jsonApiContext.QuerySet.Fields.Count == 0
134+
return (_jsonApiContext.QuerySet == null
135+
|| _jsonApiContext.QuerySet.Fields.Count == 0
136136
|| _jsonApiContext.QuerySet.Fields.Contains(attr.InternalAttributeName));
137137
}
138138

@@ -145,13 +145,13 @@ private void AddRelationships(DocumentData data, ContextEntity contextEntity, II
145145
{
146146
var relationshipData = new RelationshipData();
147147

148-
if(r.DocumentLinks.HasFlag(Link.None) == false)
148+
if (r.DocumentLinks.HasFlag(Link.None) == false)
149149
{
150150
relationshipData.Links = new Links();
151-
if(r.DocumentLinks.HasFlag(Link.Self))
151+
if (r.DocumentLinks.HasFlag(Link.Self))
152152
relationshipData.Links.Self = linkBuilder.GetSelfRelationLink(contextEntity.EntityName, entity.StringId, r.PublicRelationshipName);
153-
154-
if(r.DocumentLinks.HasFlag(Link.Related))
153+
154+
if (r.DocumentLinks.HasFlag(Link.Related))
155155
relationshipData.Links.Related = linkBuilder.GetRelatedRelationLink(contextEntity.EntityName, entity.StringId, r.PublicRelationshipName);
156156
}
157157

@@ -160,7 +160,7 @@ private void AddRelationships(DocumentData data, ContextEntity contextEntity, II
160160
var navigationEntity = _jsonApiContext.ContextGraph
161161
.GetRelationship(entity, r.InternalRelationshipName);
162162

163-
if(navigationEntity == null)
163+
if (navigationEntity == null)
164164
relationshipData.SingleData = null;
165165
else if (navigationEntity is IEnumerable)
166166
relationshipData.ManyData = GetRelationships((IEnumerable<object>)navigationEntity);
@@ -194,19 +194,22 @@ private List<DocumentData> AddIncludedEntity(List<DocumentData> entities, IIdent
194194
{
195195
var includedEntity = GetIncludedEntity(entity);
196196

197-
if(entities == null)
197+
if (entities == null)
198198
entities = new List<DocumentData>();
199199

200-
if(includedEntity != null && !entities.Any(doc => doc.Id == includedEntity.Id && doc.Type == includedEntity.Type))
200+
if (includedEntity != null && entities.Any(doc =>
201+
string.Equals(doc.Id, includedEntity.Id) && string.Equals(doc.Type, includedEntity.Type)) == false)
202+
{
201203
entities.Add(includedEntity);
204+
}
202205

203206
return entities;
204207
}
205208

206209
private DocumentData GetIncludedEntity(IIdentifiable entity)
207210
{
208-
if(entity == null) return null;
209-
211+
if (entity == null) return null;
212+
210213
var contextEntity = _jsonApiContext.ContextGraph.GetContextEntity(entity.GetType());
211214

212215
var data = GetData(contextEntity, entity);

0 commit comments

Comments
 (0)