According to the JSON API definition, filters should be able to accept
GET /comments?filter[post]=1,2
(http://jsonapi.org/recommendations/#filtering)
At the moment what works is:
GET /comments?filter[post][operator]=in&filter[post][value][0]=1&filter[post][value][1]=2
Which is clearly a lot more verbose. The module should recognize the special case of no operator being provided, and a comma-separated list and expand that before calling the base Drupal condition into a "IN" operator and an array of values. No one should be forced to know Drupal's DB layer in order to use the JSON API.
In addition to that we want to also support the shorthand syntax: filter[field_a]=x+y+z. See #7 for more details.
Comments
Comment #2
e0ipsoThere is some talk about implementing a shorthand filtering syntax for simple cases (like operator
equalsand conjunctionAND). The relevant class should have anexpand()method ready to do this.Thanks for the contribution!
Comment #3
e0ipsoAny progress on this @jcnventura? Did you get any chance to implement this?
Comment #4
spleshkaI'm quite interested in getting this done. Development sugar, sweet! Let's try to get it further.
Comment #5
spleshkaAttaching the patch just for the initial review. If everyone is happy (especially d.org test bot), I'll supply the tests. Also, the docs will have to be enhanced.
Comment #6
spleshkaOh right, the patch :)
Comment #7
e0ipsoThis is looking good. You are moving in the right direction. I agree that we want to treat the
,as an OR. We need to make sure that all the scenarios are covered forfilter[field_a]=x,y,z:We should also implement
filter[field_a]=x+y+z(I'll update the IS):field_acan contain other values and still match.Current code review.
Let's create a constant for that
','. Maybe something likestatic::MULTI_OR_SEPARATOR.Let's create the full form (add the path) right away and return early, instead of letting the next step enhance it.
Comment #8
spleshkaOnce again you beat me - I was thinking about the same, but as a follow-up :) Glad we're thinking the same way.
I'd say that it makes sense to deliver #2852259: Add comprehensive test coverage to \Drupal\jsonapi\Routing\Param\Filter ahead of this story to make sure we're not breaking anything. So going to focus on tests coverage first, then get back to this one.
Comment #9
spleshkaThe issue mentioned above was fixed, but we got another one which ideally should be done ahead of this one: #2878654: [FEATURE] Implement validation of filter params. I'm going to work on the mentioned one and then get back to this story.
Comment #10
e0ipsoComment #11
spleshkaThis feature is now unblocked and can be taken into the development.
Comment #12
spleshkaMade some progress, would like to share the results and concerns:
1. There are no issues with
ORcondition.?filter[field]=a,b,cworks as expected for both single and multiple value fields.2. There is a big issue with
ANDcondition. To get it working for a field with multiple values as described in #7, we need to change the way query is built, which will likely bring an effort which doesn't worth it's value. This stackoverflow issue describes the issue and the first answer explains my concerns. I'll tell you even more: currently it's not possible to build this query properly even with full canonical condition form (unless I'm missing anything obvious).Having this said, my proposal would be to get back to the original issue idea to handle only short form for
ORcondition, which is what most of developers are looking for.How does it sound to you guys?
Comment #13
e0ipso@Spleshka can we just use whatever solution EntityQuery provides? I like the idea of JSON API just being a syntax to write EntityQueries.
Let's have:
Become (I believe that is the syntax):
We should leave the querying to core's EntityQuery.
What do you think?
Comment #14
spleshkaSo am I, that's exactly what I've mentioned the other day in slack channel. However, the resuling sql query doesn't make sense, because it will always return null result, no matter if we query a single or a multiple value field. This issue is described in details here.
Comment #15
wim leers#12 says the
ORcase already works. TheANDcase does not yet work.IMHO it's fine to only do the
ORcase here. Especially because http://jsonapi.org/recommendations/#filtering does not even mention theANDcase. In principle, we should only supportANDby using+if we can get the JSON API spec updated to say exactly that.For now, an
ANDcan be simulated like #13.What do you think, @e0ipso?
@Spleshka: in any case, this is going to need test coverage. #2874601: refactor(QueryBuilder): Improve testability/maintainability most likely made this easier to test now :)
Comment #16
wim leersThis actually seems pretty similar to #2968891: Allow extreme shorthand filtering: ?filter[promote]=1.
Comment #17
e0ipsoI'm changing my mind here, and I'm having second thoughts on the shorthand syntaxes. They seem to go in detriment of using a client library to construct the URLs, which I believe is the best practice. So we have 2 options:
1️⃣ Manual URL construction in the consumer + support for multiple different syntax to do the same thing.
2️⃣ Library verbose URL construction in the consumer + support for a single syntax.
Nowadays I'm leaning towards 2️⃣ although in the past I introduced the concept of the shorthand syntax.
Thoughts?
Comment #18
wim leers?filter[field][value]=2and?filter[field]=2), I think 2️⃣ destroys DX. Because even simple filtering then is made complex.Comment #19
e0ipsoI see your point. I somehow wished that people didn't construct URLs manually, but followed links instead and/or filters manually but use a library instead.
Perhaps my wishes are not realistic and making things more difficult is not the way to incentivize those best practices. So I may agree with your point.
Comment #20
wim leersEither way, this seems like a nice-to-have feature we can do in the
8.x-2.xbranch.Comment #21
wim leers