fix(query-core): treat undefined object properties as missing in partialMatchKey#10795
Conversation
…ialMatchKey Closes TanStack#3741 Query keys with object properties set to undefined were not treated as equivalent to missing properties during partial key matching (used by invalidateQueries, getQueriesData, etc.). For example, invalidateQueries({ queryKey: ['todos', { filter: undefined }] }) would not match a query with key ['todos'] or ['todos', {}], even though per the docs query keys are hashed deterministically and { filter: undefined } should be equivalent to {}. The fix skips keys with undefined values in the filter object, and also handles the case where the query key array is shorter than the filter array by treating objects with all-undefined values as empty (equivalent to no constraint).
📝 WalkthroughWalkthroughThe ChangespartialMatchKey undefined handling
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/query-core/src/__tests__/utils.test.tsx (1)
168-191: 💤 Low valueConsider adding a test for empty object matching shorter arrays.
The new implementation causes
['todos', {}](as filter) to also match['todos'](as query) sinceObject.values({}).every(v => v === undefined)is vacuously true. This is a natural consequence of the design but worth documenting with a test:it('should match shorter array against filter with empty object', () => { const a = ['todos'] const b = ['todos', {}] expect(partialMatchKey(a, b)).toEqual(true) })This would make the behavioral contract explicit.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/query-core/src/__tests__/utils.test.tsx` around lines 168 - 191, Add a unit test to document the behavior that a shorter key array matches a longer key when the longer key's last segment is an empty object: in packages/query-core/src/__tests__/utils.test.tsx add a test case invoking partialMatchKey with a = ['todos'] and b = ['todos', {}] and assert expect(partialMatchKey(a, b)).toEqual(true); reference the existing test block near other partialMatchKey cases (the 'should treat empty object and object with undefined values as equivalent' group) so the new test sits alongside related scenarios.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/query-core/src/__tests__/utils.test.tsx`:
- Around line 168-191: Add a unit test to document the behavior that a shorter
key array matches a longer key when the longer key's last segment is an empty
object: in packages/query-core/src/__tests__/utils.test.tsx add a test case
invoking partialMatchKey with a = ['todos'] and b = ['todos', {}] and assert
expect(partialMatchKey(a, b)).toEqual(true); reference the existing test block
near other partialMatchKey cases (the 'should treat empty object and object with
undefined values as equivalent' group) so the new test sits alongside related
scenarios.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 398e004e-edf8-4b23-a081-c4eb25bc863a
📒 Files selected for processing (2)
packages/query-core/src/__tests__/utils.test.tsxpackages/query-core/src/utils.ts
Closes #3741
🎯 Changes
Query keys with object properties set to
undefinedwere not treated as equivalent to missing properties during partial key matching (used byinvalidateQueries,getQueriesData, etc.).Before this fix:
After this fix:
This aligns
partialMatchKeybehavior with the documented behavior that query keys are hashed deterministically, where{ filter: undefined }should be equivalent to{}.✅ Checklist
pnpm run test:pr.🚀 Release Impact
Will generate changeset after review feedback.
Summary by CodeRabbit
Tests
Bug Fixes