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 1 commit
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
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Linq;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Hooks.Internal.Execution;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCoreExample.Models;

namespace JsonApiDotNetCoreExample.Definitions
{
public class BlogsHooksDefintion : ResourceHooksDefinition<Blog>
{
public BlogsHooksDefintion(IResourceGraph resourceGraph) : base(resourceGraph)
{
}

public override IEnumerable<Blog> OnReturn(HashSet<Blog> resources, ResourcePipeline pipeline)
{
return resources.Where(t => t.Title != "This should not be included");
}
}
}
3 changes: 3 additions & 0 deletions src/Examples/JsonApiDotNetCoreExample/Models/Author.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ public sealed class Author : Identifiable

[HasMany]
public IList<Article> Articles { get; set; }

[HasMany]
public ISet<Blog> Blogs { get; set; }
}
}
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 @@ -24,6 +24,7 @@ public sealed class ResourceDefinitionTests : FunctionalTestCollection<ResourceH
private readonly Faker<TodoItem> _todoItemFaker;
private readonly Faker<Person> _personFaker;
private readonly Faker<Article> _articleFaker;
private readonly Faker<Blog> _blogFaker;
private readonly Faker<Author> _authorFaker;
private readonly Faker<Tag> _tagFaker;

Expand All @@ -36,6 +37,10 @@ public ResourceDefinitionTests(ResourceHooksApplicationFactory factory) : base(f
.RuleFor(a => a.Caption, f => f.Random.AlphaNumeric(10))
.RuleFor(a => a.Author, f => _authorFaker.Generate());

_blogFaker = new Faker<Blog>()
.RuleFor(a => a.Title, f => f.Lorem.Word())
.RuleFor(a => a.CompanyName, f => f.Company.CompanyName());

_userFaker = new Faker<User>()
.CustomInstantiator(f => new User(_dbContext))
.RuleFor(u => u.UserName, f => f.Internet.UserName())
Expand Down Expand Up @@ -241,6 +246,30 @@ public async Task Article_Through_Secondary_Endpoint_Is_Hidden()
Assert.DoesNotContain(toBeExcluded, body);
}

[Fact]
public async Task Blog_Through_Secondary_Endpoint_Is_Hidden()
{
// Arrange
var blogs = _blogFaker.Generate(3).ToHashSet();
string toBeExcluded = "This should not be included";
blogs.First().Title = toBeExcluded;
var author = _authorFaker.Generate();
author.Blogs = blogs;

_dbContext.AuthorDifferentDbContextName.Add(author);
await _dbContext.SaveChangesAsync();

var route = $"/api/v1/authors/{author.Id}/blogs";

// Act
var response = await _client.GetAsync(route);

// Assert
var body = await response.Content.ReadAsStringAsync();
Assert.True(HttpStatusCode.OK == response.StatusCode, $"{route} returned {response.StatusCode} status code with body: {body}");
Assert.DoesNotContain(toBeExcluded, body);
}

[Fact]
public async Task Passport_Through_Secondary_Endpoint_Is_Hidden()
{
Expand Down