Skip to content

Allow filtering on properties of HasMany relationships #238

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

Closed
jaredcnance opened this issue Mar 8, 2018 · 8 comments
Closed

Allow filtering on properties of HasMany relationships #238

jaredcnance opened this issue Mar 8, 2018 · 8 comments

Comments

@jaredcnance
Copy link
Contributor

jaredcnance commented Mar 8, 2018

Consider the following:

public class Foo : Identifiable
{
   [HasMany("bars")]
   public List<Bar> Bars { get; set; }
}

public class Bar : Identifiable
{
   [Attr("isActive")] public bool IsActive { get; set; }

   [HasOne("foo")]
   public Foo Foo { get; set; }
   public int FooId { get; set; }
}

If you try to filter

/foos?filter=bars.isActive=true

It will result in an exception

ArgumentException: 'IsActive' is not a member of type 'System.Collections.Generic.List`1[Bar]'
Parameter name: propertyOrFieldName
   at System.Linq.Expressions.Expression.PropertyOrField(Expression expression, String propertyOrFieldName)
   at JsonApiDotNetCore.Extensions.IQueryableExtensions.Filter[TSource](IQueryable`1 source, RelatedAttrFilterQuery filterQuery)
   at JsonApiDotNetCore.Services.EntityResourceService`2.ApplySortAndFilterQuery(IQueryable`1 entities)
   at JsonApiDotNetCore.Services.EntityResourceService`2.<GetAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at JsonApiDotNetCore.Controllers.BaseJsonApiController`2.<GetAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at JsonApiDotNetCore.Controllers.JsonApiController`2.<GetAsync>d__3.MoveNext()

We should consider an approach similar to EF Plus's IncludeFilter : http://entityframework-plus.net/query-include-filter

Workaround

The current workaround is to query the child resource directly and expose the parent id as an attribute on the resource:

GET /articles?filter[authorId]=1&filter[isActive]=true
@XaserAcheron
Copy link
Contributor

This is really kicking my rump -- I'm trying to do this with a HasManyThrough relationship so the posted workaround doesn't apply. Any advice, by chance?

@maurei
Copy link
Member

maurei commented Apr 24, 2019

@XaserAcheron is this still an issue? We can consider looking into it after releasing 3.2.0

@XaserAcheron
Copy link
Contributor

It's still a bit troublesome -- I ended up having to do some non-ideal workarounds from the UI side in order to get around the limitation -- but I'm not critically blocked or anything, thankfully. :)

Thanks for the help, BTW!

@maurei
Copy link
Member

maurei commented Apr 24, 2019

That is, indeed, suboptimal. I think this issue is one of the more workable issues so we might still look into it after v3.2.0. Also, you are encouraged to get started on it (in which case you can PM me with any questions)

@wisepotato
Copy link
Contributor

@maurei this would be easily done in the new filtering/hooks backend.

@maurei
Copy link
Member

maurei commented Apr 24, 2019

In that case you'd have to "hardcode" the filter in a hook, which doesnt allow for much flexibility like when filters are added dynamically with query params.

Although might be possible to inject JsonApiContext in the hooks and access the url query params as such, that feels hacky. I think it should be supported off the shelve

I'm wondering though how this relates to the infamous Filtered Include issue of EF Core (#1833). Im starting to think we're facing the same issue here

@wisepotato
Copy link
Contributor

I meant that we can parse the query filters and apply them on the needed items, as we are parsing them in memory anyway, refardless of hooks

@maurei
Copy link
Member

maurei commented Oct 10, 2019

Workaround is introduced in v4: resource hooks

@maurei maurei closed this as completed Oct 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants