Hello,
When fixing #2924618: Fix channels operators, and seeing https://www.drupal.org/docs/8/modules/json-api/collections-filtering-sor...
4. Filtering with arrays: Get nodes created by users [admin, john].
You can give multiple values to a filter for it to search in. Next to the field and value keys you can add an operator to your condition. Usually it's "=" but you can also use "IN", "NOT IN", ">", "<", "<>", BETWEEN".For this example we're going to use the IN operator. Note that I added two square brackets behind the value to make it into an array.
I have tested with the operators, =, <, >, <>, and I obtained fatal error due to malformed requests.
Is it the documentation that is outdated? or did I found a bug?
(in the case of a bug, I know it should be better to have tests ;), I also need tests for the Entity share module, I will try to provide it when I will get some time)
| Comment | File | Size | Author |
|---|---|---|---|
| #12 | 2926089-12.patch | 1.8 KB | wim leers |
Comments
Comment #2
wim leersThanks for reporting this!
I thought #2874601: refactor(QueryBuilder): Improve testability/maintainability added test coverage for exactly this. Perhaps the error happens before we reach the query logic?
What does the fatal error say?
Comment #3
wim leersComment #4
grimreaperOn a URL like jsonapi/node/article?filter[title][condition][path]=title&filter[title][condition][operator]=<&filter[title][condition][value][0]=test1&filter[title][condition][value][1]=test2
I got the following error:
Comment #5
wim leersSo the code building the query binds a different number of variables for tokens than there are tokens.
Looks like #4 also provided steps to reproduce. This now needs a test. @Grimreaper, are you interested in doing that? :)
Comment #6
e0ipsoI think that this issue should be more like: Throw a nicer 4XX error when a user passes multiple values to a single value operator.
Is this the case @Grimreaper?
Comment #7
wim leersIf the JSON API filter is invalid, then #6 would make sense, but I don't think that's the case here?That is the case for the query in #4, so then, yes. :)
Although I think it's not really "multiple values to a single value operator", I think it's that the
<operator does not make sense for non-numeric data.Comment #8
grimreaperHello,
Thanks both for your replies.
I now see 4 points:
To respond to comment 4, I am interested in helping but I will not have time until many weeks or even months... sorry, I am on a big project and on other subjects.
Comment #9
e0ipso@Wim Leers @Grimreaper this issue is maybe better suited for Entity Query API. If any prior checking needs to be done it should be there. It should throw a nice error instead of letting the storage driver return with unparseable errors. But that is going to need a lot of special cases.
Comment #10
wim leersBut the Entity Query API is a low-level API, not designed for exposure to the broader world. I'm afraid it's initially on JSON API's shoulders to do the necessary validation logic, and then later we could try to port that to Entity Query API.
We should double-check with Entity Query API experts and maintainers to see whether this is correct.
Comment #11
e0ipsoI don't think we want to tackle this in this module. I don't think JSON API should / can babysit intricacies of the database storages to provide good error messages. Having JSON API know about the database implementation details, instead of relying on an abstraction (Entity Query API) is not where I'd like us to go.
Comment #12
wim leersI honestly don't understand what you want to happen here.
\Drupal\Core\Entity\Query\QueryInterfaceis clearly a low-level API. It doesn't come with validation built-in. This is why e.g. Views includes tons of validation to prevent invalid/nonsensical queries.I'm afraid that the only solution here is for JSON API to similarly validate what is allowed. It leaves the heavy lifting to the Entity Query API (
\Drupal\Core\Entity\Query\QueryInterface). But that doesn't mean it doesn't need to do anything else.Just like the REST module in Drupal core has to work around lots of things and has been working for years at this point to address gaps in underlying subsystems, the same is true for the JSON API module. The JSON API module has been mostly shielded from this because the REST module has fixed the most egregious problems already. But the REST module doesn't support collections, and therefore doesn't support filtering/sorting/… Which is why the JSON API module is now the first one to run into this problem.
I think that for now, we can just not yet deal with generating very helpful error messages, and just catch the generic exception and throw a generic error message. In most cases, I think users will be able to figure out that their query doesn't make sense. Having great error messages for every possible thing is great for DX, but obviously not a blocker for it to land in core.
Attached is a patch I'd propose for now to make the DX less scary.
Comment #13
e0ipsoMaybe, hopefully.
I agree. However it always depends on the cost to achieve that.
Well, views is a query builder. Here we use the query builder.
There could be a wrapper around Entity Query API that validates the query before executing it. JSON API could use that without having to implement it on this module. That has the benefit of:
1. Other projects won't need to re-do this.
2. This is clearly not JSON API's responsibility, hence we avoid scope creep.
3. We don't couple release cycles of Entity Query API validation and JSON API.
Comment #14
e0ipsoWith regards to the patch, I'm ambivalent. If you think muffling the error in the response is good, let's go with that. However, that makes the dev's work more difficult since the database error may have contained a clue about what's going on. What if we suppress the response but log the database error to the watchdog?
Comment #15
wim leersThere could be, but there isn't, and there won't be for a long time to come. It's a huge undertaking.
What do you think about the patch in #12? Do you find that acceptable? If you don't, then the error responses will remain as explicit as they are today.
I don't have a strong opinion about which way to go — it's choosing between the lesser of two evils… 😔
Comment #16
e0ipsoI was suggesting to start said wrapper outside of this project.
Well, that's true regardless, right? I mean, it's a huge undertaking to do it in JSON API as well.
Indeed! Evil me is happy about that 😈!
Comment #17
wim leersThe thing is that JSON API could do it gradually: it could add detection for this one case, then for another, etc. That'd then at least help JSON API users from running into the same few problems over and over again. Pragmatism over perfection. That'd be a far smaller effort.
But you're right that a complete solution would be just as huge an undertaking.
Hah! :) But you still didn't say what you thought about the patch in #12…
Comment #18
e0ipsoYou may have missed #14.
Comment #19
gabesullice@e0ipso @Wim Leers or @Grimreaper, are any of you clear on what actually needs to happen next here? I can't tell anymore.
Comment #20
wim leersNeither can I.