Skip to content

fix(repository): dedupe/filter source ids before querying in hasManyThrough inclusion resolver - #11784

Open
karthikchundi-commits wants to merge 1 commit into
loopbackio:masterfrom
karthikchundi-commits:fix/has-many-through-source-id-mutation
Open

karthikchundi-commits wants to merge 1 commit into
loopbackio:masterfrom
karthikchundi-commits:fix/has-many-through-source-id-mutation

Conversation

@karthikchundi-commits

Copy link
Copy Markdown

hasManyThrough's inclusion resolver passes its raw, unfiltered sourceIds array by reference straight into findByForeignKeys(), which wraps it in an {inq: [...]} where clause and hands that same array to the connector without cloning it first. A connector/query layer that sanitizes an inq array in place (e.g. stripping falsy values before running the query, as the in-memory connector does) then mutates that exact array out from under the caller — shrinking the very sourceIds array the resolver still needs below, unmodified, to correctly zip through-results back onto each original entity via flattenTargetsOfOneToManyRelation(). That silently misaligns or truncates the returned array whenever any source entity's key was undefined (e.g. excluded by a fields filter) or duplicated another entity's.

hasMany and hasOne inclusion resolvers already had this exact bug fixed (deduplicate(sourceIds).filter(e => e) before querying, same aliasing hazard, same root cause). hasManyThrough had no dedup/filter attempt at all — not even a partial one, unlike hasMany/hasOne before their own fix.

Found via a source-reading sweep of the other relation-inclusion resolvers (belongsTo, referencesMany) after noticing the pattern already fixed for hasMany/hasOnebelongsTo and referencesMany were independently reconfirmed clean (both already pass a genuinely fresh, deduplicated array, never the original reference).

Checklist

  • DCO (Developer Certificate of Origin) signed in all commits
  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes — added a regression test to has-many-through-inclusion-resolver.acceptance.ts mirroring the existing hasMany/hasOne regression tests: a duplicate source entity plus one with an undefined key, confirming the result stays length-3 and aligned with the input. Verified the test actually catches the bug by temporarily reverting the source fix and re-running — reproduces the exact truncation (length 2 instead of 3) this fix addresses.
  • Code conforms with the style guide — eslint and prettier both clean on the two changed files

Testing

cd packages/repository && npm run build
cd packages/repository-tests && npm run build
npx mocha "dist/__tests__/**/*.js"

126 passing, 8 pending (pre-existing, unrelated to this change), no failures.

…hrough inclusion resolver

hasManyThrough's inclusion resolver passed its raw, unfiltered
sourceIds array by reference straight into findByForeignKeys(),
which wraps it in an {inq: [...]} where clause and hands that same
array to the connector without cloning it first. A connector/query
layer that sanitizes an inq array in place (e.g. stripping falsy
values before running the query, as the in-memory connector does)
then mutates that exact array out from under the caller - shrinking
the very sourceIds array the resolver still needed, unmodified, to
correctly zip through-results back onto each original entity via
flattenTargetsOfOneToManyRelation(). That silently misaligns or
truncates the returned array whenever any source entity's key was
undefined (e.g. excluded by a fields filter) or duplicated another
entity's.

hasMany and hasOne inclusion resolvers already had this fixed
(deduplicate(sourceIds).filter(e => e) before querying); hasManyThrough
had no dedup/filter attempt at all - not even a partial one, unlike
hasMany/hasOne before their own fix.

Added a regression test to has-many-through-inclusion-resolver.acceptance.ts
mirroring the existing hasMany/hasOne regression tests: a duplicate
source entity plus one with an undefined key, confirming the result
stays length-3 and aligned with the input. Verified the test actually
catches the bug by temporarily reverting the source fix and
re-running - reproduces the exact truncation (length 2 instead of 3)
this fix addresses. `npx mocha dist/__tests__/**/*.js` in
repository-tests: 126 passing, 8 pending (pre-existing, unrelated),
no failures. eslint and prettier clean on both changed files.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Signed-off-by: Karth <karthik.chundi@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants