Skip to content

Commit 34d67f7

Browse files
committed
feat: validate hook response for GetSingle
1 parent d3aec2d commit 34d67f7

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/JsonApiDotNetCore/Hooks/ResourceHookExecutor.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ IResourceGraph graph
4747
_layerFactory = new EntityTreeLayerFactory(meta, graph, _context);
4848
}
4949

50-
51-
5250
public virtual void BeforeRead<TEntity>(ResourceAction pipeline, string stringId = null) where TEntity : class, IIdentifiable
5351
{
5452
var hookContainer = _meta.GetResourceHookContainer<TEntity>(ResourceHook.BeforeRead);
@@ -227,6 +225,7 @@ public virtual IEnumerable<TEntity> OnReturn<TEntity>(IEnumerable<TEntity> entit
227225
/// this is not updating unique entities internally!!!! say [a_1, a_2] => [a_1],
228226
/// then nested hooks for relations of a_2 are still being fired
229227
entities = entities.Intersect(filteredUniqueEntities, Comparer).Cast<TEntity>();
228+
ValidateHookResponse(entities);
230229
}
231230
var nextLayer = _layerFactory.CreateLayer(layer);
232231
RecursiveOnReturn(nextLayer, pipeline);
@@ -311,7 +310,7 @@ public virtual void AfterCreate<TEntity>(IEnumerable<TEntity> entities, Resource
311310
if (hookContainer != null)
312311
{
313312
var uniqueEntities = layer.GetAllUniqueEntities().Cast<TEntity>();
314-
hookContainer?.AfterCreate(uniqueEntities, pipeline);
313+
hookContainer.AfterCreate(uniqueEntities, pipeline);
315314
}
316315
EntityTreeLayer nextLayer = _layerFactory.CreateLayer(layer);
317316

@@ -324,7 +323,7 @@ public virtual void AfterUpdate<TEntity>(IEnumerable<TEntity> entities, Resource
324323
if (hookContainer != null)
325324
{
326325
var uniqueEntities = layer.GetAllUniqueEntities().Cast<TEntity>();
327-
hookContainer?.AfterUpdate(uniqueEntities, pipeline);
326+
hookContainer.AfterUpdate(uniqueEntities, pipeline);
328327
}
329328
}
330329

@@ -420,12 +419,12 @@ IList LoadDbValues(IList entities, List<RelationshipProxy> relationships, Type e
420419
/// </summary>
421420
/// <param name="returnedList"> The collection returned from the hook</param>
422421
/// <param name="pipeline">The pipeine from which the hook was fired</param>
423-
protected void ValidateHookResponse(object returnedList, ResourceAction pipeline = 0)
422+
void ValidateHookResponse<T>(IEnumerable<T> returnedList, ResourceAction pipeline = 0)
424423
{
425-
if (pipeline != ResourceAction.None && SingleActions.Contains(pipeline) && ((IEnumerable)returnedList).Cast<object>().Count() > 1)
424+
if (pipeline == ResourceAction.GetSingle && returnedList.Count() > 1)
426425
{
427-
throw new ApplicationException("The returned collection from this hook may only contain one item in the case of the" +
428-
pipeline.ToString() + "pipeline");
426+
throw new ApplicationException("The returned collection from this hook may contain at most one item in the case of the" +
427+
pipeline.ToString("G") + "pipeline");
429428
}
430429
}
431430

0 commit comments

Comments
 (0)