Skip to content

Commit 9fe260e

Browse files
authored
Additional fix OnReturn hook (#938)
* Additional fix OnReturn hook
1 parent 7255c9e commit 9fe260e

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

src/Examples/JsonApiDotNetCoreExample/Definitions/TodoItemHooksDefinition.cs

+5
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,10 @@ public override void BeforeImplicitUpdateRelationship(IRelationshipsDictionary<T
2929
List<TodoItem> todos = resourcesByRelationship.GetByRelationship<Person>().SelectMany(kvp => kvp.Value).ToList();
3030
DisallowLocked(todos);
3131
}
32+
33+
public override IEnumerable<TodoItem> OnReturn(HashSet<TodoItem> resources, ResourcePipeline pipeline)
34+
{
35+
return resources.Where(t => t.Description != "This should not be included");
36+
}
3237
}
3338
}

src/JsonApiDotNetCore/Hooks/Internal/ResourceHookExecutorFacade.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public object OnReturnRelationship(object resourceOrResources)
122122
if (resourceOrResources is IEnumerable)
123123
{
124124
dynamic resources = resourceOrResources;
125-
return _resourceHookExecutor.OnReturn(resources, ResourcePipeline.GetRelationship).ToArray();
125+
return Enumerable.ToArray(_resourceHookExecutor.OnReturn(resources, ResourcePipeline.GetRelationship));
126126
}
127127

128128
if (resourceOrResources is IIdentifiable)

test/JsonApiDotNetCoreExampleTests/IntegrationTests/ResourceHooks/ResourceHookTests.cs

+32-4
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,31 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
202202
responseDocument.Should().NotContain(toBeExcluded);
203203
}
204204

205+
[Fact]
206+
public async Task Can_hide_secondary_resource_from_ToOne_relationship_using_OnReturn_hook()
207+
{
208+
// Arrange
209+
var person = _fakers.Person.Generate();
210+
person.Passport = new Passport {IsLocked = true};
211+
212+
await _testContext.RunOnDatabaseAsync(async dbContext =>
213+
{
214+
dbContext.People.Add(person);
215+
await dbContext.SaveChangesAsync();
216+
});
217+
218+
var route = $"/api/v1/people/{person.Id}/passport";
219+
220+
// Act
221+
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
222+
223+
// Assert
224+
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
225+
226+
responseDocument.Data.Should().BeNull();
227+
}
228+
229+
205230
[Fact]
206231
public async Task Can_hide_secondary_resource_from_ToMany_List_relationship_using_OnReturn_hook()
207232
{
@@ -233,24 +258,27 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
233258
public async Task Can_hide_secondary_resource_from_ToMany_Set_relationship_using_OnReturn_hook()
234259
{
235260
// Arrange
261+
string toBeExcluded = "This should not be included";
262+
236263
var person = _fakers.Person.Generate();
237-
person.Passport = new Passport {IsLocked = true};
264+
person.TodoItems = _fakers.TodoItem.Generate(3).ToHashSet();
265+
person.TodoItems.First().Description = toBeExcluded;
238266

239267
await _testContext.RunOnDatabaseAsync(async dbContext =>
240268
{
241269
dbContext.People.Add(person);
242270
await dbContext.SaveChangesAsync();
243271
});
244272

245-
var route = $"/api/v1/people/{person.Id}/passport";
273+
var route = $"/api/v1/people/{person.Id}/todoItems";
246274

247275
// Act
248-
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
276+
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<string>(route);
249277

250278
// Assert
251279
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
252280

253-
responseDocument.Data.Should().BeNull();
281+
responseDocument.Should().NotContain(toBeExcluded);
254282
}
255283

256284
[Fact]

0 commit comments

Comments
 (0)