-
-
Notifications
You must be signed in to change notification settings - Fork 159
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
Primary's OnApplyFilter being applied to relationship #1671
Comments
Hi @max-maurice, thanks for reporting this. I can reproduce the problem, it's a bug indeed. This happens only when the inverse relationship is used to determine the total resource count. Fetching the data itself doesn't suffer from this problem. As a temporary workaround, turn off In a nutshell, the problem is that the resource definition returns To fix this bug, the easiest solution would be to disable fetching total resource count if a resource definition adds a filter. Alternatively, come up with a way to make it work with third-party expressions. I'm open to ideas. Oh, if you're trying to implement soft-deletion, the only reliable way is by using EF Core's |
Hi @bkoelman Thank you for looking into this issue! Soft-deletion only serves as a poor example to demonstrate the issue. Disabling I've started implementing Your |
The point of implementing In which cases doesn't I don't recommend relying on the experimental code. I haven't found a good way to make it work with custom expressions. Unless that's solved, I don't think it's going to ship. Then the next best thing is to support total count only for many-to-many relationships. Would that work for you? |
Oooh, my implementation of // Program.cs
services.AddScoped<IInverseNavigationResolver, CustomInverseNavigationResolver>();
// CustomInverseNavigationResolver.cs
public class CustomInverseNavigationResolver : IInverseNavigationResolver
{
public void Resolve()
{
// no implementation, no inverse navigation properties
}
} With this, GET requests to |
Yes, there are downsides. Without setting
To reduce the impact, I recommend only clearing the Alternatively, the experimental code can be used; just don't expect future versions to return the total resource count for many-to-one relationships. |
Thanks, @bkoelman ! I've followed your recommendation and it works perfectly. For reference: public class CustomInverseNavigationResolver : IInverseNavigationResolver
{
readonly InverseNavigationResolver inverseNavigationResolver;
readonly IResourceGraph resourceGraph;
public CustomInverseNavigationResolver(InverseNavigationResolver inverseNavigationResolver,
IResourceGraph resourceGraph)
{
this.inverseNavigationResolver = inverseNavigationResolver;
this.resourceGraph = resourceGraph;
}
public void Resolve()
{
inverseNavigationResolver.Resolve();
// https://github.com/json-api-dotnet/JsonApiDotNetCore/issues/1671#issuecomment-2641524344
var authorType = resourceGraph.GetResourceType<Author>();
var authorBooksRel = authorType.GetRelationshipByPropertyName(nameof(Author.Books));
authorBooksRel.InverseNavigationProperty = null;
}
} |
DESCRIPTION
In my
AuthorResourceDefinition
the OnApplyFilter method is overridden and adds a filter expression. When requesting a relationship, theOnApplyFilter()
of AuthorResourceDefinition gets called and adds the filter as expected. The ResourceFieldChainExpression however is applied on the secondary resource level. This fails, because the resource field name of the primary resource does not exist on the secondary one.STEPS TO REPRODUCE
AuthorResourceDefinition
overrideOnApplyFilter()
like shown at https://www.jsonapi.net/usage/extensibility/resource-definitions.html#change-filters:/authors/1/relationships/books
EXPECTED BEHAVIOR
The request returns the books of author ID 1.
ACTUAL BEHAVIOR
The request returns an error:
VERSIONS USED
The text was updated successfully, but these errors were encountered: