Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging of discrimintor schema "_indexes" duplicates when both 'from' and 'to' are schema instances #14586

Closed
2 tasks done
shlomiassaf opened this issue May 11, 2024 · 1 comment
Labels
needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue

Comments

@shlomiassaf
Copy link
Contributor

shlomiassaf commented May 11, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.3.4

Node.js version

18.2

MongoDB server version

6

Typescript version (if applicable)

5.4.5

Description

When merging a discriminator schema, when both source and target are Schema instances the following logic is applied:

  1. The source schema (from) is cloned
  2. The cloned source from step 1 is merged into the target (to)

schemaMerge(to[key], from[key].clone(), true);

Step 1 (clone) will also close the _indexes property of the Schema creating new copied for the array and the index definitions.

function cloneArray(arr, options) {
let i = 0;
const len = arr.length;
const ret = new Array(len);
for (i = 0; i < len; ++i) {
ret[i] = clone(arr[i], options, true);
}
return ret;
}

Next, the merge operation will concat the _indexes from source and target effectivly duplicating the _indexes on every clone.

s1._indexes = s1._indexes.concat(s2._indexes || []);

For multiple discriminators and nested schema's this can lead to memory leak/overflow and other mongo implications.

Expected Behavior

when merging discriminator schemas, the clone phase should be able to optionally ignore _indexes cloning and the "merge" itself should clone and concat properly

@IslandRhythms IslandRhythms added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label May 13, 2024
@shlomiassaf
Copy link
Contributor Author

Not an issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue
Projects
None yet
Development

No branches or pull requests

2 participants