Skip to content

Commit e4dd855

Browse files
committed
#162 Adds new boolean value for 'canInclude' to ResourceAttribute. Updates DefaultEntityRepository to check this value when including relationship entities and to throw if set to false.
1 parent e7c65db commit e4dd855

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,18 @@ public virtual IQueryable<TEntity> Include(IQueryable<TEntity> entities, string
131131
{
132132
var entity = _jsonApiContext.RequestEntity;
133133
var relationship = entity.Relationships.FirstOrDefault(r => r.PublicRelationshipName == relationshipName);
134-
if (relationship != null)
135-
return entities.Include(relationship.InternalRelationshipName);
134+
if (relationship == null)
135+
{
136+
throw new JsonApiException(400, $"Invalid relationship {relationshipName} on {entity.EntityName}",
137+
$"{entity.EntityName} does not have a relationship named {relationshipName}");
138+
}
139+
140+
if (!relationship.CanInclude)
141+
{
142+
throw new JsonApiException(400, $"Including the relationship {relationshipName} on {entity.EntityName} is not allowed");
143+
}
144+
return entities.Include(relationship.InternalRelationshipName);
136145

137-
throw new JsonApiException(400, $"Invalid relationship {relationshipName} on {entity.EntityName}",
138-
$"{entity.EntityName} does not have a relationship named {relationshipName}");
139146
}
140147

141148
public virtual async Task<IEnumerable<TEntity>> PageAsync(IQueryable<TEntity> entities, int pageSize, int pageNumber)

src/JsonApiDotNetCore/Models/HasManyAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ namespace JsonApiDotNetCore.Models
22
{
33
public class HasManyAttribute : RelationshipAttribute
44
{
5-
public HasManyAttribute(string publicName, Link documentLinks = Link.All)
6-
: base(publicName, documentLinks)
5+
public HasManyAttribute(string publicName, Link documentLinks = Link.All, bool canInclude = true)
6+
: base(publicName, documentLinks, canInclude)
77
{ }
88

99
public override void SetValue(object entity, object newValue)

src/JsonApiDotNetCore/Models/HasOneAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ namespace JsonApiDotNetCore.Models
22
{
33
public class HasOneAttribute : RelationshipAttribute
44
{
5-
public HasOneAttribute(string publicName, Link documentLinks = Link.All)
6-
: base(publicName, documentLinks)
5+
public HasOneAttribute(string publicName, Link documentLinks = Link.All, bool canInclude = true)
6+
: base(publicName, documentLinks, canInclude)
77
{ }
88

99
public override void SetValue(object entity, object newValue)

src/JsonApiDotNetCore/Models/RelationshipAttribute.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ namespace JsonApiDotNetCore.Models
44
{
55
public abstract class RelationshipAttribute : Attribute
66
{
7-
protected RelationshipAttribute(string publicName, Link documentLinks)
7+
protected RelationshipAttribute(string publicName, Link documentLinks, bool canInclude)
88
{
99
PublicRelationshipName = publicName;
1010
DocumentLinks = documentLinks;
11+
CanInclude = canInclude;
1112
}
1213

1314
public string PublicRelationshipName { get; }
@@ -16,6 +17,7 @@ protected RelationshipAttribute(string publicName, Link documentLinks)
1617
public bool IsHasMany => GetType() == typeof(HasManyAttribute);
1718
public bool IsHasOne => GetType() == typeof(HasOneAttribute);
1819
public Link DocumentLinks { get; } = Link.All;
20+
public bool CanInclude { get; }
1921

2022
public abstract void SetValue(object entity, object newValue);
2123

@@ -26,8 +28,7 @@ public override string ToString()
2628

2729
public override bool Equals(object obj)
2830
{
29-
var attr = obj as RelationshipAttribute;
30-
if (attr == null)
31+
if (!(obj is RelationshipAttribute attr))
3132
{
3233
return false;
3334
}

0 commit comments

Comments
 (0)