Skip to content

Commit 8fc015a

Browse files
committed
feat: throw error if trying to access databasevalues when loading is turned off
1 parent 29abae0 commit 8fc015a

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/JsonApiDotNetCore/Hooks/Execution/EntityDiff.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11

2+
using System;
23
using System.Collections;
34
using System.Collections.Generic;
5+
using System.Linq;
6+
using JsonApiDotNetCore.Internal;
47
using JsonApiDotNetCore.Models;
58

69
namespace JsonApiDotNetCore.Hooks
710
{
11+
812
/// <summary>
913
/// A helper class that provides insight in what is to be updated. The
1014
/// <see cref="IEntityDiff{TEntity}.RequestEntities"/> property reflects what was parsed from the incoming request,
@@ -21,14 +25,21 @@ public interface IEntityDiff<TEntity> : IAffectedRelationships<TEntity> where TE
2125

2226
public class EntityDiff<TEntity> : AffectedRelationships<TEntity>, IEntityDiff<TEntity> where TEntity : class, IIdentifiable
2327
{
28+
private readonly HashSet<TEntity> _databaseEntities;
29+
public HashSet<TEntity> DatabaseEntities { get => _databaseEntities ?? ThrowNoDbValuesError(); }
30+
2431
public HashSet<TEntity> RequestEntities { get; private set; }
25-
public HashSet<TEntity> DatabaseEntities { get; private set; }
2632
public EntityDiff(IEnumerable requestEntities,
2733
IEnumerable databaseEntities,
28-
Dictionary<RelationshipProxy, IEnumerable> relationships) : base (relationships)
34+
Dictionary<RelationshipProxy, IEnumerable> relationships) : base(relationships)
2935
{
3036
RequestEntities = (HashSet<TEntity>)requestEntities;
31-
DatabaseEntities = (HashSet<TEntity>)databaseEntities;
37+
_databaseEntities = (HashSet<TEntity>)databaseEntities;
38+
}
39+
40+
private HashSet<TEntity> ThrowNoDbValuesError()
41+
{
42+
throw new MemberAccessException("Cannot access database entities if the LoadDatabaseValues option is set to false");
3243
}
3344
}
3445
}

0 commit comments

Comments
 (0)