fix: match array fields in where filters - #1771
Open
HafizMMoaz wants to merge 1 commit into
Open
Conversation
Filtering on a property nested inside an array field silently ignored the condition and returned every row. matchesWhere only recursed into fields that are plain objects, so an array field fell through to `continue`, which reads as a match. An array field now matches when at least one of its items matches the predicate, and fails to match when none of them do. All conditions in the predicate must be satisfied by the same item. Fixes typicode#1449
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1449
Problem
Filtering on a property nested inside an array field does not just fail to work, it silently drops the condition and returns every row.
Using the data from #1449:
{ "businesses": [ { "id": "1", "categories": [{ "id": 1, "name": "cat 1" }, { "id": 2, "name": "cat 2" }] } ] }No business has a category with id 10, but the row comes back anyway. Any value produces the same result, so the filter has no effect at all.
Cause
parseWherecorrectly turnscategories.id=10into{ categories: { id: { eq: 10 } } }.matchesWherethen reaches this branch:isJSONObjectexcludes arrays, so an array field skips the check and hitscontinue, and a skipped condition reads as a satisfied one. This is the same fail-open shape as #1731, but on a different path: there the intermediate field is missing, here it is present but is an array.Fix
An array field now matches when at least one of its items matches the predicate, and fails to match when none do. All conditions in the predicate must be satisfied by the same item, so
?categories.id=1&categories.name=cat 2does not match a business that has those values spread across two different categories.This works through
_whereas well, since both paths sharematchesWhere.Tests
matches-where.test.ts: 11 cases covering matching and non-matching items, comparison and string operators against array items, the same-item requirement, arrays of primitives, and arrays nested underor.app.test.ts: an end-to-end check of the exact URL form from the issue, asserting a match and an empty result.pnpm test,pnpm lintandpnpm typecheckall pass.Out of scope
Arrays of primitives (
?tags=redagainst"tags": ["red", "blue"]) still do not match. That needs a decision on whatneshould mean for a collection, so it seemed better kept separate from this fix.