You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched existing issues to ensure the bug has not already been reported
Mongoose version
8.9.7
Node.js version
20.18.0
MongoDB server version
8.0.4
Typescript version (if applicable)
No response
Description
After updating a discriminator key in a document with schema discriminators and getters, the Document includes the fields for the current discriminator, as expected. However, Document.toObject() returns all fields, including those defined in a different discriminator schema, but only applies getters to the fields matching the current discriminator.
Steps to Reproduce
The following script will fail when checking the discriminator fields. In particular, the Document contains only the fields relevant to the discriminator, but the object returned by Document.toObject() contains all fields, including those for the other discriminator schema.
As shown in the script above, since a Document with a discriminator includes only the fields relevant to the currently set discriminator key (prior to calling toObject()), I would expect the object returned by toObject() to also only include those fields defined in that discriminator schema.
If all fields must be returned by toObject() (even those not relevant to the current discriminator schema) I would expect all getters to have been called.
The text was updated successfully, but these errors were encountered:
lcrosetto
changed the title
Document.prototype.toObject() with discriminators returns all fields but selectively applies getters
Document.prototype.toObject() with discriminators returns all fields and selectively applies getters
Feb 1, 2025
Prerequisites
Mongoose version
8.9.7
Node.js version
20.18.0
MongoDB server version
8.0.4
Typescript version (if applicable)
No response
Description
After updating a discriminator key in a document with schema discriminators and getters, the Document includes the fields for the current discriminator, as expected. However, Document.toObject() returns all fields, including those defined in a different discriminator schema, but only applies getters to the fields matching the current discriminator.
Steps to Reproduce
The following script will fail when checking the discriminator fields. In particular, the Document contains only the fields relevant to the discriminator, but the object returned by Document.toObject() contains all fields, including those for the other discriminator schema.
Expected Behavior
As shown in the script above, since a Document with a discriminator includes only the fields relevant to the currently set discriminator key (prior to calling toObject()), I would expect the object returned by toObject() to also only include those fields defined in that discriminator schema.
If all fields must be returned by toObject() (even those not relevant to the current discriminator schema) I would expect all getters to have been called.
The text was updated successfully, but these errors were encountered: