Skip to content

Additional fix OnReturn hook #938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ public override void BeforeImplicitUpdateRelationship(IRelationshipsDictionary<T
List<TodoItem> todos = resourcesByRelationship.GetByRelationship<Person>().SelectMany(kvp => kvp.Value).ToList();
DisallowLocked(todos);
}

public override IEnumerable<TodoItem> OnReturn(HashSet<TodoItem> resources, ResourcePipeline pipeline)
{
return resources.Where(t => t.Description != "This should not be included");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public object OnReturnRelationship(object resourceOrResources)
if (resourceOrResources is IEnumerable)
{
dynamic resources = resourceOrResources;
return _resourceHookExecutor.OnReturn(resources, ResourcePipeline.GetRelationship).ToArray();
return Enumerable.ToArray(_resourceHookExecutor.OnReturn(resources, ResourcePipeline.GetRelationship));
}

if (resourceOrResources is IIdentifiable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,31 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
responseDocument.Should().NotContain(toBeExcluded);
}

[Fact]
public async Task Can_hide_secondary_resource_from_ToOne_relationship_using_OnReturn_hook()
{
// Arrange
var person = _fakers.Person.Generate();
person.Passport = new Passport {IsLocked = true};

await _testContext.RunOnDatabaseAsync(async dbContext =>
{
dbContext.People.Add(person);
await dbContext.SaveChangesAsync();
});

var route = $"/api/v1/people/{person.Id}/passport";

// Act
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);

// Assert
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);

responseDocument.Data.Should().BeNull();
}


[Fact]
public async Task Can_hide_secondary_resource_from_ToMany_List_relationship_using_OnReturn_hook()
{
Expand Down Expand Up @@ -233,24 +258,27 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
public async Task Can_hide_secondary_resource_from_ToMany_Set_relationship_using_OnReturn_hook()
{
// Arrange
string toBeExcluded = "This should not be included";

var person = _fakers.Person.Generate();
person.Passport = new Passport {IsLocked = true};
person.TodoItems = _fakers.TodoItem.Generate(3).ToHashSet();
person.TodoItems.First().Description = toBeExcluded;

await _testContext.RunOnDatabaseAsync(async dbContext =>
{
dbContext.People.Add(person);
await dbContext.SaveChangesAsync();
});

var route = $"/api/v1/people/{person.Id}/passport";
var route = $"/api/v1/people/{person.Id}/todoItems";

// Act
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<string>(route);

// Assert
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);

responseDocument.Data.Should().BeNull();
responseDocument.Should().NotContain(toBeExcluded);
}

[Fact]
Expand Down